简体   繁体   English

Flutter中用户定义的textScaleFactor的通用范围是多少?

[英]What is the universal range of the user defined textScaleFactor in Flutter?

After my fruitless search for the expected range of the textScaleFactor parameter I attempted to answer my q by printing... 在无结果的搜索textScaleFactor参数的预期范围之后,我尝试通过打印来回答我的q。

MediaQuery.textScaleFactorOf(context);

2 times while debugging on my Note 9: The output was 0.8 & 2 when I set the font size (in my accessibility settings) @ its min & max, resp.. 在我的注9上进行调试时,进行了2次调试:当我设置字体大小(在我的辅助功能设置中)@时,其输出分别为0.8和2。

My q is: Can I expect this to be the universal range across all devices? 我的问是:我可以期望它成为所有设备的通用范围吗?

If you're unsure, I would certainly accept an answer from someone posting their own test results (especially if they were to test on iphone). 如果您不确定,我肯定会接受发布自己测试结果的人的回答(尤其是如果他们要在iphone上进行测试)。

On the iOS simulator and my iPhoneXS Max the default is 1.0 Even when Display View setting is Zoomed or Standard. 在iOS模拟器和iPhoneXS Max上,即使“ 显示视图”设置为“缩放”或“标准”,默认值为1.0。

When I go to Accessibility and change the size to the max available size (step by step): 当我转到“辅助功能”并将大小更改为最大可用大小时(逐步):

flutter: text scale 1.1176470588235294
flutter: text scale 1.2352941176470589
flutter: text scale 1.3529411764705883

If i check the toggle for the "Larger Accessibility Sizes" , the max I get: 如果我检查“更大的可访问性大小”的切换,我得到的最大值是:

flutter: text scale 3.1176470588235294

Going down with the slider in Accessibility (only 3 steps available): 使用“辅助功能”中的滑块进行下去(只有3个步骤可用):

flutter: text scale 0.9411764705882353
flutter: text scale 0.8823529411764706
flutter: text scale 0.8235294117647058

I don't know how useful those values can be to you, but to answer your questions you SHOULD NOT expect min=0.8 & max=2 ... 我不知道这些值对您有多有用,但是要回答您的问题,您不应该期望min = 0.8&max = 2 ...

In any case, if you need to constrain the factor somehow, as I don't know any way to inject them in the MediaQuery that MaterialApp uses, you should have a custom function that normalizes the MediaQuery.textScaleFactorOf(context), maybe at the root of your widget tree, and manually apply that to each Text::textScaleFactor ? 无论如何,如果您需要以某种方式限制因素,因为我不知道将它们注入MaterialApp使用的MediaQuery的任何方法,则应该有一个自定义函数来规范MediaQuery.textScaleFactorOf(context),也许在小部件树的根,然后手动将其应用于每个Text :: textScaleFactor?

Someone already provided a satisfactory answer to the question; 已经有人对该问题提供了满意的答案; this is just me expanding on their closing remarks: 这只是我在结束语中的扩展:

In any case, if you need to constrain the factor somehow, as I don't know any way to inject them in the MediaQuery that MaterialApp uses, you should have a custom function that normalizes the MediaQuery.textScaleFactorOf(context), maybe at the root of your widget tree, and manually apply that to each Text::textScaleFactor ? 无论如何,如果您需要以某种方式限制因素,因为我不知道将它们注入MaterialApp使用的MediaQuery的任何方法,则应该有一个自定义函数来规范MediaQuery.textScaleFactorOf(context),也许在小部件树的根,然后手动将其应用于每个Text :: textScaleFactor?

I ended up using the following code to constrain textScaleFactor within 0.8-2.0; 我最终使用以下代码将textScaleFactor限制在0.8-2.0之内; I added an extra constraint where the result will always be a multiple of 0.1: 我添加了一个额外的约束,其中结果将始终为0.1的倍数:

import 'package:flutter/material.dart';
import 'dart:math';
import 'authentication-page.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, child) {
        onLayout(context);
        return MediaQuery(
            child: child,
            data: MediaQuery.of(context).copyWith(textScaleFactor: max(0.8, min(2.0, (10*MediaQuery.textScaleFactorOf(context)).round()/10))));
      },
      title: 'Flutter Demo',
      home: AuthenticationPage(),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM