i have a piece of code below and i wanted to style the text "From" differently, i.e, display it black and bold in colour. It should remain in the same format.
Thank you.
Text("From :" + " " + from, style: GoogleFonts.viga(
color: Colors.blue[800],
fontWeight: FontWeight.w500,
), /*TextStyle(color: Colors.blue[800],)*/
),
You can use the RichText and TextSpan classes to achieve this behavior.
RichText(
text: TextSpan(
text: 'From:',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(text: ' $from', style: TextStyle(color: Colors.blue[800])),
],
),
)
You can read more about it here
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()));
I'd like to use Twemoji font in my Flutter app. I installed this font. But when I try to use it in TextSpan, I see monochromatic emoji instead of colored. How to fix it?
My code:
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Some text ", style: GoogleFonts.roboto(
fontSize: 24,
color: Colors.black,
)),
TextSpan(text: "🐣", style: TextStyle(
fontFamily: "TwitterColorEmoji",
fontSize: 24,
)),
]
)
)
I got it, Twemoji font works in all operating system but will currently only show color emoji in Firefox, Thunderbird, Photoshop CC 2017+, and Windows Edge V38.14393+. This is not a limitation of the font, but of the operating systems and applications.
As has been said, die to limits of operation system and applications, this font won't show colored emojies.
I think the best altenative way to show colored emojies is using WidgetSpan with SvgPicture like in code below:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Some text ",
style: GoogleFonts.roboto(
fontSize: 24,
color: Colors.black,
),
),
WidgetSpan(
child: SvgPicture.asset("assets/emojies/confused-face.svg"),
style: TextStyle(fontSize: 24),
),
],
),
),
How can i make paragraphs in flutter in a text widget?
It is all just one long text and it looks stupid
What I want:
Hello,
how are you?
What it is:
Hello, how are you?
Use \n for paragraphs, like this: Text('Hello,\n\nhow are you?')
another approach, use RichText:
RichText(
text: TextSpan(
text: 'Hello,',
style: TextStyle(color: Colors.black, fontSize: 18.0),
children: <TextSpan>[
TextSpan(
text: 'how are you?',
style: TextStyle(fontWeight: FontWeight.bold, )
),
],
),
)
A picture is worth a thousand words, let me show you what I tried to achieve:
The solution could be using Flutter built-in widget or third-party widget.
Anyone?
You could use the Text.rich() method (or the RichText Widget) to achieve what you want.
The following snippet corresponds to your screenshot.
return Scaffold(
body: Center(
child: Text.rich(
TextSpan(
text: 'I lost ',
style: TextStyle(fontSize: 24),
children: <TextSpan>[
TextSpan(
text: 'Password.',
style: TextStyle(
decoration: TextDecoration.underline,
)),
TextSpan(
text: ' ',),
TextSpan(
text: 'What do?',
style: TextStyle(
decoration: TextDecoration.underline,
)),
// can add more TextSpans here...
],
),
),
),
);
How can I change the color of part of a text?
I'm trying to make it so the text says "Don't have an account? Register now!" but I want to add color to the Register now! part. How can I do this if possible?
Use RichText https://docs.flutter.io/flutter/widgets/RichText-class.html
RichText(
text: TextSpan(
text: "Don't have an account? ",
style: TextStyle(color: Colors.black, fontSize: 40),
children: <TextSpan>[
TextSpan(text: ' Register now!', style: TextStyle(color: Colors.red)),
],
),
);