Display text above underline with padding - flutter

Is there a way to achieve the following results in Flutter where the underline below text will be nicely hidden by the padding around letters that overflow it, such as 'g', 'q', 'y', etc?
Emulator (current):
Design (desired):
Font: Spartan

The best way I found to achieve that was to use text stroke and Stack to create the underline effect. Here is the code:
Align(
alignment: Alignment.centerLeft,
child: Container(
padding: EdgeInsets.symmetric(vertical: 6.0),
child: Stack(
children: [
Positioned(
bottom: 2,
left: 0,
right: 0,
child: Container(height: 1.0, color: const Color(0xFF2F3543)),
),
Text(
"Forgot password?",
style: TextStyle(
fontWeight: FontWeight.w300,
inherit: true,
fontSize: 13.0,
foreground: Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = Colors.white,
),
),
Text(
"Forgot password?",
style: TextStyle(
color: const Color(0xFF2F3543),
fontWeight: FontWeight.w300,
inherit: true,
fontSize: 13.0,
),
)
],
),
),
)
And the screenshot of the results

EDIT
The first one looks working but not looks good and the second one working fine but p word not that good.
When you dont want to add underline just write decoration:TextDecoration.none.
Text.rich(
TextSpan(
text: 'For ',
style: TextStyle(decoration: TextDecoration.underline, fontSize: 25),
children: <TextSpan>[
TextSpan(text: 'g',style:TextStyle(decoration: TextDecoration.none)),
TextSpan( text: ' ',style:TextStyle(decoration: TextDecoration.underline, decorationThickness: 1)),
TextSpan( text: ' p',style:TextStyle(decoration: TextDecoration.none,)),
TextSpan( text: 'assword?',style:TextStyle(decoration: TextDecoration.underline)),
//you can add more text span here
],
),
),
Row(
children: <Widget>[
Text('For', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
Text('g', style: TextStyle(decoration: TextDecoration.none, fontSize: 25)),
Text('ot', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
Text(' ', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
Text('p', style: TextStyle(decoration: TextDecoration.none, fontSize: 25)),
Text('assword', style: TextStyle(decoration: TextDecoration.underline, fontSize: 25)),
],
)
Working example image

Related

Flutter google_fonts package doesn't match for roboto flex font

Flutter doesn't render robotoFlex fonts Font weights Example Bold, W600, W700, W800, W900,
I'm using google_fonts for my project, I don't know what's wrong. Its working fine with other fonts like GoogleFont.lato and other
font comparison
child: Text(
'GoogleFonts.lato',
style: GoogleFonts.lato(
textStyle: const TextStyle(
fontSize: 17,
)),
).pV(5).pH(20),
),
Center(
child: Text(
'This is GoogleF.lato',
style: GoogleFonts.lato(
textStyle: const TextStyle(
fontSize: 26,
fontWeight: FontWeight.w900)),
).pV(5).pH(20),
),
Center(
child: Text(
'This is GoogleF.lato',
style: GoogleFonts.lato(
textStyle: const TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold)),
).pV(1).pH(20),
),
const SizedBox(
height: 35,
),
Center(
child: Text(
'GoogleFonts.robotoflex',
style: GoogleFonts.robotoFlex(
textStyle: const TextStyle(
fontSize: 17,
)),
).pV(5).pH(20),
),
Center(
child: Text(
'This is GoogleF.RobotoFlex',
style: GoogleFonts.robotoFlex(
textStyle: const TextStyle(
fontSize: 26,
fontWeight: FontWeight.w900)),
).pV(1).pH(20),
),
Center(
child: Text(
'This is GoogleF.RobotoFlex',
style: GoogleFonts.robotoFlex(
textStyle: const TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold)),
).pV(1).pH(20),
),
outPut Here you can see the difference google.lato font renders the font weight but roboto doesn't

how to make a row responsive in flutter?

Hello i'm try to write this text " choose a one to modify the number " and the word "to modify " is with a different style text . my problem here is that the text is not responsive that mean there is not a return to next line if the phone has a small screen
how can i fix it and make this text in the center ?
Row(
children: const [
Text(
"Choose a one",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
Text(
"to modify",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFFFF9E4F),
),
),
Text(
"the number",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
],
)
You can try using rich text, it will be like the following
RichText(
text: TextSpan(
text: "Choose a one",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
children: const <TextSpan>[
TextSpan(text: 'to modify',
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFFFF9E4F),
),
TextSpan(text: ' the number',
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
],
),
)

make space between TextSpan

I am trying to make space between TextSpan
What is the best way to add space between TextSpan?
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
SizedBox widget can be used in between two widgets to add space between two widgets.
use the SizedBox by wrapping it with a WidgetSpan widgit
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
WidgetSpan(
child: SizedBox(width: 10),
),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
If you like to get maximum space between text, you might go for Row widget. For x amount of space inside RichText you can use
TextSpan(...),
WidgetSpan(child: SizedBox(width: x)),
TextSpan(...),
RichText(
text: const TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
WidgetSpan(child: SizedBox(width: 10)), ///this
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
You can use the height property on the TextStyle to create some spacing:
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
height: 1.5, //USE THIS PROPERTY
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
SizedBox(height: 10),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
More info in the docs: https://api.flutter.dev/flutter/painting/TextStyle/height.html

WidgetSpan is not proper align in Flutter

I want to set a black heart between "m" and "ms". and also I want to use "TextAlign.justify". if I use "TextAlign.center" then it's working fine. but if I use "TextAlign.justify" so the black heart is not set in the correct place. If I remove the "WidgetSpan" widget and add "TextSpan" for a black heart so also it's working. but I want to add bottom padding.
Please guys help me.
Thanks
Text.rich(
TextSpan(
children: <InlineSpan>[
TextSpan(
text: MyString.txtAboutUs ,
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_regular)),
TextSpan(
text: " m",
children: <InlineSpan>[
WidgetSpan(
baseline: TextBaseline.ideographic,
alignment: PlaceholderAlignment.top,
child: Container(
padding: EdgeInsets.only(bottom: 3),
child: Text("🖤", style: TextStyle(
fontSize: FontSize.size8,
fontFamily: MyFont.poppins_regular) ),
)
),
TextSpan(
text: "ms ",
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_medium,
color: Colors.black,
fontWeight: FontWeight.bold)),
],
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_medium,
color: Colors.black,
fontWeight: FontWeight.bold)),
TextSpan(
text: MyString.txtAboutImmediately,
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_regular)),
],
),
textAlign: TextAlign.justify,
),
Try to below simple Text Widget
Text('you add 🖤 here'),
// OR
Text('you add \u{2764} here} '),
Updated Answer using RichText
Center(
child: Text.rich(
TextSpan(
children: <TextSpan>[
TextSpan(
text: 'You add ',
style: TextStyle(
fontSize: 10.0,
color: Colors.black,
),
),
TextSpan(
text: '🖤',
),
TextSpan(
text: 'Here',
),
],
),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 10.0,),
),
),
Screen like
replace padding with transform.
WidgetSpan(
baseline: TextBaseline.ideographic,
alignment: PlaceholderAlignment.top,
child: Container(
transform: Matrix4.translationValues(0, -3, 0),
margin: EdgeInsets.only(bottom: 8),
child: Text(
"🖤",
style: TextStyle(
// fontSize: FontSize.size8,
// fontFamily: MyFont.poppins_regular,
),
),
),
),

How I can make outlined text?

How I can make outlined text for Text widget? It's like stroke effect
You could try this option: https://stackoverflow.com/a/53211151/1534666
Copy of answer:
I was also looking for this, wasn't able to find it. But I did find a workaround using 4 shadows in the TextStyle:
Text("Border test",
style: TextStyle(
inherit: true,
fontSize: 48.0,
color: Colors.pink,
shadows: [
Shadow( // bottomLeft
offset: Offset(-1.5, -1.5),
color: Colors.white
),
Shadow( // bottomRight
offset: Offset(1.5, -1.5),
color: Colors.white
),
Shadow( // topRight
offset: Offset(1.5, 1.5),
color: Colors.white
),
Shadow( // topLeft
offset: Offset(-1.5, 1.5),
color: Colors.white
),
]
),
);
I also opened an Issue on GitHub: https://github.com/flutter/flutter/issues/24108
UPD:
I wouldn't call it the best solution, but it works
Stack(
children: <Widget>[
Text(
'TEXT',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 36.0),
),Text(
'TEXT',
textAlign: TextAlign.center,
style: TextStyle(
foreground: Paint()..color = Colors.white..style = PaintingStyle.stroke..strokeWidth = 1.0,
fontWeight: FontWeight.bold,
fontSize: 36.0),
),
],
);
In TextStyle you can't define both color and foreground, so I've tried to use Stack with two identical Texts with different decorations.
As I said - not the best solution, but maybe it'll be suitable for someone
style: GoogleFonts.londrinaOutline(),
This is simple but not perfect....!
Use bordered_text package
BorderedText(
strokeColor: Colors.white,
strokeWidth: 6,
child: Text(
'Bordered Text',
style: const TextStyle(
fontSize: 130,
fontWeight: FontWeight.bold,
color: blackColor
),
),
),