English 中文(简体)
如何从文本ScaleFactor迁移到文本Scalar。
原标题:How to migrate from textScaleFactor to TextScalar.scale
  • 时间:2023-11-16 11:31:20
  •  标签:
  • flutter
  • dart

<代码>ScaleFactor最近作了解释,应当使用<代码>。 文本Scaler.pl(双轨),但并非所有采用<代码>ScaleFactor的地方似乎都支持这一新方法。

例如,ButtonStyleButton.pldPaids 正在一个<编码>textScaleFactor上,但fontSize是:请打电话TextScalar.calar.cal,并随附比额表而通过?

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

此前:

    ButtonStyleButton.scaledPadding(
      const EdgeInsets.symmetric(horizontal: 16),
      const EdgeInsets.symmetric(horizontal: 8),
      const EdgeInsets.symmetric(horizontal: 4),
      MediaQuery.maybeOf(context)?.textScaleFactor,
    );
最佳回答

我有同样的问题。 否则,我们就可以这样做。

final textScaleFactor = TextScaler.scale(10) / 10;
问题回答

安德森·演播室提供从预定执行中迁移的门槛:

BEFORE:

MediaQuery(
  data: MediaQuery.of(context).copyWith(
    textScaleFactor: hasFontScaling ? 1.5 : 1,
  ),
  child: card,
);

词汇:

MediaQuery(
  data: MediaQuery.of(context).copyWith(
    textScaler: TextScaler.linear(hasFontScaling ? 1.5 : 1),
  ),
  child: card,
);

There is still global way to define scale level for all widgets. Note, this may not be the best way to handle all scaling issues as it will limit user defined accessibility scaling. However, this defines scale level for text and widgets as very high user accesibility values coming from mobile OS settings may make the app unusable or distort the UI design. Before it was like this:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(      
      builder: (BuildContext context, Widget? child) {
        final mediaQueryObj = MediaQuery.of(context);
        final scaleRange = mediaQueryObj.textScaleFactor.clamp(0.5, 2.0);  // set min and max scale values here


        return MediaQuery(
          data: MediaQuery.of(context).copyWith(
            textScaleFactor: scaleRange,
          ),
          child: child!,
        );
      },

Now, as the Flutter textScaleFactor is deprected due to new requirement of Android 14 s scaling changes, we need to use textScaler and text scale clamping:

@override
      Widget build(BuildContext context) {
        return MediaQuery.withClampedTextScaling(
        minScaleFactor: 0.7, // set min scale value here
  maxScaleFactor: 2,  // set max scale value here
        child: MaterialApp(

  )
          },




相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签