1.0x开头 例如:0xfffefefe
class ColorUtil {
static Color hexColor(int hex,{double alpha = 1}){
if (alpha < 0){
alpha = 0;
}else if (alpha > 1){
alpha = 1;
}
return Color.fromRGBO((hex & 0xFF0000) >> 16 ,
(hex & 0x00FF00) >> 8,
(hex & 0x0000FF) >> 0,
alpha);
}
}
使用:ColorUtil.hexColor(0xffDfDfDf)
2.#开头 例如:#fefefe
class HexColor extends Color{
static int _getColorfromString(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}
HexColor(String hexColor) : super(_getColorfromString(hexColor));
}
使用:static Color trans_red = HexColor('#4cE62929');
|