How to make a line through text in Flutter? - 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,)
],
),

Related

TextSpan avoid line break on symbol String group

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

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

How to center title in flutter obx

I am using get's obx() like below code.
But I want the title to be centered.
But I can't even use "align", so I'm curious how to align it.
title:
Obx(
() => Text.rich(
TextSpan(
text:
"${_questionController.questionNumber.value}. ",
style: TextStyle(fontSize: 15, color: Colors.blue),
children: [
TextSpan(
text: '${Questiontitle[widget.index]}',
style: TextStyle(fontSize: 15, color: Colors.blue),
),
],
),
),
),
Wrap title with Center or with Align and set alignment to Alignment.center.

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 show Icon in Text widget in flutter?

I want to show an icon in text widget. How do I do this ?
The following code only shows the IconData
Text("Click ${Icons.add} to add");
Flutter has WidgetSpan() to add a widget inside the RichText().
Example use:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
)
Above code will produce:
You can treat the child of WidgetSpan like the usual widget.
Screenshot:
Using Wrap:
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Row:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Text.rich:
Text.rich(
TextSpan(
children: [
TextSpan(text: 'Click'),
WidgetSpan(child: Icon(Icons.add)),
TextSpan(text: 'to add'),
],
),
)
An alternative might be to use emoji.
In Dart, strings supports escape sequences for special characters. For unicode emoji, you can use \u and curly braces with emoji hex code inside. Like this:
Text('Click \u{2795} to add')
The result is:
You can find a complete unicode emoji list here: http://unicode.org/Public/emoji/1.0/emoji-data.txt
Row Widget can be one solution for this issue, but you have to use different widgets to make it happen.
Follow below example.
Row(
children: <Widget>[
Text("Hi"),
Icon(Icons.add),
Text("Hello")
]
)
Another option is String.fromCharCode(int charCode)
This is icons.dart created by flutter team and charCodes are can found here.
RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
text: 'This is alarm icon : ',
children: <TextSpan>[
TextSpan(
text: String.fromCharCode(0xe190), //<-- charCode
style: TextStyle(
fontFamily: 'MaterialIcons', //<-- fontFamily
fontSize: 24.0,
color: Colors.red,
),
)
],
),
)
And result :
I think it's better to use the Row widget. here is an example
Row(
children: [
Icon(Icons.download_rounded),
Text(" Download")
],
)
You can use WidgetSpan() on Text.rich().
Here's a link for InlineSpan-class.
You can use this Package to achieve this.
I am not able to find the pub of this package.
Here is the Implementation.
RealRichText(
[
TextSpan(
text: "A Text Link",
style: TextStyle(color: Colors.red, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked.");
},
),
ImageSpan(
AssetImage("packages/real_rich_text/images/emoji_9.png"),
imageWidth: 24,
imageHeight: 24,
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 10)),
TextSpan(
text: "哈哈哈",
style: TextStyle(color: Colors.yellow, fontSize: 14),
),
TextSpan(
text: "#Somebody",
style: TextStyle(
color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: " #RealRichText# ",
style: TextStyle(color: Colors.blue, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: "showing a bigger image",
style: TextStyle(color: Colors.black, fontSize: 14),
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 5)),
TextSpan(
text: "and seems working perfect……",
style: TextStyle(color: Colors.black, fontSize: 14),
),
],
);
You can also check out below issue for more:
Flutter Issue #2022
For simple icons, You can use a Unicode character as regular text, just copy and paste.
You can search and copy Unicodes from this site
Text("Enjoy! 🔎❌💖🔥✅💜😍")
This method will work with any Text widget and package.
You can use this function:
Widget _spanIcon(String text, IconData icon, TextStyle st) {
return Row(
children: [
Text(text,style: st,),
Text(
String.fromCharCode(icon.codePoint), //<-- charCode from icon data
style: TextStyle(
fontFamily: icon.fontFamily, //<-- fontFamily from icon data
fontSize: st.fontSize! + 4.0,//you can increase font size for icon here
color: st.color,
),)
],
);}
and then use it :
_spanIcon('11',Icons.access_alarm, TextStyle(fontSize: 14.0, color: Color.fromARGB(113, 0, 0, 0))),