Is there a way to make TextSpan.text selectable in flutter? - 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!'),
],
),
)

Related

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,
),
),
],
),
),
)

Multiple Text Styles in TextField in Flutter

Is there a way to take input in multiple text styles in a textfield in Flutter like this?
Where the user can select a part of the text and edit its style like he wants
You can try use RichText
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
Source: Source on api.flutter.dev
You can use this https://pub.dev/packages/flutter_social_textfield. You can add different styles too.

Flutter RichText is not showing

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!'),
],
),
),
));

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

Add image to RichText element

I am trying to achieve following:
To achieve this I am using RichText. Using this I can add multiple text with different styles. But I am not getting idea how I can add image to this TextSpan. May be there is some other element.
You do not need third-party libraries to achieve this. With the latest stable release, Flutter has introduced WidgetSpan. This is similar to TextSpan but you can use any widget instead of just text. Sample code:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
),