Is there any way to change the color on a particular word in a string?
Text("some long string")
Now, I want to set a color only for 'long'.
Can someone tell me how I can do this programmatically?
Example:
I am long, a really long and long string in some variable, a long one
Now here I want to separate the word 'long'. I can separate simple strings to highlight one word, but I am not sure on how to find and highlight all the cases of that one word.
Wrap the word in a TextSpan and assign style
properties to change the text appearance and use RichText instead of Text
RichText( text: TextSpan( text: 'Hello ', style: DefaultTextStyle.of(context).style, children: <TextSpan>[ TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)), TextSpan(text: ' world!'), ], ), )
or use the Text.rich
constructor https://docs.flutter.io/flutter/widgets/Text-class.html
const Text.rich( TextSpan( text: 'Hello', // default text style children: <TextSpan>[ TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)), TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)), ], ), )