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

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


当前回答

你可以用这个

Color getColorFromColorCode(String code){
  return Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}

其他回答

使用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")),
        ),
    ),
),

“# b74093”?好吧……

到HEX配方

int getColorHexFromStr(String colorStr)
{
  colorStr = "FF" + colorStr;
  colorStr = colorStr.replaceAll("#", "");
  int val = 0;
  int len = colorStr.length;
  for (int i = 0; i < len; i++) {
    int hexDigit = colorStr.codeUnitAt(i);
    if (hexDigit >= 48 && hexDigit <= 57) {
      val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
    } else if (hexDigit >= 65 && hexDigit <= 70) {
      // A..F
      val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
    } else if (hexDigit >= 97 && hexDigit <= 102) {
      // a..f
      val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
    } else {
      throw new FormatException("An error occurred when converting a color");
    }
  }
  return val;
}

如何在扑动中使用十六进制颜色代码#B74093

只需从十六进制颜色代码中删除#符号,并在color类中添加带有颜色代码的0xFF:

#b74093将在Flutter中变为Color(0xffb74093)

#B74093将在Flutter中变为Color(0xFFB74093)

ff或ff in Color(0xFFB74093)定义不透明度。

十六进制颜色的例子,所有不透明度类型在达特

我使用这个material_color_gen包,它的工作就像一个魅力

material_color_gen: ^2.0.0

使用:

import 'package:material_color_gen/material_color_gen.dart';
primarySwatch: Color(0xFFFF0000).toMaterialColor()

这是一个HexColor的例子:#ff0000 使用0xFF更改#的结果是:0xFFFF0000

官方链接: https://pub.dev/packages/material_color_gen

如果你想使用不透明度的颤振十六进制颜色

只使用。Color(0x800F1D41)表示50%,80表示50%。

详情请点击这里