无法创建自定义颜色表

时间:2018-09-20 16:07:47

标签: flutter

我正在尝试为应用程序使用自定义颜色主题。

这是main.dart文件

import 'package:flutter/material.dart';

import 'pages/HomePage.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: new ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: MaterialColor(0xFF990000, {
          50: Color(0xFF990001),
          300: Color(0xFF990002),
          900: Color(0xFF990003)
        }),
      ),
      home: new HomePage(),
    );
  }
}

我收到以下错误:

颤振:W小工具库引起的异常CA ═══════════════════════ 颤抖:构建MyApp(dirty)时引发以下NoSuchMethodError: flutter:方法'computeLuminance'在null上调用。 颤动:接收者:null 扑:尝试调用:computeLuminance()

我做错了什么?

3 个答案:

答案 0 :(得分:0)

您不能将 MaterialColor 分配给 primaryswatch ,从而感染用于分配不同颜色类型(例如primaryColor,primaryColorLight / Dark,accentColor等)的primaryswatch。

您可以从类似的问题类型中获得更多详细信息。 How do I use the different shades of a color swatch in Flutter?

答案 1 :(得分:0)

如果要使用所有值,则应定义所有值,当我也仅定义一个或两个值时,也会遇到相同的错误。

official documentation中我们发现

  

MaterialColor类   定义一种颜色以及具有十种颜色阴影的色板。

例如:

 primarySwatch: MaterialColor(0xFFFFFFFF, {
  50:  Color(0xFFFFFFFF),
  100: Color(0xFFFFFFFF),
  200: Color(0xFFFFFFFF),
  300: Color(0xFFFFFFFF),
  400: Color(0xFFFFFFFF),
  500: Color(0xFFFFFFFF),
  600: Color(0xFFFFFFFF),
  700: Color(0xFFFFFFFF),
  800: Color(0xFFFFFFFF),
  900: Color(0xFFFFFFFF),
    });

答案 2 :(得分:0)

您需要整体创建一个MaterialColor对象,为十个不同的亮度值提供所有颜色值。请考虑使用此解决方案以获得更好的方法。

Flutter: Creating a custom color swatch for MaterialColor