Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between fontSize and textScaleFactor in Flutter?

Tags:

flutter

The default for fontSize is 14.0. Therefore, textScaleFactor: 2.0 appears to be the same as fontSize: 28.0 as seen in my code example:

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Title')),
        body: Row(
          children: <Widget>[
            new Text("Jane", textScaleFactor: 2.0),
            new Text(" Doe", style: new TextStyle(fontSize: 28.0)),
          ],
        )
      )
    );
  }
}

enter image description here

What are the pros and cons? Are there any recommendations when to use one or another in specific cases?

like image 879
Tobi Obeck Avatar asked Sep 02 '18 15:09

Tobi Obeck


People also ask

What is textScaleFactor in Flutter?

textScaleFactor. The number of font pixels for each logical pixel. For example, if the text scale factor is 1.5, text will be 50% larger than the specified font size. The value given to the constructor as textScaleFactor. If null, will use the MediaQueryData.

How do you restrict font size in Flutter?

As a developer you should make sure your layout has enough room to render all it's contents when the font sizes are increased. But sometimes we actualy need font size to be fixed. All you have to do is create Material App Builder to set Media Query property "textScaleFactor: 1.0" for the entire app.

What is the default text size in Flutter?

The default size of the text in Flutter is 14 (in logical pixels).

How do I auto resize text in Flutter?

This rich text does not have a style so it will fall back to the default style (probably fontSize = 14 ). It has an implicit minFontSize of 12 that means it can be resized to 86% of its original size at the most (14 -> 12). Just set minFontSize = 0 or add style: TextStyle(fontSize: 200) to the AutoSizeText .


1 Answers

There's no difference between them in the rendering. What changes are their purpose.

Font-size is usually a per-component value. While the scale factor is more global. The fact that you can override the scale factor directly on the Text is just a bonus.

In your typical application, you'll have the following:

MediaQuery(
  data: MediaQuery.of(context).copyWith(textScaleFactor: 2.0),
  child: Whatever(
    child: Text("Foo", style: Theme.of(context).textTheme.headline),
  ),
);

Basically, consider textScaleFactor as a zoom option. While font-size is used to separate a title from its content.

like image 62
Rémi Rousselet Avatar answered Oct 21 '22 23:10

Rémi Rousselet