设置亮度时断言失败:Brightness.dark for darkTheme

u2t*_*all 13 flutter flutter-theme

我收到此错误:

'package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.
Run Code Online (Sandbox Code Playgroud)

我在黑暗模式下使用了这个Brightness: 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,
         ),
Run Code Online (Sandbox Code Playgroud)

BJW*_*BJW 26

这是在 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),
          ),
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!你是我的英雄 (3认同)