Home > Net >  Flutter - How can I use MediaQuery.of(context).copyWith(textScaleFactor)
Flutter - How can I use MediaQuery.of(context).copyWith(textScaleFactor)

Time:12-18

in Flutter how can I use MediaQuery.of(context).copyWith(textScaleFactor:1.0) in the following main.dart?

I want my app is independent from the various screen size of iOS and Android that the user can set in the settings

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,
          );
        }));

CodePudding user response:

You can limit the Text Scale Factor to a specific size with this code

MaterialApp(
  builder: (BuildContext context, Widget child) {
    final MediaQueryData data = MediaQuery.of(context);
    return MediaQuery(
      data: data.copyWith(
        textScaleFactor: 1.0),
        child: child,
       );
    },
)

Also check this Post: How to manage global textScaleFactor in Flutter app properly? it shows some more code examples

CodePudding user response:

final width=MediaQuery.of(context)?.size.width ?? double.nan returns the width of your screen. If your interested in the widget size then use LayoutBuilder. LayoutBuilder returns the constraint of its parent: LayoutBuilder(builder(context,constraints){return Text("${constraints.maxWidth}");}

use the width and to a certain ratio of the screen size to set your widget size. there is no copyWith method for the mediaquery.

https://api.flutter.dev/flutter/widgets/MediaQuery-class.html

  • Related