toColor method
Convert to color from hex string. Returns null if the string is not a valid hex color.
Implementation
Color? toColor() {
if (this == null) {
throw ArgumentError('string: $this');
}
if (this!.isEmpty) {
return null;
}
if (!this!.isHexColor()) {
return null;
}
var hexColor = this!.replaceAll('#', '');
if (hexColor.length == 3) {
hexColor = hexColor.split('').map((e) => '$e$e').join();
}
return Color(int.parse('FF$hexColor', radix: 16));
}