Flutter 替代 accentColor

Flutter Substitute for accentColor

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

我试图搜索解决方案,我找到了两个,一个在 Whosebug 的 Flutter docs and other on 中。我尝试了两种解决方案,但仍然出现相同的错误

错误信息:

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

我实际上找到了一个临时解决方案。 问题是一个可能的空值,所以代码不起作用。该解决方案基本上是在 ColorScheme 之后放置一个 ?。 例如:

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

如果还是不行,请尝试在配色方案中添加亮度,例如:

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