我如何转换一个十六进制的颜色字符串像#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

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

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

可以使用from_css_color包从十六进制字符串中获取Color。它支持三位和六位RGB十六进制表示法。

Color color = fromCSSColor('#ff00aa')

为了优化起见,为每种颜色创建一个Color实例,并将其存储在某个地方以供以后使用。

最简单的方法是将其转换为整数。例如,#BCE6EB。你会添加0xFF,然后你会删除标签,使它:

0 xffbce6eb

然后让我们假设你要通过这样做来实现它:

写成backgroundColor:颜色(0 xffbce6eb)

如果你只能使用十六进制,那么我建议使用Hexcolor包。

简单的扩展基于@Serdar回答https://stackoverflow.com/a/57943307/4899849

extension HexString on String {
  int getHexValue() => int.parse(replaceAll('#', '0xff'));
}

用法:

'#b74093'.getHexValue()