How to tap text span in Flutter driver? - flutter

How can I tap on the second Text Span area even if it has rich text property? I am new to flutter driver and I found out rich text not supported...
Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 24.0),
child: Text.rich(
TextSpan(
text: s.refill_description,
style: primaryTextTheme.textSmall,
children: [
if (user.paymentDetails?.type == LegalEntityType.private)
TextSpan(
text: s.withdraw_label,
style: primaryTextTheme.textSmall.copyWith(color: FlAppStyles.linkTextColor),
recognizer: TapGestureRecognizer()..onTap = () => showRefundScreen(context),
)

You can do it like this
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Single tap',
style: TextStyle(color: Colors.red[300]),
recognizer: TapGestureRecognizer()..onTap = () {
// Single tapped.
},
),
],
),
)

Related

How can I show PopupMenu under the Text?

I need to show the popup menu under the Text but it shows at top of the screen. How can I show it under the Text? I used showMenu() because I can't wrap the text with PopupMenuButton and I didn't find any alternative widget. My Code:
RichText(
text: TextSpan(
style: const TextStyle(
fontSize: 30.0,
color: Colors.black,
),
children: List.generate(
data.length,
(index) => TextSpan(
style: const TextStyle(),
children: [
TextSpan(
text: data[index].transcription + ' ',
style: TextStyle(color: Colors.yellow),
recognizer: TapGestureRecognizer()
..onTap = () async {
await showMenu(
position:
RelativeRect.fromLTRB(
100.0,
100.0,
100.0,
100.0),
context: context,
items: [
PopupMenuItem(
child: Text(data[index].id),
)
],
);
}),
TextSpan(
text: 'end',
)
],
)),
),
),

RichText widget has vertical padding built into the text

I have a RichText widget that displays a time like "5:21" where the minutes are in bold and the seconds are not.
I am using the widget inspector tool to observe the bounds of the text.
The region of the RichText widget has a large amount of padding above and below the text (like 10 dp top and bottom). On the horizontal, there is no pad at all, the region is snug to the edges of the font. How can I get the region to be snug to the top and bottom of the font when using RichText?
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: "$minutes:",
style: textStyle,
),
TextSpan(
text: "$seconds".padLeft(2, '0'),
style: textStyle.copyWith(fontWeight: FontWeight.w100),
),
],
),
),
Text(
"time",
),
],
);
try this
Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: "$minutes:",
style: textStyle,
),
TextSpan(
text: "$seconds".padLeft(2, '0'),
style: textStyle.copyWith(fontWeight: FontWeight.w100),
),
],
),
),
),
Text(
"time",
),

Flutter "RangeError (index): Invalid value: Valid value range is empty: 0 with WidgetSpan

I am trying to create the picture below in Flutter. As you can see, I need a link and an image icon. However, I am getting the "range" error in the console.
Here a snippet of my code
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.asset(
"assets/images/checkbox-check.png",
),
Expanded(
child: Container(
child: Wrap(
children: [
RichText(
text: new TextSpan(
children: [
TextSpan(
text: 'Email opportunities. ',
style: TextStyle(
color: Palette.DARK_BLUE, fontSize: 16.0),
),
TextSpan(
text: 'privacy policy',
style: TextStyle(
color: Palette.DARK_BLUE,
decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()
..onTap = () {
launch();
},
),
TextSpan(
text: ' and ',
),
TextSpan(
text: 'terms of use',
style: TextStyle(
decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()
..onTap = () {
launch();
},
),
WidgetSpan(
child: Icon(
Icons.info_outline,
color: Colors.red,
),
)
],
),
),
],
),
),
)
],
);
}
}
Is there another method to create my UI? I don't get an error screen but I see the error in the console. I have a wrap in the widget tree but don't think I need it. but never the less, same issue if I remove it.
This is a Flutter issue when you use TextSpan with recognizer together with WidgetSpan: https://github.com/flutter/flutter/issues/51936
A workaround is to wrap RichText in ExcludeSemantics:
ExcludeSemantics(
excluding: true,
child: RichText( ... ),
),

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

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