我在Android Studio上迈出的第一步是Flutter .我的步骤:

  1. 下载并安装Android Studio
  2. 下载并安装Flutter 翼和省道
  3. 创建Flutter 中的第一个默认应用程序

这是演示应用程序:

enter image description here

在这个应用程序代码的注释中,我们有:

    // 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"

我将Colors.blue更改为Colors.green,热重新加载运行正常,应用程序更改为绿色.但是,当我try 更改到Colors.black时,出现错误:

类型"Color"不是类型"MaterialColor"的子类型.

演示应用的完整代码如下:

import 'package:flutter/material.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',
      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: Colors.black,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: new Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: new Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug paint" (press "p" in the console where you ran
          // "flutter run", or select "Toggle Debug Paint" from the Flutter tool
          // window in IntelliJ) to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

推荐答案

不能使用Colors.black,因为它不是MaterialColorprimarySwatch需要material 调色板.

如果您转到ThemeData的定义,您将看到以下内容:

  ///  * The primary color palette (the [primarySwatch]), chosen from
  ///    one of the swatches defined by the material design spec. This
  ///    should be one of the maps from the [Colors] class that do not
  ///    have "accent" in their name.

例如,Colors.blue的定义是:

  static const MaterialColor blue = MaterialColor(
    _bluePrimaryValue,
    <int, Color>{
       50: Color(0xFFE3F2FD),
      100: Color(0xFFBBDEFB),
      200: Color(0xFF90CAF9),
      300: Color(0xFF64B5F6),
      400: Color(0xFF42A5F5),
      500: Color(_bluePrimaryValue),
      600: Color(0xFF1E88E5),
      700: Color(0xFF1976D2),
      800: Color(0xFF1565C0),
      900: Color(0xFF0D47A1),
    },
  );
  static const int _bluePrimaryValue = 0xFF2196F3;

而 colored颜色 的定义.黑色是:

static const Color black = Color(0xFF000000);

这就是为什么你不能用黑色.因为同样的原因,你不能使用Colors.white.这两种 colored颜色 是上述解释的例外,即使用没有重音的 colored颜色 .

如果您想要黑色,您可以创建自己的调色板:

const MaterialColor primaryBlack = MaterialColor(
  _blackPrimaryValue,
  <int, Color>{
    50: Color(0xFF000000),
    100: Color(0xFF000000),
    200: Color(0xFF000000),
    300: Color(0xFF000000),
    400: Color(0xFF000000),
    500: Color(_blackPrimaryValue),
    600: Color(0xFF000000),
    700: Color(0xFF000000),
    800: Color(0xFF000000),
    900: Color(0xFF000000),
  },
);
const int _blackPrimaryValue = 0xFF000000;

然后用primaryBlack代替 colored颜色 .黑色

您可以在调色板中调整不同的 colored颜色 .

Flutter相关问答推荐

获取屏幕大小的滚动视图与动态大小?

错误:找不到字体功能类型.Ffltter Google_Fonts包错误

如何使用新的Riverpod语法将依赖传递给AsyncNotifier?

我不能通过第5步了解Flutter 翼Firebase教程

Fighter:基于ChangeNotifier状态动态更新AppBar中的图标

Flutter 翼doctor 显示的错误,我似乎不能修复.问题:系统32、Android工具链、Windows 10 SDK

如何在扩展其他小部件时访问Ref

查询满足AND条件的文档的FiRestore

在LinkedIn上分享Fighter和Web的链接会产生不同的结果

如何在flutter中将所有单词的第一个字母大写

GridView 渲染项目之间有微小的间隙

Flutter居中的SizedBox无法居中

当我使用 Confetti Widget 时,Flutter Android 和 IOS 应用程序崩溃

使用 Riverpod 和 copyWith 方法更新数据类中列表中变量的值

flutter 中 // 和 /// 有什么区别

alert 对话框内的 ListView 显示空的透明窗口

如何从 Flutter Slider Widget 中移除发光效果?

扩展磁贴尾随图标在与一个磁贴交互时更新列表中的所有内容.如何只更改展开图块的图标?

无法在Flutter 图像小部件中打开大尺寸(500MB)图像

如何在Flutter中制作带有波浪边框的圆圈来显示图像?