Flutter different color for different letters in a Text widget
Asked Answered
K

2

10

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.

Kohinoor answered 26/12, 2018 at 20:10 Comment(0)
D
18

You can do this using a RichText widget and different TextSpans. You can read the documentation to familiarize yourself with it more. https://api.flutter.dev/flutter/widgets/RichText-class.html

Dejadeject answered 26/12, 2018 at 20:25 Comment(0)
M
5
RichText(
  text: TextSpan(
    text: 'Hello ',
    style: DefaultTextStyle.of(context).style,
    children: const <TextSpan>[
      TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(text: ' world!'),
    ],
  ),
)
Mackenziemackerel answered 16/8, 2022 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.