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
Related
I want to add a TextButton in the middle of a paragraph in a Text widget. Similarly when writing a paragraph and want to add a button to let users route to that screen to get the desired information about something. How could I do that?
Thank you
you can use RichText to make your works clickable.
you can use like this :
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: 'By clicking Sign Up, you agree to our '),
TextSpan(
text: 'Terms of Service',
recognizer: TapGestureRecognizer()
..onTap = () {
print('Terms of Service"');
}),
TextSpan(text: ' and that you have read our '),
TextSpan(
text: 'Privacy Policy',
recognizer: TapGestureRecognizer()
..onTap = () {
print('Privacy Policy"');
}),
],
),
),
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
I have a line that has three buttons inside it. I know that I can use RichText with Gesture Recogniser to make them clickable like HTML <a href=""> tag. But I am new to flutter and don't know how can I achieve that behaviour.
Also is there any better option to do similar thing?
Also I don't want to open an External Link. Just navigate to another page.
I have attached a picture containing what I want to do. Picture of what I want to do
import 'package:flutter/gestures.dart';
and the widget
RichText(
text: TextSpan(
children: [
TextSpan(
text: "By Clicking Sign up you are agree to the ",
),
TextSpan(
text: "Privacy Policy ",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Privacy Policy"),
),
TextSpan(text: "and our "),
TextSpan(
text: "Terms and Conditions ",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Terms and Conditions"),
),
TextSpan(text: "and "),
TextSpan(
text: "Cookie Statement",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Cookie Statement"),
),
TextSpan(text: "."),
],
),
),
does it help?
you can wrap the Text widget with GestureDetector()
GestureDetector(
child: Text(
'Clickable text'
),
onTap: () {
//go to destination page
},
)
In case if you're opening to an external link you can check URL launcher to open websites
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.
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),
], ), ),