DEV Community

Cover image for Using gradients in Flutter
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

6 1

Using gradients in Flutter

Yesterday, I wondered how to create gradients in Flutter, so let's look at how this works.

Gradients, in general, can give us that extra fancy feeling in our app.
While researching this article, I learned that Flutter allows you to modify everything, another great win of Flutter.

Basic gradients in Flutter

The main way to add gradients to our Flutter app is to use a box decorator. You can use decorations on a couple of elements, including a container.

Let's look at how that looks.

We'll start using our basic Flutter app as our blank canvas again.

return Container(
  decoration: BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.topRight,
      end: Alignment.bottomLeft,
      colors: [
        Color(0xff9796F0),
        Color(0xffFBC7D4),
      ],
    ),
  ),
  child: Center(
    child: Text(
      'Hello world 👋',
      style: TextStyle(
        fontSize: 50.0,
        color: Colors.white,
      ),
    ),
  ),
);
Enter fullscreen mode Exit fullscreen mode

I've used hex colors, but one could also opt for Colors.red and Colors.blue.

We have to prefix the hex value with 0xff and then the hex value when using the hex colors.

Example: #2BC0E4 becomes: 0xff2BC0E4.

This sample example will give the container a nice gradient.

Flutter Gradient

Adding more stops to gradients

Sometimes two colors are not enough, and we'd like some more stops. We can achieve this in Flutter by defining the stops and the colors.

decoration: BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.topRight,
      end: Alignment.bottomLeft,
      stops: [
        0.1,
        0.4,
        0.9,
      ],
      colors: [
        Color(0xffD16BA5),
        Color(0xff86A8E7),
        Color(0xff5FFBF1),
      ],
    ),
)
Enter fullscreen mode Exit fullscreen mode

We introduced a new element which is the stops. This is an array of points stating at which space the gradient should change.

The stop value runs from 0 to 1.
Then we also added that amount of colors, so the arrays match.

Look at this example. The colors are not equal widths.

Flutter multi stop gradients

Changing the gradient direction

Of course, we don't always want the gradient to run like that.
We can easily change the from/to on the gradient configuration to change the gradient flow.

Let's try and make it from left to right.

decoration: BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
      colors: [
        Color(0xffEAECC6),
        Color(0xff2BC0E4),
      ],
    ),
),
Enter fullscreen mode Exit fullscreen mode

Resulting in the following gradient:

Flutter left-right gradient

That's it. We can now use this gradient decorator to give our application a nice finished touch!

If you want to see the source code for today, check it out on this GitHub repo.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay