Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Flutter AppTheme TextTheme without overriding automatic text color

Tags:

flutter

themes

When I use the text theme from my app, Theme.of(context).textTheme.subhead The text is updated with the proper size and weight, but the color is also always black. How do I use the defined text theme, while still allowing the color to change automatically (eg: from black to white, when placed on a dark button?

My app theme is using the default text theme, (minus a couple weight changes)

I have looked around, and I think I am using it properly, but I am not certain.


TextTheme _customizeTextTheme(TextTheme base) {
  return base.copyWith(
    title: base.title.copyWith(
      fontWeight: FontWeight.bold,
    ),
    body2: base.body2.copyWith(
      fontWeight: FontWeight.bold,
    ),
  );
}

ThemeData _buildLightTheme() {
  const Color primaryColor = Colors.white;
  const Color secondaryColor = Colors.red;
  final ColorScheme colorScheme = const ColorScheme.light().copyWith(
    primary: primaryColor,
    secondary: secondaryColor,
  );
  final ColorScheme buttonColorScheme = const ColorScheme.light().copyWith(
    primary: secondaryColor,
    secondary: primaryColor,
  );
  final ThemeData base = ThemeData(
//    typography: Typography(
//      englishLike: Typography.englishLike2018,
//      dense: Typography.dense2018,
//      tall: Typography.tall2018,
//    ), //This is for another stackOverflowQuestion.  I can't get this to do anything
    fontFamily: 'Avenir',
    brightness: Brightness.light,
    accentColorBrightness: Brightness.dark,
    colorScheme: colorScheme,
    primaryColor: primaryColor,
    buttonColor: primaryColor,
    indicatorColor: Colors.white,
    toggleableActiveColor: const Color(0xFF1E88E5),
    splashColor: Colors.white24,
    splashFactory: InkRipple.splashFactory,
    accentColor: secondaryColor,
    errorColor: const Color(0xFFB00020),
    disabledColor: Colors.grey,
    hintColor: ServerColors.greenAccent,
    buttonTheme: ButtonThemeData(
      colorScheme: buttonColorScheme,
      textTheme: ButtonTextTheme.primary,
    ),
  );
  return base.copyWith(
    textTheme: _customizeTextTheme(base.textTheme),
    primaryTextTheme: _customizeTextTheme(base.primaryTextTheme),
    accentTextTheme: _customizeTextTheme(base.accentTextTheme),
  );
}

and when I build my app:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ServerThemes.lightTheme,
        initialRoute: Routes.home,
        routes: {
          Routes.home: (context) => AppHomePage(),
        },
        onUnknownRoute: (settings) => MaterialPageRoute(
            builder: (context) => UndefinedRoute(
                  badPathName: settings.name,
                )),
      ),
    );
  }
}

and when I use it:

FloatingActionButton.extended(
        onPressed: () {},
        label: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
                "View Cart",
                style: Theme.of(context).textTheme.title, // <---- RIGHT HERE
              ),
        ),
        icon: Icon(Icons.shopping_cart),
      ),

Currently the FAB is Red, and without styling it, the Text is white as expected. but when I add the styling line, the text goes black.

I would imagine that there is a way to use the textTheme without disabling the auto color adjust.?

like image 910
Airborne Eagle Avatar asked Jan 31 '26 03:01

Airborne Eagle


1 Answers

Okay, the solution was quite simple. i found it in the flutter code comments of theme_data.dart in flutter material:

  /// Text with a color that contrasts with the card and canvas colors.
  final TextTheme textTheme;

  /// A text theme that contrasts with the primary color.
  final TextTheme primaryTextTheme;

  /// A text theme that contrasts with the accent color.
  final TextTheme accentTextTheme;

I was using the text theme, when I should have been using the primary text theme.

solution:

FloatingActionButton.extended(
        onPressed: () {},
        label: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
                "View Cart",
                style: Theme.of(context).primaryTextTheme.headline6, // <---- RIGHT HERE
              ),
        ),
        icon: Icon(Icons.shopping_cart),
      ),
like image 93
Airborne Eagle Avatar answered Feb 01 '26 22:02

Airborne Eagle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!