Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support text size accessibility in Flutter like Android sp size and iOS Dynamic Type

In Android you can set the font size using Scale-independent Pixels (sp) to take into account user font size preferences chosen in the settings.

android:textSize="26sp"

In iOS you can use Dynamic Type to so something similar.

label.font = UIFont.preferredFont(forTextStyle: .body)
label.adjustsFontForContentSizeCategory = true

How do you do the same thing in Flutter?

like image 928
Suragch Avatar asked Feb 01 '19 20:02

Suragch


People also ask

How do I make text size responsive in flutter?

You can simply set responsive or dynamic font size in flutter for your texts. You can use AutoSizeText widget to get responsive size of font in Flutter. First of all, just add auto_size_text on pubspec. yaml file as a dependency.

What is SP in flutter?

In Android you can set the font size using Scale-independent Pixels (sp) to take into account user font size preferences chosen in the settings. android:textSize="26sp"


1 Answers

Flutter has accessibility support for larger fonts built in by default. You can override this behavior by specifying a textScaleFactor, which Flutter normally uses to apply the user selected text size.

You can test this by comparing two Text widgets, the second one with textScaleFactor set to 1.0. The default font size for both of them is 14.0 logical pixels.

Widget _myWidget() {
  return Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Text('Some sample text'),
      Text('Some sample text', textScaleFactor: 1.0),
    ],
  );
}

You can get the current text scale factor like this:

final scale = MediaQuery.of(context).textScaleFactor;

Android

In Android settings Accessibility > Font size choose the smallest size.

enter image description here

Do it again and choose the largest size.

enter image description here

Notice that the first Text widget text changed sizes, but second one with the textScaleFactor override didn't.

iOS

In iOS go to Settings > General > Accessibility > Larger text and choose the smallest option.

enter image description here

And again with the largest setting:

enter image description here

like image 129
Suragch Avatar answered Jan 01 '23 21:01

Suragch