Flutter: How to programmatically change the font weight - flutter

I am new to flutter and trying to build a text field with basic options just as in word (underlineing, changing fontsize and weight,...)
I was able to write a code for changing the font size and I tried the same for font weight, but i wasn´t able to make it work.
As I am also new to Stackoverflow I am having troubles with inserting my working code properly...
So my question is, if you could share your version of changing the fontweight of a text in a textfield via buttons just like in word.
Thank you!

Firstly, use stateful widget, then make variable:
bool isBold = false;
Then use a Text widget like this:
Text(‘hello’, style: TextStyle(fontWeight: isBold ? FontWeight.bold : FontWeight.normal))
Next you need a button for user to toggle fontweight option:
MaterialButton(onPressed: () => setState(() => isBold = !isBold))

You can change fontweight of the textfield by using style property of Textfield
TextField(
....
style: fontweight=Fontweight.bold),
...

Related

Text widget cannot read html tags Flutter

Text widget in Flutter dose not support a special charterers like &, where sometimes this charterer comes like "first & second" from the database, or "&quot" should be ".
Text widget should read/convert these characters automatically. I have already fixed by following lines, But I need to put this solution in all Text widgets I have in my app.
Is it possible to give all Text widgets the possibility to read these special charterers directly in main function in runAPP or "MaterialApp" without putting the following lines in all Text widgets the app has?
final document = parse(htmlString);
final String parsedString = parse(document.body.text).documentElement.text;
Use this package flutter_html and try as follows:
Html(data: htmlString, style: {
"body": Style(
fontSize: FontSize(18),
color: Colors.black,
fontWeight: FontWeight.normal)
}),

Which one is performance wise better Text or extracted TextWidget function?

In my flutter code when I am creating an UI lot of places using Text widget. So I converted this Text widget into a function and calling everywhere? Text widget also including some styling. So calling the function or calling the Text widget is better (execution speed)?
Example code:
Text('Time left to Entrance exam',style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
color: Colors.black
),);
or
Text buildText(String text,double fontSize, Color color) {
return Text(text,
style: TextStyle(
fontSize: fontSize,
color: color
),);
}
TextWidget function is more useful than multiple texts. It's absolutely a good practice and if any changes need you can able to change centrally and it's time-saving with clean code. You do not get the execution speed issue. And more important things, in both widget and function you just call a single Text widget. That's why there is no performance issue. You go for the second one for good practice.

How to change Font Family using setState in Flutter?

I want to change my Text Widget font family on button click.
Which means I created a Container and in this Container I added multiple text style widget with GestureDetector widget. So whenever I click on Particular font family, I want to change my Text widget Font-Family.
Any suggestion for doing this type of functionality.
Hope this works or modify accordingly:
code inside your Text Widget:
Text("Your Text",
style: tapped
?TextStyle(fontFamily: 'Your-Font-Family')
:TextStyle(fontFamily: 'Your-Second-Font-Family'),
),
code inside Gesture Detector:
GestureDetector(
child: your child widget,
onTap: () {
setState(() {
tapped = !tapped;
});
}),
and remember to define bool tapped; in your widget class.
Still, if you face any difficulty, add the code to comment so I can help you out.
Use a variable to store font name
reference it in your text Widget
then call setState to update the value of variable and UI
String fontName;
//your TextWidget
Text("some text for testing",
style: TextStyle( fontFamily: fontName )
)
//function to update the value
_updateFont(String name){
setState((){
fontName = name;
});
}
make sure you are importing the fonts to be used
add them in pubspec.yaml file under fonts
- family: font1
fonts:
- asset: assets/fonts/font1.ttf

Check for color during widget test?

The goal is to verify the color of a RaisedButton.icon
During my widget tests, I have the ability to look for text with find.text as well as icons with find.byIcon. There is no built in method for finding color.
How does one do the equivalent of find.color?
And example of my code is
RaisedButton.icon(
color: Color(_isAttendingEvent ? 0xFFD9FFB3 : 0xFFE3FFC7),
icon: Icon(_isAttendingEvent ? Icons.star : Icons.star_border),
label: Text(
_isAttendingEvent ? 'Attending' : 'Attend',
),
),
I'm trying to determine whether there is color: Color(0xFFD9FFB3) or color: Color(0xFFE3FFC7)
And I'm not sure if this is possible
I am not sure with RaisedButton.icon, but if it is just an icon you can try using:
expect((tester.firstWidget(find.byType(Icon)) as Icon).color, Color(0xFFE3FFC7));
If it not the first icon widget that appears on your screen, you can specific an index of it by:
expect((tester.widget(find.byType(Icon).at(/*index*/)) as Icon).color, Color(0xFFE3FFC7));
Note: To find a widget colour, you need to cast that widget to be something that contain a color property
For example:
To find Material color you can use:
expect((tester.firstWidget(find.byType(Material)) as Material).color, Colors.black);
To find Container with BoxDecoration color:
expect(((tester.firstWidget(find.byType(Container)) as Container).decoration
as BoxDecoration).color, Colors.black);
To find Text color:
expect(((tester.firstWidget(find.text('text')) as Text).style).color, Colors.black);
To find Appbar Material. color
final appbar = tester.widget<AppBar>(find.byKey(Key("appbar")));
expect(appbar.backgroundColor,Colors.white);
To find Text color Material
final text = tester.widget<Text>(find.text("text"));
expect(text.style.color, Colors.grey);
expect(text.style.fontSize, 15);
Update you flutter version. It's working now, i am able to access color in widget test. My current flutter version is "Flutter 1.15.18 • channel dev".
Earlier it was not working with version "Flutter v1.12.13+hotfix.5"
Hope this will help. More details here
If you're using a TextButton, ElevatedButton, or Outlined button, the background color will be a MaterialStateProperty. You can verify the color in almost the same way as mentioned above:
expect(
tester.widget(textButton.first),
isA<TextButton>().having(
(w) => w.style?.backgroundColor?.resolve({}),
'button color',
Colors.grey,
));

check if text Widget has style

I am trying to add a default style to a Text Widget, but I need to be able to overwrite it.
this is what I am trying right now.
var newTitle = title;
if (title is Text) {
Text titleText = title as Text;
newTitle = Text(titleText.data, style: TextStyle == null ? TextStyle() : TextStyle(fontWeight: FontWeight.bold));
}
So I want to check if it has style if not add the default style to it, otherwise use overwriten
Flutter provides a simple way to do this by providing a DefaultTextStyle widget, which can be used to specify a default texty style for the subtree. If a child Text widget already defines a style, the specific Text style will be used.
DefaultTextStyle(
child: title,
style: TextStyle(fontWeight: FontWeight.bold),
),