flutter/flutter

Add `bodyFontFamily` and `displayFontFamily` to `TextTheme.apply()`

martin-braun opened this issue · 0 comments

Use case

When calling ThemeData.apply() we are able to define two different colors, separating base text from emphasized large text.

From https://stackoverflow.com/a/49287410/1540350:

bodyColor will be applied to headline, title, subhead, button, body1, and body2. displayColor will be applied to display1 through display4, and caption. If you specify both bodyColor and displayColor and use the same color value, that will effectively change text colors on all text styles.

Example:

final newTextTheme = Theme.of(context).textTheme.apply(
  bodyColor: Colors.pink,
  displayColor: Colors.pink,
);

I like this separation and would enjoy to have the same consistent experience for the font family, however there is only apply's fontFamily, so in order to make this separation with fonts, I have to construct some crazy tree:

  static ThemeData materialLightTheme = ThemeData.light().copyWith(
      colorScheme: ColorScheme.fromSeed(seedColor: envPrimaryColor),
      textTheme: ThemeData.light()
          .textTheme
          .apply(
            fontFamily: envBodyFont,
          )
          .copyWith(
              displaySmall: ThemeData.light().textTheme.displaySmall!.copyWith(
                    fontFamily: envDisplayFont,
                  ),
              displayMedium:
                  ThemeData.light().textTheme.displayMedium!.copyWith(
                        fontFamily: envDisplayFont,
                      ),
              displayLarge: ThemeData.light().textTheme.displayLarge!.copyWith(
                    fontFamily: envDisplayFont,
                  )),
      cupertinoOverrideTheme: CupertinoThemeData(
        primaryColor: envPprimaryColor,
      ));

Ugh, I'm not a fan of that.

Proposal

Something like this would satisfy me:

final newTextTheme = Theme.of(context).textTheme.apply(
  bodyFontFamily: envBodyFont,
  displayFontFamily: envDisplayFont,
);

Only if the specific value is not set, it should fallback to the more generic fontFamily to avoid breaking changes.

Thank you!