TextSpan avoid line break on symbol String group - flutter

I have a RichText() widget with a TextSpan containing a String of symbols such as €€€€€€€
this group of symbol is not considered as one word even thought there is no spacing
-> I'd like to avoid a line break to happen between those symbols have to stay together
I can't set softWrap: false on RichText as I need a line break somewhere to avoid overflow
RichText(
text: TextSpan(
text: 'Random text hello there Random long text',
children: <TextSpan> [
const TextSpan(text: ' · '),
TextSpan(text: '€€€€€€€'),
],
),
),
can't I set softWrap: false directly on TextSpan or something similar ?

Before:
After:
Use WidgetSpan with Text instead of TextSpan.
RichText(
text: TextSpan(
text: 'Random text hello there Random',
children: [
const TextSpan(text: ' · '),
WidgetSpan(
child: Text('€€€€€€€'),
),
],
),
)

You can set TextOverflow.visible for the respective TextSpan, it will force the word to be visible as one word, basically the flutter version of softWrap: false but a possible problem can be that it will exceed the available width.
To do that, check this code:
RichText(
text: TextSpan(
text: 'Random text hello there Random long text',
children: <TextSpan> [
const TextSpan(text: ' · '),
TextSpan(
text: '€€€€€€€',
style: TextStyle(
overflow: TextOverflow.visible,
)
),
],
),
),
Let's see if it works.

Does an InlineSpan help you?
or adding an "overflow" property to its style: https://api.flutter.dev/flutter/painting/TextOverflow.html like
RichText(
text: TextSpan(
text: 'Random text hello there Random long text',
children: <TextSpan> [
const TextSpan(text: ' · '),
TextSpan(
text: '€€€€€€',
style: TextStyle(
overflow: 'visible',
)
)
]
)
?

add :- softWrap: true,
style: TextStyle(color: Colors.red),
RichText(
softWrap: true,
text: const TextSpan(
text: 'Random text hello there Random long text ',
style: TextStyle(color: Colors.red),
children: <TextSpan>[
TextSpan(
text: 'hello there Random long',
style: TextStyle(color: Colors.red)),
TextSpan(
text: '€€€€€€€', style: TextStyle(color: Colors.red)),
],
),
),

Related

How to change colors of text letters in flutter

I want to change the color of letters in a text eg. I want to change the color of letter H to orange from text Hello and let the ello as it is in flutter. So how to change it.
If its static you can use RichText
example
RichText(
text: TextSpan(
text: 'H',
style: TextStyle(color:Colors.orange),
children: const <TextSpan>[
TextSpan(text: 'ello', style: TextStyle()),
],
),
)
If its dynamic you might want to use plugins eg simple_rich_text
SimpleRichText(r'*_/this is all three*_/ (*{color:red}bold*, _{color:green}underlined_, and /{color:brown}italicized/). _{push:home;color:blue}clickable hyperlink to home screen_')
Row(
children: [
Text(
'H',
style: TextStyle(color: Colors.orange),
),
Text('ello')
],
),
Row(
children: [
Text(
'H',
style: TextStyle(color: Colors.orange),
),
Text('ELLO',style: TextStyle(color: Colors.black),],
),
RichText(
text: TextSpan(
text: 'H',
style: TextStyle(color:Colors.orange),
children: const [
TextSpan(text: 'ello', style: TextStyle()),
],
),
)
you can use https://api.flutter.dev/flutter/widgets/RichText-class.html

is there a functiojn to break a text and keep it in the next line inside a container in flutter?

Container(
height: 100,
width: 320,
child: Text(
domaindesc,
softWrap: true,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.roboto(textStyle: ktextstyle),
),
),
If you mean creating a new line in the same text paragraph? add this '\n' where you want a new line to start.
Also if you want to change the fontstyle for different parts of the text (e.g. on a new line, for example) you can use TextSpan. Example borrowed from the Flutter docs with '\n' new line inserted to illustrate:
return Container(
child: RichText(
text: TextSpan(
text: 'Hello \n ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
));

How can i make a paragraph when writing a long text in flutter?

How can i make paragraphs in flutter in a text widget?
It is all just one long text and it looks stupid
What I want:
Hello,
how are you?
What it is:
Hello, how are you?
Use \n for paragraphs, like this: Text('Hello,\n\nhow are you?')
another approach, use RichText:
RichText(
text: TextSpan(
text: 'Hello,',
style: TextStyle(color: Colors.black, fontSize: 18.0),
children: <TextSpan>[
TextSpan(
text: 'how are you?',
style: TextStyle(fontWeight: FontWeight.bold, )
),
],
),
)

How to produce Flutter Text Field that underline incorrect words with suggestion provided?

A picture is worth a thousand words, let me show you what I tried to achieve:
The solution could be using Flutter built-in widget or third-party widget.
Anyone?
You could use the Text.rich() method (or the RichText Widget) to achieve what you want.
The following snippet corresponds to your screenshot.
return Scaffold(
body: Center(
child: Text.rich(
TextSpan(
text: 'I lost ',
style: TextStyle(fontSize: 24),
children: <TextSpan>[
TextSpan(
text: 'Password.',
style: TextStyle(
decoration: TextDecoration.underline,
)),
TextSpan(
text: ' ',),
TextSpan(
text: 'What do?',
style: TextStyle(
decoration: TextDecoration.underline,
)),
// can add more TextSpans here...
],
),
),
),
);

How to make a line through text in Flutter?

Using the TextStyle() class in Flutter, how can I strike through an old price?
To apply strikethrough decoration to a Text widget directly:
Text('\$8.99', style: TextStyle(decoration: TextDecoration.lineThrough))
You can also style separate spans of a paragraph by using the RichText widget, or the Text.rich() constructor.
Based on this example code, to show a discounted price:
RichText()
new RichText(
text: new TextSpan(
text: 'This item costs ',
children: <TextSpan>[
new TextSpan(
text: '\$8.99',
style: new TextStyle(
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
new TextSpan(
text: ' \$3.99',
),
],
),
)
Text.rich()
Text.rich(TextSpan(
text: 'This item costs ',
children: <TextSpan>[
new TextSpan(
text: '\$8.99',
style: new TextStyle(
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
new TextSpan(
text: ' \$3.99',
),
],
),
)
style: TextStyle(decoration: TextDecoration.lineThrough),
i use this
Column(
children: [
Text(
"sample text"
),
Divider(color: Colors.red,)
],
),