【问题标题】:Flutter - How can I use MediaQuery.of(context).copyWith(textScaleFactor)Flutter - 我如何使用 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,
          );
        }));

【问题讨论】:

    标签: flutter dart main


    【解决方案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

      【讨论】:

        猜你喜欢
        • 2021-06-08
        • 2021-04-11
        • 2020-11-10
        • 2021-12-02
        • 2021-03-23
        • 1970-01-01
        • 2019-07-02
        • 2020-04-22
        • 1970-01-01
        相关资源
        最近更新 更多