Change text style based on search bar text - flutter

I would like to change the style of the text according to what I enter in the search bar, that is, search if it matches and change dynamically. Something like that.
Example image

Use RichText to specify different styles for each text segment:
RichText(text: TextSpan(children: [
TextSpan(text: 'Hello', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
]));

Related

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.

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

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

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