3

I know I should not disable any of the text and bold settings set on the device but I have a reason for it. I have disabled the text size by setting the textScaleFactor to 1 on a global level. But this does nothing to avoid the user from setting the Bold Text option that also changes the size of the text. How do I override that function also so even if the user sets it on the device there is no effect on the app? I have tried setting the fontweitgh on the text item using a TextStyle but that does also not stop the Bolding.

Jaco Fourie
  • 112
  • 3
  • 11

2 Answers2

5

After checking Text widget I found out that it's using boldTextOverride from MediaQuery to change font weight of default text style provided to it. So I guess you can override it by wrapping your MeterialApp inside of a MediaQuery and assigning false to boldText and set useInheritedMediaQuery on MaterialApp to true to prevent it from creating another MediaQuery. Something like this:

return MediaQuery(
  data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window).copyWith(boldText: false),
  child: MaterialApp(
    useInheritedMediaQuery: true,
  ),
);
Amir_P
  • 8,322
  • 5
  • 43
  • 92
4

Amir_P's answer is good but, when testing it out, I found that it breaks switching between dark mode and light mode in system, as the Flutter app would only apply system brightness after killing and reopening the app on iOS rather than immediately while the app is open. Therefore, I think that rather than wrapping MaterialApp in a MediaQuery, the following should be done:

return MaterialApp(
    builder: (context, child) => MediaQuery(
          data: MediaQuery.of(context).copyWith(boldText: false),
          child: child!,
    ),
);

The builder property of the Material App is specifically for overriding things like this - see https://api.flutter.dev/flutter/material/MaterialApp/builder.html

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    This is the best answer, simple and works! The one from @Amir_P is causing weird MediaQuery.of(context).size.height problems! – SwiftiSwift Mar 01 '23 at 06:30