getColorFromHex function
Returns Color from hex String.
Color color = getColorFromHex('#E5E5E5');
returns default color if not able to parse given hex
Implementation
Color getColorFromHex(String hexColor, {Color? defaultColor}) {
if (hexColor.validate().isEmpty) {
if (defaultColor != null) {
return defaultColor;
} else {
throw ArgumentError('Can not parse provided hex $hexColor');
}
}
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return Color(int.parse(hexColor, radix: 16));
}