Flutter how to paint custom gradients

1,047

pskinks comment was right and I totally overlooked this option. Using the Alignment(x,y) was the key here.

Here is the solution for my above problem.

BoxDecoration(
      gradient: LinearGradient(
        colors: [
          AppColors.roseGradientColor,
          AppColors.purpleInAppGradientColor,
          AppColors.blueInAppGradientColor
        ],
        begin: Alignment(-0.7,12),
        end: Alignment(1,-2),
      ),
        borderRadius: BorderRadius.only(bottomLeft: Radius.circular(25),bottomRight: Radius.circular(25))
    )
Share:
1,047
Christian X
Author by

Christian X

Updated on December 18, 2022

Comments

  • Christian X
    Christian X over 1 year

    I recently experienced a problem when I was trying to create following AppBar with gradient.

    Expected color gradient

    When I tried to replicate this design in flutter with the colors rose = Color(0xFFec15ee), purple = Color(0xFF8561f5) and blue = Color(0xFF1eaefc) and set the alignment property accordingly it somehow gave me not the expected result

    BoxDecoration(
          gradient: LinearGradient(
            colors: [
              AppColors.roseGradientColor,
              AppColors.purpleInAppGradientColor,
              AppColors.blueInAppGradientColor
            ],
            stops: [
              0.0,
              0.05,
              1.0
            ],
            begin: Alignment.bottomLeft,
            end: Alignment.topRight
          ),
            borderRadius: BorderRadius.only(bottomLeft: Radius.circular(25),bottomRight: Radius.circular(25))
        )
    

    d

    Just imagine the opacity wouldnt be there. As you can see I only want the rose to be aligned at the bottomLeft and not to expand up to topLeft as shown in the example.

    My question how can I do that. There must a way to do that within CustomPainter yet I haven't found the right way.

    • pskink
      pskink about 4 years
      insetead of Alignment.topRight use Alignment that is far above top - see Alignment(double, double) constructor
  • pskink
    pskink about 4 years
    ok, but why -0.7 in begin alignment? why not -1? -1 is for 'left"
  • Christian X
    Christian X about 4 years
    I gotta be honest, I tested the design with -1, -5, -10, -0.2, -0.5 and so on and I found -0.7 to be the closest at the design I want. This value was 100% found through trial and error ;)
  • pskink
    pskink about 4 years
    the most important is that final result looks ok ;-)