Flutter - 我如何使用 MediaQuery.of(context).copyWith(textScaleFactor)
Posted
技术标签:
【中文标题】Flutter - 我如何使用 MediaQuery.of(context).copyWith(textScaleFactor)【英文标题】:Flutter - How can I use MediaQuery.of(context).copyWith(textScaleFactor) 【发布时间】:2022-01-20 11:36:16 【问题描述】:在 Flutter 中如何在以下 main.dart 中使用 MediaQuery.of(context).copyWith(textScaleFactor:1.0)?
我希望我的应用独立于用户可以在设置中设置的各种 ios 和 android 屏幕尺寸
return MultiProvider(
providers: <SingleChildWidget>[
...providers,
],
child: DynamicTheme(
defaultBrightness: Brightness.light,
data: (Brightness brightness)
if (brightness == Brightness.light)
return themeData(ThemeData.light());
else
return themeData(ThemeData.dark());
,
themedWidgetBuilder: (BuildContext context, ThemeData theme)
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'MyApp',
theme: theme,
initialRoute: '/',
onGenerateRoute: router.generateRoute,
localizationsDelegates: <LocalizationsDelegate<dynamic>>[
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
EasyLocalization.of(context).delegate,
DefaultCupertinoLocalizations.delegate
],
supportedLocales: EasyLocalization.of(context).supportedLocales,
locale: EasyLocalization.of(context).locale,
);
));
【问题讨论】:
【参考方案1】:您可以使用此代码将文本比例因子限制为特定大小
MaterialApp(
builder: (BuildContext context, Widget child)
final MediaQueryData data = MediaQuery.of(context);
return MediaQuery(
data: data.copyWith(
textScaleFactor: 1.0),
child: child,
);
,
)
另请查看此帖子:How to manage global textScaleFactor in Flutter app properly? 它显示了更多代码示例
【讨论】:
【参考方案2】:最终宽度=MediaQuery.of(context)?.size.width ?? double.nan 返回屏幕的宽度。如果您对小部件大小感兴趣,请使用 LayoutBuilder。 LayoutBuilder 返回其父级的约束: LayoutBuilder(builder(context,constraints)return Text("$constraints.maxWidth");
使用宽度和屏幕大小的一定比例来设置您的小部件大小。 mediaquery 没有 copyWith 方法。
https://api.flutter.dev/flutter/widgets/MediaQuery-class.html
【讨论】:
以上是关于Flutter - 我如何使用 MediaQuery.of(context).copyWith(textScaleFactor)的主要内容,如果未能解决你的问题,请参考以下文章
Flutter No MediaQuery ancestor could be found starting from the context that was passed to MediaQuer
Flutter No MediaQuery ancestor could be found starting from the context that was passed to MediaQuer