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

标签 flutter dart colors flutter-layout

我想更改 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

关于flutter - 如何为 flutter 添加自定义颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549539/

相关文章:

flutter - 如何使用 travis CI 配置 flutter

flutter - 如何修复文本小部件填充?

flutter - Flutter中如何使用Column在屏幕中间显示文本?

javascript - HTML 在 Rgb 中获取颜色

java - 如何将 .setColor() 作为 PGM 值

flutter - 如何为 'onEvent' 的事件实现去抖动器?

dart - Flutter 搜索栏布局问题 BoxConstraints 强制无限宽度

google-maps - flutter_google_places 不显示自动完成搜索结果

dart - DART:Canvas:跨浏览器setLineDash(Firefox 18.0.1)

c - BMP 颜色反转功能不起作用