Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextScaleFactor for TextField in Flutter?

Was wondering is there a way to control textscalefactor for TextField widget in flutter. Basically I want to limit a text field from growing too large when a user increase font/text size in their devices accessibility settings.

Thanks.

like image 534
Caleb Hatcher Avatar asked Feb 07 '19 20:02

Caleb Hatcher


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 I customize my TextField Flutter?

In Flutter, this can be done using TextEditingController . First, create a TextEditingController and set it as a controller property of your TextField widget. In this example, I have added an extra Button and Text widget which will show the added text when you click the “Show Text” button.


1 Answers

You can wrap it with a MediaQuery with a custom textScaleFactor

Widget build(BuildContext context) {
  final mqData = MediaQuery.of(context);
  final mqDataNew = mqData.copyWith(textScaleFactor: mqData.textScaleFactor > 5.0 ? 5.0 : mqData.textScaleFactor)
  return MediaQuery(data: mqDataNew, child: TextField());
}

The TextField does not need to be a direct child of MediaQuery.

like image 110
Günter Zöchbauer Avatar answered Oct 14 '22 09:10

Günter Zöchbauer