I use some RichText throughout my application to switch colors between text and display widgets in between.
E.g.
RichText(
text: TextSpan(text: 'text1', style: TextStyle(color: Colors.black),
children: [
WidgetSpan(alignment: PlaceholderAlignment.middle, child: SomeWidget()),
TextSpan(text: 'text2', style: TextStyle(color: Colors.blue),
WidgetSpan(alignment: PlaceholderAlignment.middle, child: Icon(Icons.someIcon)),
TextSpan(text: 'text3'),
]
));
The problem now is that when switching to dark mode, the black test is contrasted to a black background and thus invisible. The regular Text('some text') widget will automatically display black by default but will become white in dark mode. How can I achieve the same or something similar when using RichtText?
Well you defined your
RichTextlike this:TextSpan(text: 'text1', style: TextStyle(color: Colors.black)As you know the
colorargument that you pass to a Widget has the highest priority, therefore, yourTextSpanwill always be black.One solution is instead of hardcoding the color you can do this:
If we look at the definition of this
themeit looks like this:If you use this approach your
TextSpanand normalTextwidgets should have the same color. In general one should always prefer working withthemesover hardcoding colors. If your user switches to dark mode the theme will switch too and therefore, all styles should be switched correctly as well.