I am trying to make only the word "Name:" bold in code below, but am unsure as to the best way to do that. I have tried the "textspan" function and putting the two words on seperate lines but this does not seem to be working.
The original code is below:
ListTile(
leading: Icon(
Icons.book,
color: Colors.grey,
),
title: Text("Name: Timmy",
style: theme.textTheme.body1),
),
You should use RichText with TextSpan like this
Widget build(BuildContext context) {
TextStyle defaultStyle = TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: 20.0);
return RichText(
text: TextSpan(
style: defaultStyle,
text: "Name: ",
children: <TextSpan>[
TextSpan(text: 'Timmy', style: TextStyle(fontWeight: FontWeight.normal)),
],
),
);
}
or like this
Widget build(BuildContext context) {
TextStyle defaultStyle = TextStyle(color: Colors.black, fontSize: 20.0);
return RichText(
text: TextSpan(
style: defaultStyle,
children: <TextSpan>[
TextSpan(text: 'Name: ', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: 'Timmy'),
],
),
);
}
Related
I want to make specific letter to have a custom font or style in Flutter, is it possible to do that? I only saw tutorial on how to make specific word in Flutter to have custom style by using RichText. Is there anyway to make a specific letter instead of a word?
for example
πn elephant
In this case the letter A will have a custom font but the letter n does not.
The text parameter in a TextSpan can be omitted. So for what you want, you can just use the children parameter and have the first child be a single character.
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'L',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
const TextSpan(text: 'ike this?')
],
),
),
You can do like this :
Text.rich(
TextSpan(text: 'This is textspan ', children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)
]),
)
You can customize it any way you want :D
String myString = "Helloooo";
String specialChar = "o";
RichText(
text: TextSpan(
children: myString.characters.map((e) {
return TextSpan(
text: e,
style: e == specialChar
? TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 40)
: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold, fontSize: 50),
);
}).toList()));
There is a random list whose length is not constant. For example
List<String> exampleList = List<String>();
exampleList.add("one");
exampleList.add("second random line");
exampleList.add("third random line");
Depending on the content of the exampleList[index], which is generated randomly, it is necessary to change the appearance, design, styles etc. If you use RichText TextSpan, then already hardcoded data is transferred there, and I need to generate this RichText dynamically. Any ideas on this?
approximate result of formating
child: RichText(
text: TextSpan(
text: 'GitHub is a development platform inspired by the way you work. From ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
children: <TextSpan>[
TextSpan(text: 'op_n',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Sending Message"),
));
}
),
TextSpan(
text: ' to ',
style: TextStyle(color: Colors.grey,
fontSize: 20,
)
),
TextSpan(
text: 'busin_ss,',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Sending Message"),
));
}
),
TextSpan(
text: ' you can host and review code, manage projects, and build software alongside 36 million developers.',
style: TextStyle(color: Colors.grey,
fontSize: 20,
)
)
]
),
)
Based on your sample, I see that you define each part of your RichText with a text, a style, and optionally a callback.
You could use a List<MyText> instead of a List<String>:
class MyText {
final String text;
final TextStyle style;
final void Function(BuildContext) callback;
MyText({
this.text,
this.style,
this.callback,
});
}
List<MyText> textSpanList = [
MyText(
text:
'GitHub is a development platform inspired by the way you work. From ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
MyText(
text: 'op_n',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
callback: (context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sending Message"),
),
);
},
),
MyText(
text: ' to ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
MyText(
text: 'busin_ss,',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
callback: (context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sending Message"),
),
);
},
),
MyText(
text:
' you can host and review code, manage projects, and build software alongside 36 million developers.',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
];
And then use the List<MyText> as follows:
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
home: Scaffold(body: MyWidget()),
),
);
}
class MyWidget extends StatelessWidget {
const MyWidget({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
padding: const EdgeInsets.all(8.0),
child: RichText(
text: TextSpan(
children: textSpanList
.map(
(data) => data.callback == null
? TextSpan(
text: data.text,
style: data.style,
)
: TextSpan(
text: data.text,
style: data.style,
recognizer: TapGestureRecognizer()
..onTap = () => data.callback(context),
),
)
.toList(),
),
),
);
}
}
i'm trying to display the old and the new price inside of a RichText.
if(regularPrice >= discountPrice){
TextSpan(
text: ' $regularPrice',
style: TextStyle(
decoration: TextDecoration.lineThrough,
decorationThickness: 2.85,
fontSize: 15,
ontWeight: FontWeight.bold),
),
TextSpan(
text: ' $discountPrice',
style: TextStyle(
fontSize: 15,
ontWeight: FontWeight.bold),
),
}else{
TextSpan(
text: ' $regularPrice',
style: TextStyle(
fontSize: 15,
ontWeight: FontWeight.bold),
),
}
Can i use this logic inside a RichText?
You can use the spread operator:
class RichTextDiscountedPrice extends StatelessWidget {
static const regularPrice = 100;
static const discountedPriced = 95;
#override
Widget build(BuildContext context) {
final regularPriceTextSpan = TextSpan(text: ' $discountedPriced', style: TextStyle(fontWeight: FontWeight.bold));
final discountedTextSpanList = [
TextSpan(text: '$regularPrice', style: TextStyle(decoration: TextDecoration.lineThrough)),
regularPriceTextSpan
];
return Center(
child: RichText(
text: TextSpan(
text: "Price ",
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
if (regularPrice > discountedPriced) ...discountedTextSpanList else ...<TextSpan>[regularPriceTextSpan],
],
),
),
);
}
}
Note: I edited the styles a little to reduce the number of lines for the example.
I want to show an icon in text widget. How do I do this ?
The following code only shows the IconData
Text("Click ${Icons.add} to add");
Flutter has WidgetSpan() to add a widget inside the RichText().
Example use:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
)
Above code will produce:
You can treat the child of WidgetSpan like the usual widget.
Screenshot:
Using Wrap:
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Row:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Text.rich:
Text.rich(
TextSpan(
children: [
TextSpan(text: 'Click'),
WidgetSpan(child: Icon(Icons.add)),
TextSpan(text: 'to add'),
],
),
)
An alternative might be to use emoji.
In Dart, strings supports escape sequences for special characters. For unicode emoji, you can use \u and curly braces with emoji hex code inside. Like this:
Text('Click \u{2795} to add')
The result is:
You can find a complete unicode emoji list here: http://unicode.org/Public/emoji/1.0/emoji-data.txt
Row Widget can be one solution for this issue, but you have to use different widgets to make it happen.
Follow below example.
Row(
children: <Widget>[
Text("Hi"),
Icon(Icons.add),
Text("Hello")
]
)
Another option is String.fromCharCode(int charCode)
This is icons.dart created by flutter team and charCodes are can found here.
RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
text: 'This is alarm icon : ',
children: <TextSpan>[
TextSpan(
text: String.fromCharCode(0xe190), //<-- charCode
style: TextStyle(
fontFamily: 'MaterialIcons', //<-- fontFamily
fontSize: 24.0,
color: Colors.red,
),
)
],
),
)
And result :
I think it's better to use the Row widget. here is an example
Row(
children: [
Icon(Icons.download_rounded),
Text(" Download")
],
)
You can use WidgetSpan() on Text.rich().
Here's a link for InlineSpan-class.
You can use this Package to achieve this.
I am not able to find the pub of this package.
Here is the Implementation.
RealRichText(
[
TextSpan(
text: "A Text Link",
style: TextStyle(color: Colors.red, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked.");
},
),
ImageSpan(
AssetImage("packages/real_rich_text/images/emoji_9.png"),
imageWidth: 24,
imageHeight: 24,
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 10)),
TextSpan(
text: "εεε",
style: TextStyle(color: Colors.yellow, fontSize: 14),
),
TextSpan(
text: "#Somebody",
style: TextStyle(
color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: " #RealRichText# ",
style: TextStyle(color: Colors.blue, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: "showing a bigger image",
style: TextStyle(color: Colors.black, fontSize: 14),
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 5)),
TextSpan(
text: "and seems working perfectβ¦β¦",
style: TextStyle(color: Colors.black, fontSize: 14),
),
],
);
You can also check out below issue for more:
Flutter Issue #2022
For simple icons, You can use a Unicode character as regular text, just copy and paste.
You can search and copy Unicodes from this site
Text("Enjoy! πβππ₯β
ππ")
This method will work with any Text widget and package.
You can use this function:
Widget _spanIcon(String text, IconData icon, TextStyle st) {
return Row(
children: [
Text(text,style: st,),
Text(
String.fromCharCode(icon.codePoint), //<-- charCode from icon data
style: TextStyle(
fontFamily: icon.fontFamily, //<-- fontFamily from icon data
fontSize: st.fontSize! + 4.0,//you can increase font size for icon here
color: st.color,
),)
],
);}
and then use it :
_spanIcon('11',Icons.access_alarm, TextStyle(fontSize: 14.0, color: Color.fromARGB(113, 0, 0, 0))),
Using the TextStyle() class in Flutter, how can I strike through an old price?
To apply strikethrough decoration to a Text widget directly:
Text('\$8.99', style: TextStyle(decoration: TextDecoration.lineThrough))
You can also style separate spans of a paragraph by using the RichText widget, or the Text.rich() constructor.
Based on this example code, to show a discounted price:
RichText()
new RichText(
text: new TextSpan(
text: 'This item costs ',
children: <TextSpan>[
new TextSpan(
text: '\$8.99',
style: new TextStyle(
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
new TextSpan(
text: ' \$3.99',
),
],
),
)
Text.rich()
Text.rich(TextSpan(
text: 'This item costs ',
children: <TextSpan>[
new TextSpan(
text: '\$8.99',
style: new TextStyle(
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
new TextSpan(
text: ' \$3.99',
),
],
),
)
style: TextStyle(decoration: TextDecoration.lineThrough),
i use this
Column(
children: [
Text(
"sample text"
),
Divider(color: Colors.red,)
],
),