How to select user type letters in Search Delegate? - dart

I developed search using search delegate. when user search I bold user type letters. But it's working only if user start to type with beginning otherwise bold letters wrong.
RichText(
text: TextSpan(
text: suggestList[index].d.substring(0, query.length),
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestList[index].d.substring(query.length),
style: TextStyle(color: Colors.grey))
])),

Related

text justify is not working correctly with RichText

I am using RichText and in this
text justify is not working correctly with RichText when using any of text span with style decoration: TextDecoration.underline
RichText(
softWrap: true,
textAlign: TextAlign.justify,
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
children: getSpan(),
),
)
List<TextSpan> getSpan() {
List<TextSpan> spans = List<TextSpan>();
spans.add(TextSpan(
text:
"And thus We made them rise ",
));
spans.add(
TextSpan(
text: " they ",
style: TextStyle(
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
print("click on me");
}),
);
spans.add(TextSpan(
text:
" And thus We made them rise ",
));
return spans;
}
It looks like a nasty effect of textAlign property when set to TextAlign.justify.
RichText(
softWrap: true,
//textAlign: TextAlign.justify,
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
children: getSpan(),
),
)
Updated to 21/03/19
The bug it is now fixed and at the moment (21/03/19) it is available in the master flutter channel:
flutter channel master

CupertinoSliverNavigationBar largeTitle multiline

I am creating a Flutter application which uses the Cupertino widgets. Now I am using the CupertinoSliverNavigationBar, but I did like the title to be multiline.
Currently I have written the following:
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(child: CustomScrollView(slivers: <Widget>[
CupertinoSliverNavigationBar(largeTitle: Text(
"Why doesn't this text wrap? I want it to be multiline...")),
SliverFillRemaining(child: Container())
]));
}
I tried several ways, like putting the Text in a Flexbile but this is not working. Any idea in how I can achieve the desired effect?
The height of the largeTitle is limited and I have found anything to make it bigger, so the solution that I propose require to make the text smaller.
largeTitle: RichText(
text: TextSpan(children: [
TextSpan(
text: "Why doesn't this text wrap?",
style: TextStyle(color: Colors.black, fontSize: 20)),
TextSpan(text: "\n"),
TextSpan(
text: "I want it to be multiline...",
style: TextStyle(color: Colors.orange, fontSize: 20))
]),
),
I think you should avoid using multiline largeTitle but in case you really want it you can try this.

How to make different style in same text widget?

How to apply different styles for different part of displayed text. How to do this in flutter?
Example:
Hello World
This can be achieved using RichText
RichText(
text: TextSpan(
text: 'Hello',
style: TextStyle(fontSize: 22, color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'World',
style: TextStyle(
fontSize: 24,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
),
),
]
),
),
return RichText(
text: TextSpan(
text: 'Can you ',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'find the',
style: TextStyle(
color: Colors.green,
decoration: TextDecoration.underline,
decorationStyle: TextDecorationStyle.wavy,
),
recognizer: _longPressRecognizer,
),
TextSpan(text: 'secret?'),
],
),
);

How to format text to be displayed in Flutter

I want to display following text on screen with superscripts being displayed properly. How to do that in flutter?
You can use RichText and Unicode
final defaultStyle = TextStyle(color: Colors.black);
final blueStyle = TextStyle(color: Colors.blue);
return Center(
child: RichText(
text: TextSpan(
text: "c. 10\u{207B}\u{2076} seconds: ",
style: defaultStyle,
children: [
TextSpan(text: "Hadron epoch ", style: blueStyle, children: [
TextSpan(
style: defaultStyle,
text:
"begins: As the universe cools to about 10\u{00B9}\u{2070} kelvin,",
),
])
]),
),
);
More info: https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts

Adding extra padding to TextSpan background in RichText widget

I have a RichText with some TextSpan. I want one of the TextSpan widgets to have a background on it so I used background: Paint()..color = Colors.redAccent as its text style.
Is there a way to add some extra red space/padding to the background.
This is how it currently looks:
This is how I want it to look (notice the extra red on the top and bottom of the highlighted text):
This is the code used:
Scaffold(
appBar: new AppBar(),
body: new RichText(
text: new TextSpan(
text: 'This is a one the lines in the span \n ',
style: TextStyle(fontSize: 15.0, color: Colors.black),
children: <TextSpan>[
new TextSpan(
text: 'This is the second line',
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(
text: ' background on me ',
style: new TextStyle(
fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent,
)),
new TextSpan(text: ' This is another of the lines in the span!'),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
)
I tried adding height to the text style, but it doesn't affect the height of the background.
The new solution in 2019 with flutter 1.7.8 is WidgetSpan, you could just add a Container and a Padding or any combination of widget to make more variety!
Here is an example to use on the case:
WidgetSpan(
child: Container(
color: Colors.red,
padding: EdgeInsets.all(8.0),
child: Text("background on me", style: ...),
)
)
And don't forget to change <TextSpan>[] to <InlineSpan>[], that's It!
Quite ugly solution, but I haven't found other (
TextStyle(fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent
..strokeWidth = 16.5
..style = PaintingStyle.stroke,)
strokeWidth have to be big enough to cover height of text. In my case 16.5 is minimal suitable value
you can use widgetSpan that inside RichText so that you can add any widget inside RichText.
RichText(
maxLines: 1,
overflow: TextOverflow.ellipsis,
text: TextSpan(
text: "text one", // text of exemple
children: [
// add a space between texts
WidgetSpan(
child: Padding(
padding: const EdgeInsets.only(left: 10.0),
),
),
TextSpan(
text: "text two", // text exemple
),
],
),
),

Resources