Flutter RichText is not showing - flutter

I have a regular normal rich text widget in a Centered per https://api.flutter.dev/flutter/widgets/RichText-class.html within a Scaffold. I can't see it. I can't see anything. The debug tools show that the Rich Text is indeed there in the widget heirarchy (though I can't see the TextSpans within it, presumably they're in there.)
Scaffold(body: Center(child:
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
),
));

I tried an android build to see whether this was a bug specific to linux desktop builds. It wasn't! It's happening here too. However, the contrast ratio of my phone is high enough that I can just barely see the text in there, White on Off White. It turns out that, unlike the Text widget, the default color for RichText text is white, same as the theme's background, so the text wont be visible.
(Is it possible the app is responding to the system-wide dark mode theme? Nope. Switching theme doesn't affect anything.)
So, WHY is this the default color? Because DefaultTextStyle is bad and wrong I guess? Use Theme.of(context).textTheme.bodyText1 and you will get the results you expect.

RichText is a basic, lower-level approach to rendering text and intentionally does not use theme styles.
However, Text.rich does:
Scaffold(body: Center(
child: Text.rich(
TextSpan(
text: 'Hello ',
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
),
));

Related

Flutter: RichText with darktheme

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 RichText like this:
TextSpan(text: 'text1', style: TextStyle(color: Colors.black)
As you know the color argument that you pass to a Widget has the highest priority, therefore, your TextSpan will always be black.
One solution is instead of hardcoding the color you can do this:
Theme.of(context).textTheme.bodyText2?.color
If we look at the definition of this theme it looks like this:
/// The default text style for [Material].
TextStyle? get bodyText2 => bodyMedium;
If you use this approach your TextSpan and normal Text widgets should have the same color. In general one should always prefer working with themes over hardcoding colors. If your user switches to dark mode the theme will switch too and therefore, all styles should be switched correctly as well.

How to localize text with TextSpan widget?

I am using Flutter Intl android studio plugin for ready made localization.
But in my scenario i have the widget like this,
Text.rich(
TextSpan(
text: 'It is time to',
children: [
TextSpan(
text: 'Stay Relaxed',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
)
So how can i translate a single message "It is time to Stay Relaxed" and use in Text.rich widget?
EDIT: More explanation
The text can not be handled as one piece. Each part of needs to be handled separately. Because from the TextSpans concern, those are different texts. So you need to create two different texts within the arb file.
From the Flutter Intl's perspective, these are two different components. TextSpan has its own drawing perspective. That is why you need to create two different components for these items.
There are packages out there like https://pub.dev/packages/localized_rich_text but I did not use it. In our app I use it like the following.
Flutter Intl creates the localized elements as follows:
AppLocalizations.of(context).tranlatedElement;
//or
AppLocalizations.current.tranlatedElement;
So your Code should be like the following
Text.rich(
TextSpan(
text: AppLocalizations.current.relaxTextPrefix,
children: [
TextSpan(
text: AppLocalizations.current.relaxTextSuffix,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
),
)

Is there a way to make TextSpan.text selectable in flutter?

I've a webpage made with flutter. I'm using RichText to make paragraphs and each of those have TextSpan children widgets. I want to make them selectable, searchable(when ctrl+f is used within the webpage) and on-page seo friendly.
Is there a way to achieve it without using SelectableText as it doesn't suits my usecase?
try extended_text
or
have you tried SelectableText.rich?
SelectableText.rich(
TextSpan(
children: [
TextSpan(text: 'Hello '),
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(text: ' world!'),
],
),
)

SelectableText.rich in Flutter Web

I'm trying to use the following code snippet :
SelectableText.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)),
],
),
)
After exporting for the web I noticed that the text is not selectable. Is it by chance a feature not supported for the web but only on mobile?
Thankyou
Web is still at alpha preview level. Check the flutter issues, and if you can't find it already marked as an issue, feel free. It's fairly easy to get noticed, but you might not like the priority it gets perhaps. :)

change the selected text color of the TextField flutter

is there any way to change the selected text color inside a TextField in Flutter?
I tried using the controller itself but no much properties to play around with..
Here's a sample of what I want
is there any way to achieve this behavior?
Use TextSpan widget, below a perfect recreation of your sample image
RichText(
text: TextSpan(
// set the default style for the children TextSpans
style: Theme.of(context).textTheme.body1.copyWith(fontSize: 30),
children: [
TextSpan(
text: 'Hello, ',
),
TextSpan(
text: 'Wor',
style: TextStyle(
color: Colors.blue
)
),
TextSpan(
text: 'ld',
),
]
)
)
Within a TextField this would already become difficult, because you do not know what the user enters.
If you are talking about a Text field, you can use RichText
RichText(
text: TextSpan(
// set the default style for the children TextSpans
style: Theme.of(context).textTheme.body1.copyWith(fontSize: 30),
children: [
TextSpan(
text: 'Styling ',
),
TextSpan(
text: 'text',
style: TextStyle(
color: Colors.blue
)
),
TextSpan(
text: ' in Flutter',
),
]
)
)
I think what you're looking for is a rich text editor or text field, I don't think flutter supports that out of the box. there are a few libs on pub best I found are zypher or creamy_field
zypher is being re-written on the pre-release might wanna watch out for that