Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate from textScaleFactor to TextScalar.scale

Tags:

flutter

dart

textScaleFactor was recently deprecated and one is supposed to use the TextScaler.scale(double fontSize) instead, but not all places that take a textScaleFactor seemingly supports the new way.

For example ButtonStyleButton.scaledPadding is taking in a textScaleFactor, but what fontSize are you supposed to call TextScalar.scale with to pass in as the scale factor there?

    ButtonStyleButton.scaledPadding(
      const EdgeInsets.symmetric(horizontal: 16),
      const EdgeInsets.symmetric(horizontal: 8),
      const EdgeInsets.symmetric(horizontal: 4),
      MediaQuery.maybeOf(context)?.textScaler.scale(??),
    );

Previously:

    ButtonStyleButton.scaledPadding(
      const EdgeInsets.symmetric(horizontal: 16),
      const EdgeInsets.symmetric(horizontal: 8),
      const EdgeInsets.symmetric(horizontal: 4),
      MediaQuery.maybeOf(context)?.textScaleFactor,
    );
like image 770
spydon Avatar asked Nov 27 '25 17:11

spydon


2 Answers

The scale method of TextScaler takes your fontSize and scales it with the text scale factor.

That means if you pass it 1, it will return the textScaleFactor.

Before

double textScaleFactor = MediaQuery.of(context).textScaleFactor;

After

double textScaleFactor = MediaQuery.textScalerOf(context).scale(1);
like image 170
Christian Niemann Avatar answered Nov 30 '25 10:11

Christian Niemann


I have same problem. For the alternative maybe we can do this

final textScaleFactor = TextScaler.scale(10) / 10;
like image 31
Karen Hovhannisyan Avatar answered Nov 30 '25 10:11

Karen Hovhannisyan