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?'),
],
),
);
Related
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;
});
},
),
],
),
I want to use Appbar with text "Back" button I a using below code but
"Back" is coming in two line like below, also Appbar title is moving down side.
Ba
ck
Flutter code for same
final topAppBar = AppBar(
// elevation: 0.1,
backgroundColor: Color.fromRGBO(0, 113, 188, 1.0),
title: Text(
"MyAppBar",
style: TextStyle(
color: Colors.white,
fontFamily: 'Raleway-ExtraBold',
fontWeight: FontWeight.w900,
fontSize: 20.0,
),
),
leading: Padding(
padding: const EdgeInsets.only(left: 0),
child: FlatButton(
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
),
);
Is there any thing which I am missing here??
Use - FittedBox - fit: property to adjust leading widget.
leading: FittedBox(
fit: BoxFit.cover,
child: FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // add this to remove padding.
onPressed: () {},
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
),
Use leadingWidth with enough width.
leadingWidth: 80,
leading: Padding(
padding: const EdgeInsets.only(left: 0),
child: FlatButton(
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
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
I want to display following text on screen with superscripts being displayed properly. How to do that in flutter?
You can use RichText and Unicode
final defaultStyle = TextStyle(color: Colors.black);
final blueStyle = TextStyle(color: Colors.blue);
return Center(
child: RichText(
text: TextSpan(
text: "c. 10\u{207B}\u{2076} seconds: ",
style: defaultStyle,
children: [
TextSpan(text: "Hadron epoch ", style: blueStyle, children: [
TextSpan(
style: defaultStyle,
text:
"begins: As the universe cools to about 10\u{00B9}\u{2070} kelvin,",
),
])
]),
),
);
More info: https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
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(
...
),
);