dart Flutter将颜色应用于自定义主题中的文本

7vux5j2d  于 2023-05-26  发布在  Flutter
关注(0)|答案(1)|浏览(112)

我正在使用Flutter + Dart制作一个移动的应用程序。我定义了一个CustomTheme类,如下所示

class CustomTheme {
  static ThemeData lightTheme = ThemeData(
      primaryColor: Colors.red,
      colorScheme: ColorScheme.fromSwatch().copyWith(
          primary: Colors.red,
          secondary: Colors.green),
      textTheme: lightTheme.textTheme.copyWith(
          displayMedium:
              lightTheme.textTheme.displayMedium!.copyWith(color: Colors.blue)),
      useMaterial3: true,
      fontFamily: GoogleFonts.merriweather().fontFamily);
}

我试图改变我的displayMedium的颜色,但颜色不被应用。我使用它如下:

Text("Hello", style: Theme.of(context).textTheme.displayMedium)

我怎样才能让颜色适用?

hgb9j2n6

hgb9j2n61#

用这个我希望它有用

class CustomTheme {
      static final TextStyle _displayMediumTextStyle = TextStyle(color: Colors.blue);

      static ThemeData lightTheme = ThemeData(
      primaryColor: Colors.red,
      colorScheme: ColorScheme.fromSwatch().copyWith(
      primary: Colors.red,
      secondary: Colors.green,
      ),
      textTheme: TextTheme(
      displayMedium: _displayMediumTextStyle,
      ).copyWith(
      headline4: _displayMediumTextStyle, // Example: Apply the same style to headline4
      ),
      useMaterial3: true,
      fontFamily: GoogleFonts.merriweather().fontFamily,
  );
}

相关问题