通过设置亮度获得失败的断言:Brightness.dark for darkTheme

Getting failed assertion with setting brightness: Brightness.dark for darkTheme

我收到这个错误:

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

我已经将此 亮度:Brightness.dark 参数 用于我的黑暗模式,直到最近更新之前没有任何问题。我一次更新了几件事,所以我不确定是什么导致了变化。 我现在需要以不同的方式设置暗模式吗?

当前深色主题:

darkTheme: ThemeData(
           toggleableActiveColor: Colors.blue,
           visualDensity: VisualDensity.adaptivePlatformDensity,
           textTheme: _textTheme(),
           colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue).copyWith(secondary: Colors.blueAccent),
           brightness: Brightness.dark,
         ),

这是在更新 Flutter 时收紧 ThemeData 构造函数的亮度参数和 ColorScheme 的亮度参数的结果。在您的示例中,ColorScheme 的亮度为亮(默认),但 ThemeData 的亮度为暗。

要使 darkTheme 正常工作,您需要删除亮度参数并将其放入 colorScheme,如下所示:

darkTheme: ThemeData(
            toggleableActiveColor: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
            colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue)
                .copyWith(
                    secondary: Colors.blueAccent, brightness: Brightness.dark),
          ),