ThemeData primarySwatch属性无法使用我的自定义MaterialColor

时间:2019-12-20 18:15:12

标签: flutter

我正在创建自己的MaterialColor,如下所示:

Map<int, Color> pokeballRedSwatch = {
  50: Color.fromRGBO(238, 21, 21, 0.1),
  100: Color.fromRGBO(238, 21, 21, 0.2),
  200: Color.fromRGBO(238, 21, 21, 0.3),
  300: Color.fromRGBO(238, 21, 21, 0.4),
  400: Color.fromRGBO(238, 21, 21, 0.5),
  500: Color.fromRGBO(238, 21, 21, 0.6),
  600: Color.fromRGBO(238, 21, 21, 0.7),
  700: Color.fromRGBO(238, 21, 21, 0.8),
  800: Color.fromRGBO(238, 21, 21, 0.9),
  900: Color.fromRGBO(238, 21, 21, 1.0),
};
MaterialColor pokeballRed = MaterialColor(0xffee1515, pokeballRedSwatch);

我正在这样使用它:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: pokeballRed,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

我收到此错误:

'package:flutter/src/widgets/title.dart': Failed assertion: line 24 pos 15: 'color != null && color.alpha == 0xFF': is not true.

但是,当我用以下内容替换primarySwatch时:

Map<int, Color> pokeballWhiteSwatch = {
  50: Color.fromRGBO(240, 240, 240, 0.1),
  100: Color.fromRGBO(240, 240, 240, 0.2),
  200: Color.fromRGBO(240, 240, 240, 0.3),
  300: Color.fromRGBO(240, 240, 240, 0.4),
  400: Color.fromRGBO(240, 240, 240, 0.5),
  500: Color.fromRGBO(240, 240, 240, 0.6),
  600: Color.fromRGBO(240, 240, 240, 0.7),
  700: Color.fromRGBO(240, 240, 240, 0.8),
  800: Color.fromRGBO(240, 240, 240, 0.9),
  900: Color.fromRGBO(240, 240, 240, 1.0),
};
MaterialColor pokeballWhite = MaterialColor(0xfff0f0f0, pokeballWhiteSwatch);

运行正常。

据我所知,颜色不为null,两种情况下的alpha字节都应为0xff。有什么想法我会出错,还是应该在github上为Flutter打开问题?

2 个答案:

答案 0 :(得分:0)

据说重建应用程序解决了我的问题。仍然不确定为什么要解决此问题,因为我同时编写了所有这些内容,然后重新加载以进行测试。

答案 1 :(得分:0)

在此行的末尾添加Alpha

    MaterialColor pokeballRed = MaterialColor(0xffee1515, pokeballRedSwatch).withAlpha(255);