flutter - 如何为 flutter 添加自定义颜色?

我想更改 AppBar 的颜色并为其使用自定义颜色,我尝试了很多选项,但似乎都不起作用。 我有什么遗漏吗?

import 'package:flutter/material.dart';

final ThemeData CompanyThemeData = new ThemeData(
  brightness: Brightness.light,
  primaryColorBrightness: Brightness.light,
  accentColor: CompanyColors.black[500],
  accentColorBrightness: Brightness.light
);
  
class CompanyColors {
  CompanyColors._(); // this basically makes it so you can instantiate this class
 
 static const _blackPrimaryValue = 0xFF000000;

  static const MaterialColor black = const MaterialColor(
    _blackPrimaryValue,
    const <int, Color>{
      50:  const Color(0xFFe0e0e0),
      100: const Color(0xFFb3b3b3),
      200: const Color(0xFF808080),
      300: const Color(0xFF4d4d4d),
      400: const Color(0xFF262626),
      500: const Color(_blackPrimaryValue),
      600: const Color(0xFF000000),
      700: const Color(0xFF000000),
      800: const Color(0xFF000000),
      900: const Color(0xFF000000),
    },
  );
}

然后我在 main.dart 中使用它作为

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:Theme1.CompanyColors.black[50],
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

但在执行后它说

type Color is not of subtype MaterialColor

最佳答案

flutter 基本上使用颜色 AARRGGBB 格式,您可以在下面的颜色代码中使用任何颜色属性,例如:

new Container(color: const Color(0xff2980b9));

AA = 透明度

RR = 红色

GG = 绿色

BB = 蓝色

现在,如果您想从 6 位颜色代码创建自定义颜色 8 位代码,那么只需将透明度 (AA) 值附加到它

透明度百分比 以下是一些示例透明度百分比及其十六进制值

100% - FF
95% - F2
90% - E6
85% - D9
80% - CC
75% - BF
70% - B3
65% - A6
60% - 99
55% - 8C
50% - 80
45% - 73
40% - 66
35% - 59
30% - 4D
25% - 40
20% - 33
15% - 26
10% - 1A
5% - 0D
0% - 00

在我的情况下,我总是使用 AA = ff,因为 6 位颜色具有 ff 透明度。 用于 6 位颜色 best site

https://stackoverflow.com/questions/50549539/

相关文章:

flutter - 单击屏幕上的文本字段/任何位置后,如何在 flutter 上隐藏软输入键盘?

dart - 如何在 Flutter 中调度后台任务?

flutter - 如何在 Dart 中对 Textfield onChange 进行去抖动?

flutter - 在 Flutter 中使用 SafeArea

dart - 将带参数的函数传递给 VoidCallback

mobile - 是否可以在 Linux 虚拟机上使用 Flutter 开发 iOS 应用程序?

dart - Flutter:是否有可能创建 App Widgets (Android) 和 Tod

dart - 从 Flutter 记录大字符串

flutter - Scoped Model、BLoC 模式、StreamBuilder 和 Inh

flutter - 如何在 Flutter 中创建带有圆角的模态底页?