RichText widget has vertical padding built into the text - flutter

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

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

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 add iconbutton at the end of the text in Flutter?

How can I add the iconbutton at the end of the text like this ?
Pic
I use Row widget but the iconbutton is not in the right place, it is not at the end of the text
Pic
This is my code:
Row(
children: [
Expanded(
child: Text(
'All about marketing, lead generation, content marketing and growth hacks',
style: const TextStyle(
fontSize: 12.0, fontWeight: FontWeight.w500),
),
),
IconButton(
icon: Icon(Icons.info),
iconSize: 12.0,
color: Palette.appbar,
onPressed: () {},
),
],
),
Use RichText with TextSpan and WidgetSpan.
WidgetSpan allow to add your own widget as a part of text.
like this :
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: _text, style: _style),
WidgetSpan(
child: _child
)
],
),

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

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