I have a use-case where I want to change the colour of a letter in a String inside a Text
widget. Let's say that my String is "Flutter". On pressing the FloatingActionButton
, I want the letter 'F' to change its colour, then on the next press of the FloatingActionButton
, change the colour of letter 'l', then 'u', then 't', then 't', then 'e' and then finally 'r' with every FloatingActionButton
press.
Please don't suggest having a different Text
widget for every letter.
Steps to change theme text color in Flutter You can change theme text color in Flutter, by defining the TextTheme (inside MaterialApp) and then adding headings type. For example, headline1 , headline2 , BodyText1 , and so on. After that, you can assign the TextStyle widget with the color of your choice.
Instead, you can parse the string into a value and construct a new Color object. Color color = new Color(0x12345678); String colorString = color. toString(); // Color(0x12345678) String valueString = colorString. split('(0x')[1].
You can do this using a RichText
widget and different TextSpan
s. You can read the documentation to familiarize yourself with it more.
https://api.flutter.dev/flutter/widgets/RichText-class.html
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With