Flutter - Text with space breaks background color - flutter

Text with whitespace and background color does not apply the background color for the whitespace part.
Please see the following screenshot:
I would expect the whitespace to be colorized with a red background as well, instead, it increases the width of the parent container without applying the background color.
Here is a live playground link
Question:
Is there a way to fix this?

Seems like all spaces on right has been removed for nth(i don't know yet) reason, Currently I am able to solve with RichText with adding extra transparent char.
Container(
color: Colors.blue,
child: Text.rich(
TextSpan(
text: "hi ",
style: TextStyle(
backgroundColor: Colors.red,
fontSize: 250.0,
),
children: [
TextSpan(text: "a", style: TextStyle(color: Colors.transparent))
],
),
)),
It takes extra single space,

One thing You can do is set text background color to transparent and give the main color to container.

Related

Flutter: RichText with darktheme

I use some RichText throughout my application to switch colors between text and display widgets in between.
E.g.
RichText(
text: TextSpan(text: 'text1', style: TextStyle(color: Colors.black),
children: [
WidgetSpan(alignment: PlaceholderAlignment.middle, child: SomeWidget()),
TextSpan(text: 'text2', style: TextStyle(color: Colors.blue),
WidgetSpan(alignment: PlaceholderAlignment.middle, child: Icon(Icons.someIcon)),
TextSpan(text: 'text3'),
]
));
The problem now is that when switching to dark mode, the black test is contrasted to a black background and thus invisible. The regular Text('some text') widget will automatically display black by default but will become white in dark mode. How can I achieve the same or something similar when using RichtText?
Well you defined your RichText like this:
TextSpan(text: 'text1', style: TextStyle(color: Colors.black)
As you know the color argument that you pass to a Widget has the highest priority, therefore, your TextSpan will always be black.
One solution is instead of hardcoding the color you can do this:
Theme.of(context).textTheme.bodyText2?.color
If we look at the definition of this theme it looks like this:
/// The default text style for [Material].
TextStyle? get bodyText2 => bodyMedium;
If you use this approach your TextSpan and normal Text widgets should have the same color. In general one should always prefer working with themes over hardcoding colors. If your user switches to dark mode the theme will switch too and therefore, all styles should be switched correctly as well.

Text underline display line above text instead of below

As showing the given image After applying underline decoration text style to the text it shows line above text instead of below in flutter 2.2.
TextButton(
onPressed: () {},
child: Text(
getLocalValue(context, txtTermsNConditions),
style: TextStyle(
fontSize: 12.sp,
fontWeight: fwMedium,
color: Color(clrPrimary),
decoration: TextDecoration.underline),
),
)
style: TextStyle(decoration: TextDecoration.overline) will do the trick see this link for more information.
You can do with a shortcut just use a Stack and place both Text and a Divider Widget in it and gave then position according to your Requirements that's all.
That was a problem with fonts I have used, using another font it has been resolved.

how to make the text go to the next line in flutter

I am trying to build a cv app that i can present my information. but when i write a long text it can not go to next line auto and i tried other solution but it does not work. here the code
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Flexible(
child: Text(
'longgggggggggggggggggggggggggggggggggggggg text',
overflow: TextOverflow.ellipsis,
softWrap: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'NotoSans',
fontSize: 15,
),
),
),
),
here is the image
here is the image
Simply use \n inside the text.
You have to usesoftWrap: true, instead of softWrap: false, this will make the text go to the next line whenthere is no more space.
Just make sure the container where the text is, lets it go to the next line, if the height of the container is fixed it'll get the overflow error but on the vertical axis
Another simple way to do it is make a container and customize it a bit like you want for example you want border or not its up-to you. Add padding to it so the text inside the container will adjust itself automatically it wont go out of the container.

how to add icon into text flutter?

i have a simple code
RaisedButton(
onPressed: () {
model.resetmodem();
},
child: Text(
'Size (also 24 pixels, not coincidentally, since both FlutterLogo and Icon honor the ambient IconTheme). This leaves some room left over, and now the row tells the text exactly how wide to be: the exact width of the remaining space. The text, now happy to comply to a reasonable request, wraps the text ',
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
color: Colors.blue,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)),
),
I want to add an icon in front of the text but I don't want to overflow the text
Use Row and put this text and icon inside it if it overflows you got to lower font sized or icon size or make your button bigger
You could you RichText along with InkWell like:
InkWell(
onTap: () {
//Your code here
},
child: RichText(
text: TextSpan(
text: 'Size (also 24 pixels, not coincidentally, since both FlutterLogo and Icon honor the ambient IconTheme). This leaves some room left over, and now the row tells the text exactly how wide to be: the exact width of the remaining space. The text, now happy to comply to a reasonable request, wraps the text ',
children: [
WidgetSpan(child: Icon(Icons.done)),
]),
),
)

Flutter RichText is not showing

I have a regular normal rich text widget in a Centered per https://api.flutter.dev/flutter/widgets/RichText-class.html within a Scaffold. I can't see it. I can't see anything. The debug tools show that the Rich Text is indeed there in the widget heirarchy (though I can't see the TextSpans within it, presumably they're in there.)
Scaffold(body: Center(child:
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
),
));
I tried an android build to see whether this was a bug specific to linux desktop builds. It wasn't! It's happening here too. However, the contrast ratio of my phone is high enough that I can just barely see the text in there, White on Off White. It turns out that, unlike the Text widget, the default color for RichText text is white, same as the theme's background, so the text wont be visible.
(Is it possible the app is responding to the system-wide dark mode theme? Nope. Switching theme doesn't affect anything.)
So, WHY is this the default color? Because DefaultTextStyle is bad and wrong I guess? Use Theme.of(context).textTheme.bodyText1 and you will get the results you expect.
RichText is a basic, lower-level approach to rendering text and intentionally does not use theme styles.
However, Text.rich does:
Scaffold(body: Center(
child: Text.rich(
TextSpan(
text: 'Hello ',
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
),
));