How to style a part of the Text in Flutter? - flutter

I have a Text widget and I want to make a part of it bold. How can I do that?
Example: Text("Required interests: Music, Guitar")
I want "Required interests:" to be bold and rest to be normal.

You can use the Text.rich() to achieve this.
Text.rich(
TextSpan(
text: 'Required interests: ',
style: TextStyle(fontWeight: FontWeight.bold),
children: <TextSpan>[
TextSpan(text: 'Music, Guitar',
style: TextStyle(fontWeight: FontWeight.normal),
)
]
),
);
Note if you do not specify any style in any of the children's TextSpan it will use the parent's style.

Related

Flutter dynamic clickable part of text

I read different posts on how to make parts of a text clickable. What I did not found and that I need in my case, is that the clickable part of text is set dinamically because I have different translations for each language, so the clickable word can be in different parts of the sentence. I have some files with all the texts that my application uses and those texts contains placeholders, So I want something more or less like this:
title: RichText(
text: TextSpan(
style: Theme.of(context)
.textTheme
.bodyText1
?.copyWith(fontSize: 14.0),
children: <TextSpan>[
TextSpan(
text: labels.label1(new TextSpan(
text: labels.placeholder_value,
style: new TextStyle(color: Colors.blue),
recognizer: new TapGestureRecognizer()
..onTap = () async {
func(context);
},
)
]
),
)
This piece of code of course doesn't work, it is only to show better what I need...I want the TextSpan with blue color and TapGestureRecognizer to be passed as a parameter to label1

Pass params to stateless widget in Flutter

I am trying to do a stateless widget for inline text links.
I am using this answer here https://stackoverflow.com/a/55607224/3808307 to create the links
RichText(
text: TextSpan(
children: [
TextSpan(text: 'This is a going to be a Text which has '),
TextSpan(
text: 'single tap',
style: style,
recognizer: TapGestureRecognizer()
..onTap = () {
// single tapped
},
),
TextSpan(text: ' along with '),
TextSpan(
text: 'double tap',
style: style,
recognizer: DoubleTapGestureRecognizer()
..onDoubleTap = () {
// double tapped
},
),
TextSpan(text: ' and '),
TextSpan(
text: 'long press',
style: style,
recognizer: LongPressGestureRecognizer()
..onLongPress = () {
// long pressed
},
),
],
),
)
, but I would like to have a TextSpan that I can import, and already has the styling applied, passing it a text that would act as label and a function.
Do I need a stateful widget for this, or can I just use a stateless widget?
You can use a stateless widget without a problem.
In the case that you need to change the style of the text when you press the links you will need to use a stateful widget.

How to create sentence with different textStyle in flutter?

Is there any way to define a sentence with the different textStyle in the flutter?
Need more clarity in your question..
But as far as I understood..I believe that you need something like..
A sentence with a mix of different colors and font weights.. something like that..
If that is the case, You can go for RichText..
Here is a sample code..
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
You can get more information from the documentation..
https://api.flutter.dev/flutter/widgets/RichText-class.html

How to add red asterisk in label of TextFormField In Flutter

As we are not able to make widget like RichText/Text Span For styling TextFormField,
Can anyone help me out regarding this...
Now Getting:-
Expected Result:-
How we can achieve a result like this?
Simplest way, but not exactly equals:
TextField(
decoration: InputDecoration(
hintText: 'Ciao',
suffixText: '*',
suffixStyle: TextStyle(
color: Colors.red,
),
),
),
Or create a custom TextField to use hint as Widget instead of String
You can use my two customized files:
input_decorator.dart
text_field_custom.dart
TextFieldCustom(
hintText: Text.rich(
TextSpan(
text: 'FIRST NAME',
children: <InlineSpan>[
TextSpan(
text: '*',
style: TextStyle(color: Colors.red),
),
],
style: TextStyle(color: Colors.black54),
),
),
),
Try these two files I had same requirement.
Just add attribute isMandate: true in CInputDecoration and use CTextField.
CTextField(
...
decoration: new CInputDecoration(
isMandate: true,
...
))
https://github.com/sumit1954/Flutter/blob/master/CInputDecorator.dart
https://github.com/sumit1954/Flutter/blob/master/CTextField.dart
Place this code in TextFormField or TextField
this label works as an hint property
label: RichText(
text: TextSpan(
text: 'Full Name',
style: const TextStyle(
color: Colors.grey),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
)
)
]
),
floatingLabelBehavior:FloatingLabelBehavior.never,
look at above screenshot of label parameter holding a Widget not a String without any error
Achieved this in hard fast way. Replace input_decorator.dart with below code:
https://github.com/neal786y/InputDecoratorForMandatoryFields/blob/master/input_decorator.dart
In your InputDecoration scope add a property "isMandatoryField: true"
Worked for me on temporary basis.
With the latest version of flutter you can also send a widget in label parameter. So, something like this is possible
label: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(label.replaceAll('*', '')),
label.contains('*')
? Text(
'*',
style: TextStyle(color: Colors.red),
)
: Container(),
],
),
Use label property inside InputDecorator
See this answer https://stackoverflow.com/a/72209425/8913302

fomat partial text of Text widget in flutter

My Text widget looks like Text("Last Active: some location area..").
I can change text style of complete text using style. but I just like to change Last active as a bold. Text("<b>Last Active</b> some location area name..")
If I go with Row for separate text it will work but render a problem of spacing.
What's the best solution for this. And to make it bold is the only requirement.
Thanks
RichText is solution
RichText(text: TextSpan(children: [
TextSpan(text: 'Last Active', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' some location area name..')
]))
I had problems with Andrey Turkovsky solution, the text area would be blank. I did find a slightly alternative solution. (I did want to comment to Andreys, and maybe he might be able to explain the differences between the two, Id be interested)
Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Last Active',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' some location area name..')
],
),
)
Working solution in 2022:
RichText( text: TextSpan(text: 'Last Active: ', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black), children:
<TextSpan>[
TextSpan(text: 'some location area...', style: DefaultTextStyle.of(context).style),
], ), ),