Flutter中自定义textScaleFactor的通用范围是多少?
What is the universal range of the user defined textScaleFactor in Flutter?
在毫无结果地搜索 textScaleFactor 参数的预期范围后,我试图通过打印来回答我的问题...
MediaQuery.textScaleFactorOf(context);
在我的 Note 9 上调试时 2 次:当我设置字体大小时(在我的辅助功能设置中)@ 其最小值和最大值,输出为 0.8 和 2,resp..
我的问题是:我可以期望这是所有设备的通用范围吗?
如果您不确定,我当然会接受发布自己测试结果的人的回答(特别是如果他们要在 iphone 上进行测试)。
在 iOS 模拟器和我的 iPhoneXS Max 上默认是 1.0
即使 Display View setting 是缩放或标准。
当我转到“辅助功能”并将尺寸更改为最大可用尺寸时(逐步):
flutter: text scale 1.1176470588235294
flutter: text scale 1.2352941176470589
flutter: text scale 1.3529411764705883
如果我检查 "Larger Accessibility Sizes" 的开关,我得到的最大值:
flutter: text scale 3.1176470588235294
使用“辅助功能”中的滑块向下滑动(仅可用 3 个步骤):
flutter: text scale 0.9411764705882353
flutter: text scale 0.8823529411764706
flutter: text scale 0.8235294117647058
我不知道这些值对你有多大用处,但要回答你的问题,你不应该期望 min=0.8 & max=2 ...
无论如何,如果你需要以某种方式限制这个因素,因为我不知道有什么方法可以将它们注入到 MaterialApp 使用的 MediaQuery 中,你应该有一个自定义函数来规范化 MediaQuery.textScaleFactorOf (上下文),也许在您的小部件树的根部,然后手动将其应用于每个 Text::textScaleFactor ?
已经有人对问题给出了满意的答案;这只是我对他们的结束语的补充:
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 ?
我最终使用以下代码将 textScaleFactor 限制在 0.8-2.0 范围内;我添加了一个额外的约束,结果总是 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(),
);
}
}
在毫无结果地搜索 textScaleFactor 参数的预期范围后,我试图通过打印来回答我的问题...
MediaQuery.textScaleFactorOf(context);
在我的 Note 9 上调试时 2 次:当我设置字体大小时(在我的辅助功能设置中)@ 其最小值和最大值,输出为 0.8 和 2,resp..
我的问题是:我可以期望这是所有设备的通用范围吗?
如果您不确定,我当然会接受发布自己测试结果的人的回答(特别是如果他们要在 iphone 上进行测试)。
在 iOS 模拟器和我的 iPhoneXS Max 上默认是 1.0 即使 Display View setting 是缩放或标准。
当我转到“辅助功能”并将尺寸更改为最大可用尺寸时(逐步):
flutter: text scale 1.1176470588235294
flutter: text scale 1.2352941176470589
flutter: text scale 1.3529411764705883
如果我检查 "Larger Accessibility Sizes" 的开关,我得到的最大值:
flutter: text scale 3.1176470588235294
使用“辅助功能”中的滑块向下滑动(仅可用 3 个步骤):
flutter: text scale 0.9411764705882353
flutter: text scale 0.8823529411764706
flutter: text scale 0.8235294117647058
我不知道这些值对你有多大用处,但要回答你的问题,你不应该期望 min=0.8 & max=2 ...
无论如何,如果你需要以某种方式限制这个因素,因为我不知道有什么方法可以将它们注入到 MaterialApp 使用的 MediaQuery 中,你应该有一个自定义函数来规范化 MediaQuery.textScaleFactorOf (上下文),也许在您的小部件树的根部,然后手动将其应用于每个 Text::textScaleFactor ?
已经有人对问题给出了满意的答案;这只是我对他们的结束语的补充:
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 ?
我最终使用以下代码将 textScaleFactor 限制在 0.8-2.0 范围内;我添加了一个额外的约束,结果总是 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(),
);
}
}