text justify is not working correctly with RichText - dart

I am using RichText and in this
text justify is not working correctly with RichText when using any of text span with style decoration: TextDecoration.underline
RichText(
softWrap: true,
textAlign: TextAlign.justify,
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
children: getSpan(),
),
)
List<TextSpan> getSpan() {
List<TextSpan> spans = List<TextSpan>();
spans.add(TextSpan(
text:
"And thus We made them rise ",
));
spans.add(
TextSpan(
text: " they ",
style: TextStyle(
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
print("click on me");
}),
);
spans.add(TextSpan(
text:
" And thus We made them rise ",
));
return spans;
}

It looks like a nasty effect of textAlign property when set to TextAlign.justify.
RichText(
softWrap: true,
//textAlign: TextAlign.justify,
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
children: getSpan(),
),
)
Updated to 21/03/19
The bug it is now fixed and at the moment (21/03/19) it is available in the master flutter channel:
flutter channel master

Related

Changing Text Color onTap, Flutter

I am an android developer new to flutter and i am trying to create 3 grey text widgets where when a user clicks one, the clicked one becomes blue while the others remain grey. This is the code i currently have
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const <Widget>[
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
],
),
Also whatever i put in onTap() i get the error that says
invalid constant value
You should create three separate variables for each button's color and then remove the const keyword in Row,
Color firstColor = Colors.indigo;
Color secondColor = Colors.indigo;
Color thirdColor = Colors.indigo;
...
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[ /// remove const keyword
InkWell(
child: Text('Click 3',
style: TextStyle(
color: firstColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
firstColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: secondColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
secondColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: thirdButton,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
thirdColor=Colors.blue;
});
},
),
],
),

how to detect otp text in textfield flutter

How can I make auto detect otp text of my textfield in flutter
I am not getting anything appropriate.
Thanks in andvance.
Center(
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child:TextField(
controller: otpController,
maxLength: 6,
maxLengthEnforced: true,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "_ _ _ _ _ _",
hintStyle: new TextStyle(
fontWeight: FontWeight.bold
)
// border:
// OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))
),
style: new TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 22.0,
),
),
),
),

How to make different style in same text widget?

How to apply different styles for different part of displayed text. How to do this in flutter?
Example:
Hello World
This can be achieved using RichText
RichText(
text: TextSpan(
text: 'Hello',
style: TextStyle(fontSize: 22, color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'World',
style: TextStyle(
fontSize: 24,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
),
),
]
),
),
return RichText(
text: TextSpan(
text: 'Can you ',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'find the',
style: TextStyle(
color: Colors.green,
decoration: TextDecoration.underline,
decorationStyle: TextDecorationStyle.wavy,
),
recognizer: _longPressRecognizer,
),
TextSpan(text: 'secret?'),
],
),
);

Adding extra padding to TextSpan background in RichText widget

I have a RichText with some TextSpan. I want one of the TextSpan widgets to have a background on it so I used background: Paint()..color = Colors.redAccent as its text style.
Is there a way to add some extra red space/padding to the background.
This is how it currently looks:
This is how I want it to look (notice the extra red on the top and bottom of the highlighted text):
This is the code used:
Scaffold(
appBar: new AppBar(),
body: new RichText(
text: new TextSpan(
text: 'This is a one the lines in the span \n ',
style: TextStyle(fontSize: 15.0, color: Colors.black),
children: <TextSpan>[
new TextSpan(
text: 'This is the second line',
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(
text: ' background on me ',
style: new TextStyle(
fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent,
)),
new TextSpan(text: ' This is another of the lines in the span!'),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
)
I tried adding height to the text style, but it doesn't affect the height of the background.
The new solution in 2019 with flutter 1.7.8 is WidgetSpan, you could just add a Container and a Padding or any combination of widget to make more variety!
Here is an example to use on the case:
WidgetSpan(
child: Container(
color: Colors.red,
padding: EdgeInsets.all(8.0),
child: Text("background on me", style: ...),
)
)
And don't forget to change <TextSpan>[] to <InlineSpan>[], that's It!
Quite ugly solution, but I haven't found other (
TextStyle(fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent
..strokeWidth = 16.5
..style = PaintingStyle.stroke,)
strokeWidth have to be big enough to cover height of text. In my case 16.5 is minimal suitable value
you can use widgetSpan that inside RichText so that you can add any widget inside RichText.
RichText(
maxLines: 1,
overflow: TextOverflow.ellipsis,
text: TextSpan(
text: "text one", // text of exemple
children: [
// add a space between texts
WidgetSpan(
child: Padding(
padding: const EdgeInsets.only(left: 10.0),
),
),
TextSpan(
text: "text two", // text exemple
),
],
),
),

How to give onTap functionality to a container which is wrapped in a Listview

I have a list of containers wrapped in a listview, I need to give a onTap functionality like when the user taps on the container it should give me a value of that particular container, below is my code and I tried using Inkwell function but it throws an error saying vertical portview. Help me out and thanks in advance.
dynamic _allVehiclesDataView = new ListView(
children: List.generate(this._allVehiclesData.length,
(i) => new Container(
decoration: new BoxDecoration(
border: Border(
bottom: BorderSide(width: 1.0, color: Colors.grey[300]),
),
),
padding: const EdgeInsets.all(32.0),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.only(bottom:5.0),
child: Text(
this._allVehiclesData[i]["vehicle"]["registration"],
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
Text(
'Location :',
style: TextStyle(
fontSize: 15.0,
color: Colors.grey,
),
),
Text(
(this._allVehiclesData[i]["address"] == null ||
this._allVehiclesData[i]["address"] == 'Not determined') ?
"No data available" : ""+this._allVehiclesData[i]["address"]["LongLabel"],
style: TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Last known update :',
style: TextStyle(
fontSize: 15.0,
color: Colors.grey[500],
),
),
Text(
(this._allVehiclesData[i]["deviceTime"]),
style: TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
),
),
this._allVehiclesData[i]["fuel"] != "null"?
new Row(
children: <Widget>[
Text(
'Fuel Level :',
style: TextStyle(
fontSize: 15.0,
color: Colors.grey[500],
),
),
new Container(
decoration: new BoxDecoration(
shape : BoxShape.circle,
border: new Border.all(
color: Colors.blue[300],
width: 5.0,
)
),
),
Text(
(this._allVehiclesData[i]["fuel"].toString()),
style: TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
),
),
],
) : Text(''),
this._allVehiclesData[i]["temperature"] != "null" ?
new Row(
children: <Widget>[
Text(
'Temp :',
style: TextStyle(
fontSize: 15.0,
color: Colors.grey[500],
),
),
new Container(
decoration: new BoxDecoration(
shape : BoxShape.circle,
border: new Border.all(
color: Colors.red[300],
width: 5.0,
)
),
),
Text(
(' '+this._allVehiclesData[i]["temperature"].toString()+' C'),
style: TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.bold,
),
),
],
):Text(""),
],
),
),
status(i),
// new InkWell(),
],
),
),
),
);
You can use the GestureDetector for the same
Wrap your Container in a GestureDetector with an onTap callback
GestureDetector(
// When the child is tapped, do your work
onTap: () {
...
...
},
// Container
child: Container(
...
),
);

Resources