Flutter 替代 AccentColor
我试图为我的应用程序制作一个浅色主题和一个深色主题,我制作了一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上发现了一个临时解决方案。
问题是可能的零值,因此代码不起作用。该解决方案基本上将
?
在Colorscheme之后放置。例如:
如果仍然不起作用,请尝试在配色方案中添加亮度,例如:
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:
if still doesn't work, try to add the brightness in the color scheme, for example: