Flutter 替代 AccentColor

发布于 2025-01-20 01:59:07 字数 1737 浏览 2 评论 0原文

我试图为我的应用程序制作一个浅色主题和一个深色主题,我制作了一个 flutter 课程,代码有点过时,有些命令在 Flutter 的实际版本中不起作用,特别是 Themedata.accentColor 或只是在上一个 Flutter 版本中已弃用的 accentColor

我尝试寻找解决方案,发现了其中两个,其中一个位于 Flutter 文档 以及 StackOverflow 的另一个问题。我尝试了两种解决方案,但仍然收到相同的错误

错误消息:

Exception has occurred.
_AssertionError ('package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.)

代码:

colorScheme: ColorScheme.fromSwatch().copyWith(
  secondary: Colors.white,
),

我的代码是:

import 'package:flutter/material.dart';

const brightness = Brightness.dark;
Color primaryColor = const Color(0xFF00C569);
Color lightColor = const Color(0xFFFFFFFF);
Color backgroundColor = const Color(0xFFF5F5F5);
Color dangerColor = const Color(0xFFFF0000);

ThemeData darkTheme() {
  return ThemeData(
    brightness: brightness,
    //iconTheme: const IconThemeData(color: Colors.black),
    //textTheme: const TextTheme(
      /*
      bodyText2: TextStyle(color: Colors.red),
      headline1: TextStyle(fontSize: 78),
      headline2: TextStyle(
        color: Colors.black,
        fontSize: 30,
      ),
      button: TextStyle(color: Colors.green),
    ),
    */
    colorScheme: ColorScheme.fromSwatch().copyWith(
      secondary: Colors.white,
    ),
    primaryColor: primaryColor,
  );
}

I trying to make a light theme and a dark theme for my app, I making an course of flutter and the code it's a little out of date and some commands doesn't work in the actual version of Flutter, in special the Themedata.accentColor or just accentColor who is deprecated in the last Flutter version.

I tried to search for a solution and I found two of them, one in the Flutter docs and other on another question of StackOverflow. I tried both of solutions but I still getting the same error

Error message:

Exception has occurred.
_AssertionError ('package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.)

Code:

colorScheme: ColorScheme.fromSwatch().copyWith(
  secondary: Colors.white,
),

My code is:

import 'package:flutter/material.dart';

const brightness = Brightness.dark;
Color primaryColor = const Color(0xFF00C569);
Color lightColor = const Color(0xFFFFFFFF);
Color backgroundColor = const Color(0xFFF5F5F5);
Color dangerColor = const Color(0xFFFF0000);

ThemeData darkTheme() {
  return ThemeData(
    brightness: brightness,
    //iconTheme: const IconThemeData(color: Colors.black),
    //textTheme: const TextTheme(
      /*
      bodyText2: TextStyle(color: Colors.red),
      headline1: TextStyle(fontSize: 78),
      headline2: TextStyle(
        color: Colors.black,
        fontSize: 30,
      ),
      button: TextStyle(color: Colors.green),
    ),
    */
    colorScheme: ColorScheme.fromSwatch().copyWith(
      secondary: Colors.white,
    ),
    primaryColor: primaryColor,
  );
}

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

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

发布评论

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

评论(1

迟到的我 2025-01-27 01:59:07

我实际上发现了一个临时解决方案。
问题是可能的零值,因此代码不起作用。该解决方案基本上将在Colorscheme之后放置。
例如:

colorScheme: ColorScheme?.fromSwatch().copyWith(
      secondary: Colors.white,
    ),

如果仍然不起作用,请尝试在配色方案中添加亮度,例如:

colorScheme: ColorScheme?.fromSwatch().copyWith(
      brightness: Brightness.light,
      secondary: Colors.white,
    ),

I actually discovered a temporary solution for this.
The problem was a possible null value, so the code don't work. The solution basically put a ? after the ColorScheme.
For example:

colorScheme: ColorScheme?.fromSwatch().copyWith(
      secondary: Colors.white,
    ),

if still doesn't work, try to add the brightness in the color scheme, for example:

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