如何在不覆盖自动文本颜色的情况下使用Flutter AppTheme TextTheme

时间:2019-11-09 03:59:11

标签: flutter themes

当我从应用程序中使用文本主题时,Theme.of(context).textTheme.subhead 文本将使用适当的大小和粗细进行更新,但颜色也始终为黑色。 如何使用定义的文本主题,同时仍然允许颜色自动更改(例如:当放置在深色按钮上时从黑色变为白色?

我的应用程序主题使用默认的文本主题(减去一些权重更改)

我环顾四周,我认为我使用得当,但不确定。


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

以及当我构建我的应用程序时:

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

当我使用它时:

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

当前,FAB为红色,并且未设置其样式,因此文本为预期的白色。但是当我添加样式线时,文本变为黑色。

我想有一种不用禁用自动颜色调整就可以使用textTheme的方法吗??

1 个答案:

答案 0 :(得分:0)

好的,解决方案非常简单。我在flutter材料中的theme_data.dart的flutter代码注释中找到了它:

  /// 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;

当我应该使用主要文本主题时,我正在使用文本主题。

解决方案:

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