Search code examples
fluttertextdisablebold

How to disable Bold Text set by iOS Accessibility setting in Flutter?


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.


Solution

  • Thanks to Josiah Mendes, I found out my original answer had some unexpected issues. It is safer to update the MediaQueryData created by MaterialApp:

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

    Old Answer:

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