toColor property

Color? get toColor

Returns the color value of this string.

Example: '#FF0000'.toColor -> Color(0xFFFF0000).

Implementation

Color? get toColor {
  if (isBlank) return null;
  var hex = trim();
  if (hex.startsWith('#')) hex = hex.substring(1);
  if (hex.startsWith('0x')) hex = hex.substring(2);
  if (!RegExp(r'^[0-9a-fA-F]{6}$|^[0-9a-fA-F]{8}$').hasMatch(hex)) {
    return null;
  }
  if (hex.length == 6) hex = 'FF$hex';
  return Color(.parse(hex, radix: 16));
}