我如何转换一个十六进制的颜色字符串像#b74093在扑动的颜色?

我想在Dart中使用HEX颜色代码。


当前回答

最好的方法是使用来自pub.dev的flutter十六进制颜色插件 然后导入包。

import 'package:hexcolor/hexcolor.dart';

然后把它用在这种特殊的方式上。

Text( 
     'Running on: $_platformVersion\n',
      style: TextStyle(color: HexColor("#f2f2f2")),
    ),
Text(
      "Hex From Material  $textColor",
       style: TextStyle(color: ColorToHex(Colors.teal)),
    ),

这里的特殊插件。

其他回答

如果您想使用格式为#123456的颜色的十六进制代码,那么很容易做到。创建一个类型为Color的变量,并将以下值赋给它。

Color myHexColor = Color(0xff123456)

// Here you notice I use the 0xff and that is the opacity or transparency
// of the color and you can also change these values.

使用myHexColor,你就可以开始了。

如果您想直接从十六进制代码更改颜色的不透明度,则将0xff中的ff值更改为下表中相应的值。(或者你也可以使用

myHexColor.withOpacity(0.2)

这是比较简单的方法。0.2是平均20%不透明度)

十六进制的不透明度值

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

使用hexcolor将十六进制颜色带入Dart hexcolorPlugin:

hexcolor: ^2.0.3

示例使用

import 'package:hexcolor/hexcolor.dart';
Container(
    decoration: new BoxDecoration(
        color: Hexcolor('#34cc89'),
    ),
    child: Center(
        child: Text(
            'Running on: $_platformVersion\n',
            style: TextStyle(color: Hexcolor("#f2f2f2")),
        ),
    ),
),

在fromRGB构造函数中使用十六进制数:

Color.fromRGBO(0xb7, 0x40, 0x93, 1),

最好的方法是使用来自pub.dev的flutter十六进制颜色插件 然后导入包。

import 'package:hexcolor/hexcolor.dart';

然后把它用在这种特殊的方式上。

Text( 
     'Running on: $_platformVersion\n',
      style: TextStyle(color: HexColor("#f2f2f2")),
    ),
Text(
      "Hex From Material  $textColor",
       style: TextStyle(color: ColorToHex(Colors.teal)),
    ),

这里的特殊插件。

//调用这一行来设置颜色 颜色:HexColor (HexColor.Primarycolor)

我已经创建了一个类HexColor和玷污所有的颜色在这个类。这是100%工作的代码

class HexColor extends Color {
    static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");

      if (hexColor.length == 6) {
       hexColor = "FF" + hexColor;
       }

    return int.parse(hexColor, radix: 16);
   }

 static var Primarycolor="FF3E3F";

  static var Accentcolor="b74093";

  static var white="b74093";

static var black="b74093";

HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}