flutter-caffold提供黑色背景,而不是我的自定义颜色

发布于 2025-02-05 03:22:32 字数 1522 浏览 2 评论 0原文

我将apptheme类做如下:

class AppTheme {
  static const Color white = Colors.white,
      white2 = Color(0x00fefefe),
      grey1 = Color(0x00e5e5e5),
      grey2 = Color(0x006b718b),
      grey3 = Color(0x004b4a5a),
      grey4 = Color(0x00AFBEC4),
      black = Colors.black,
      purple = Color(0x007154b8);

  static const TextStyle s1 = TextStyle(
        color: AppTheme.black,
        fontSize: 16,
        fontWeight: FontWeight.w600,
        height: 2.4,
      ),
      s2 = TextStyle(
        color: AppTheme.black,
        fontSize: 14,
        fontWeight: FontWeight.w600,
        height: 2.1,
      ),
      s3 = TextStyle(
        color: AppTheme.grey3,
        fontSize: 12,
        height: 1.8,
        fontWeight: FontWeight.w500,
      ),
      s4 = TextStyle(
        fontSize: 10,
        color: AppTheme.purple,
        height: 1.5,
        fontWeight: FontWeight.w500,
      );

  static final ThemeData theme = ThemeData(
    fontFamily: 'Poppins',
    textTheme: const TextTheme(
      headlineMedium: s2,
      bodyLarge: s1,
      bodyMedium: s3,
      bodySmall: s4,
    ),
    scaffoldBackgroundColor: grey1,
    bottomAppBarColor: white,
  );
}

然后,我为主题的apptheme.theme.theme.theme提供了yeartialapp。但是,每当我使用自定义颜色时,例如

return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,

我的自定义颜色,我都会得到一个完全黑色的脚手架。即使直接用作apptheme.grey1,我也会得到一个完全黑色的脚手架。我在做什么错以及如何解决?

I made an AppTheme class as follows :

class AppTheme {
  static const Color white = Colors.white,
      white2 = Color(0x00fefefe),
      grey1 = Color(0x00e5e5e5),
      grey2 = Color(0x006b718b),
      grey3 = Color(0x004b4a5a),
      grey4 = Color(0x00AFBEC4),
      black = Colors.black,
      purple = Color(0x007154b8);

  static const TextStyle s1 = TextStyle(
        color: AppTheme.black,
        fontSize: 16,
        fontWeight: FontWeight.w600,
        height: 2.4,
      ),
      s2 = TextStyle(
        color: AppTheme.black,
        fontSize: 14,
        fontWeight: FontWeight.w600,
        height: 2.1,
      ),
      s3 = TextStyle(
        color: AppTheme.grey3,
        fontSize: 12,
        height: 1.8,
        fontWeight: FontWeight.w500,
      ),
      s4 = TextStyle(
        fontSize: 10,
        color: AppTheme.purple,
        height: 1.5,
        fontWeight: FontWeight.w500,
      );

  static final ThemeData theme = ThemeData(
    fontFamily: 'Poppins',
    textTheme: const TextTheme(
      headlineMedium: s2,
      bodyLarge: s1,
      bodyMedium: s3,
      bodySmall: s4,
    ),
    scaffoldBackgroundColor: grey1,
    bottomAppBarColor: white,
  );
}

I then provided the AppTheme.theme to the MaterialApp for theme. However whenever I am using my custom colors, for example

return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,

instead of my custom color, I am getting a completely black scaffold. Even when using directly as AppTheme.grey1 I am getting a completely black scaffold. What am I doing wrong and how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2025-02-12 03:22:32

更改 themedata 这样的代码:

  import 'package:flutter/material.dart';

  class AppTheme {
    static const Color white = Colors.white,
        white2 = Color(0x00fefefe),
        grey1 = Color(0xffe5e5e5),
        grey3 = Color(0x004b4a5a),
        grey4 = Color(0x00AFBEC4),
        black = Colors.black,
        purple = Color(0x007154b8);
    static const TextStyle s1 = TextStyle(
          color: black,
          fontSize: 16,
          fontWeight: FontWeight.w600,
          height: 2.4,
        ),
        s2 = TextStyle(
          color: black,
          fontSize: 14,
          fontWeight: FontWeight.w600,
          height: 2.1,
        ),
        s3 = TextStyle(
          color: grey3,
          fontSize: 12,
          height: 1.8,
          fontWeight: FontWeight.w500,
        ),
        s4 = TextStyle(
          fontSize: 10,
          color: purple,
          height: 1.5,
          fontWeight: FontWeight.w500,
        );

    static ThemeData lightTheme() {
      return ThemeData(
        scaffoldBackgroundColor: grey1,
        bottomAppBarColor: white,
      );
    }
  }

和内部 main 这样的主题对象这样:

      ThemeData theme = AppTheme.lightTheme();
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: theme,
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }

以下是结果:

Change your code for ThemeData like this:

  import 'package:flutter/material.dart';

  class AppTheme {
    static const Color white = Colors.white,
        white2 = Color(0x00fefefe),
        grey1 = Color(0xffe5e5e5),
        grey3 = Color(0x004b4a5a),
        grey4 = Color(0x00AFBEC4),
        black = Colors.black,
        purple = Color(0x007154b8);
    static const TextStyle s1 = TextStyle(
          color: black,
          fontSize: 16,
          fontWeight: FontWeight.w600,
          height: 2.4,
        ),
        s2 = TextStyle(
          color: black,
          fontSize: 14,
          fontWeight: FontWeight.w600,
          height: 2.1,
        ),
        s3 = TextStyle(
          color: grey3,
          fontSize: 12,
          height: 1.8,
          fontWeight: FontWeight.w500,
        ),
        s4 = TextStyle(
          fontSize: 10,
          color: purple,
          height: 1.5,
          fontWeight: FontWeight.w500,
        );

    static ThemeData lightTheme() {
      return ThemeData(
        scaffoldBackgroundColor: grey1,
        bottomAppBarColor: white,
      );
    }
  }

And inside Main define the theme object like this:

      ThemeData theme = AppTheme.lightTheme();
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: theme,
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }

Here is the result:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文