WidgetSpan is not proper align in Flutter - 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,
),
),
),
),

Related

fill the spaces of a paragraph in flutter

i'm stuck trying to design a text widget that have empty Text Field like below:
If you have an idea how to do this, I would be grateful
You can use rich text to achieve this
RichText(
text: TextSpan(
children: [
TextSpan(text: "some really big text which will have a textfield like this"),
WidgetSpan(child: Container(
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.orange
),
child:TextField(
style:TextStyle(fontSize:12))
)),
TextSpan(text:" to enter some text in between a text"),
]
)
)
You may have to align items. But this should work
Center(
child: RichText(
text: TextSpan(
text:
'Grout crack happens mainly due to movement between two surfaces. It can be as a ',
style: TextStyle(
color: Colors.black,
fontSize: 40,
fontWeight: FontWeight.bold),
children: [
TextSpan(
text: 'thin',
style: TextStyle(
color: Colors.red,
fontSize: 35,
fontStyle: FontStyle.italic)),
TextSpan(
text: ' of humidity',
style: TextStyle(color: Colors.black, fontSize: 40)),
]),
),
),

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

How to get some space between in Richtext?

Im trying getting some space in Richttext. First heres my code
title: RichText(
text: TextSpan(
text: _snapshot.data['username'], // _snapshot.data['username']
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
children: <TextSpan>[
TextSpan(
//"${comment.data()['comment']}"
text: "${comment.data()['comment']}",
style: TextStyle(
fontWeight: FontWeight.normal,
),
)
],
),
)
And what I want is some space between the this
text: TextSpan(
text: _snapshot.data['username'], // _snapshot.data['username']
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
)
And this
children: <TextSpan>[
TextSpan(
//"${comment.data()['comment']}"
text: "${comment.data()['comment']}",
style: TextStyle(
fontWeight: FontWeight.normal,
),
],
),
Hope anyone can help. If you need a picture how it looks at the moment please leave a comment
You can use a SizedBox like below :
RichText(
text: TextSpan(
text: "Test:",
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
children: <InlineSpan>[
WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
child: SizedBox(width: 10)),
TextSpan(
text: "Data",
style: TextStyle(
fontWeight: FontWeight.normal,
),
)
],
),
)

Display text above underline with padding

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

flutter richtext separated align

i see this code for rich text in flutter:
return Row(
children: <Widget>[
?
Container(
child: RichText(
text: TextSpan(
text: "Hello",
style: TextStyle(fontSize: 20.0,color: Colors.black),
children: <TextSpan>[
TextSpan(text:"Bold"),
style: TextStyle( fontSize: 10.0, color: Colors.grey),
),
],
),
),
)
this print
Hellobold
but i need to separe both text one aling to left other to right, like this
Hello bold
how i do this?
thanks
I don't know your exact Use Case - But One way of getting what you require:
Row(
children: <Widget>[
Expanded(
child: RichText(
text: TextSpan(children: [
TextSpan(text: 'Singh', style: TextStyle(fontSize: 22.0,color: Colors.grey))
])),
),
RichText(
text: TextSpan(children: [
TextSpan(text: 'Kaur', style: TextStyle(fontSize: 28.0,color: Colors.redAccent))
])),
],
),
if you want to use textalign in RichText use WidgetSpan and Text widget
WidgetSpan(
child: Text(
'your text',
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
),
),
I think this will can help you:
RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Padding(
padding: const EdgeInsets.fromLTRB(1, 10, 10, 0),
child: Icon(
Icons.developer_mode_sharp,
),
),
),
TextSpan(
text: ' Tinkle is Powered By:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black),
),
],
),
),
Text(
'Together Marketing Agency',
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 16, color: Colors.black, fontWeight: FontWeight.w500),
),
To Align your Text when using RichText widget, Simply add textAlign: TextAlign() to your RichText Property.
return Row(
children: <Widget>[
?
Container(
child: RichText(
textAlign: TextAlign.justify //You can change it to any aligment style that you want.
text: TextSpan(
text: "Hello",
style: TextStyle(fontSize: 20.0,color: Colors.black),
children: <TextSpan>[
TextSpan(text:"Bold"),
style: TextStyle( fontSize: 10.0, color: Colors.grey),
),
],
),
),
)
Container(
width: double.infinity,
child: Text.rich(
TextSpan(
text: 'This is a ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
// Handle the tap event
},
),
TextSpan(text: ' and normal text'),
],
),
textAlign: TextAlign.center,
),
)