How to create gradient text in Flutter

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

When I started working on gradients in Flutter yesterday, I also had a look at text gradients as those intrigue me. However, quickly fell down the rabbit hole of methods that work on paper but don't work stable.
Today I'll guide you through creating gradient text in Flutter.
A shader is by far the easiest solution. We can create a custom shader and use that as the foreground option for our text element.
It looks like this:
final Shader linearGradient = LinearGradient(
colors: <Color>[Colors.pink, Colors.green],
).createShader(
Rect.fromLTWH(0.0, 0.0, 200.0, 70.0),
);
child: Text(
'Hello Gradients!',
style: new TextStyle(
fontSize: 60.0,
fontWeight: FontWeight.bold,
foreground: Paint()..shader = linearGradient),
),
However, when we run this code we get the following output:

As you can see above, the gradient doesn't stick right on the text. It can move depending on the screen size.
This is because we used the Rect code in the shader.
Rect.fromLTWH(0.0, 0.0, 200.0, 70.0),
As you can see, the 200 and 70 are fixed number, which would be magic numbers for a mobile screen. However, this doesn't scale and will give us headaches in the long term.
Another way to do this is to use a shader mask. The idea is similar to the above shader example, except we can now use the bounds as a callback.
final gradient = LinearGradient(
colors: [Colors.pink, Colors.green],
);
ShaderMask(
shaderCallback: (Rect bounds) {
return gradient.createShader(Offset.zero & bounds.size);
},
child: Center(
child: Text(
'Hello Gradients!',
style: new TextStyle(
color: Colors.white,
fontSize: 60.0,
fontWeight: FontWeight.bold,
),
),
),
)
The shader mask has a callback function, so we can use the size of the bounds to give it a more accurate position.
Note: Do remember this method only works when using white-colored text.
This shader mask will result in the following, a scalable gradient text.

To be honest, I'm reinventing the wheel here, so instead of creating this ourselves, we can use people's plugins to make life easier for us.
These are excellent packages, the package Tony made is just for text and implements the shader mask method, so it's easier for us to re-use throughout our codebase.
The gradient UI kit by Sim is a more advanced toolkit, including not just text elements but all kinds of elements that can now have gradients.
I do hope this article showed you the differences between the Flutter gradient text options. And you're able to create some fantastic gradients.
You can view the complete code with both examples on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter