toColor static method

Color? toColor(
  1. String? color
)

Parse a String to a Flutter Color

Works with plaintext colors like red, blue, yellow Works with hex code colors like #FF00000 and with opacity hex #AA0000FF

Implementation

static Color? toColor(String? color) {
  Color? c;
  Map<String, Color> colors = ColorObservable.colors;
  try {
    if (color != null) {
      if ((colors.containsKey(color.toLowerCase()))) {
        c = colors[color.toLowerCase()];
      }
      if (c == null && color.length == 7) {
        c = Color(int.parse(color.substring(1, 7), radix: 16) + 0xFF000000);
      }
      if (c == null && color.length == 9) {
        c = Color(int.parse(color.substring(1, 9), radix: 16) + 0x00000000);
      }
    }
  } catch (e) {
    c = null;
    Log().debug(e.toString(),
        caller: 'helper/string.dart => Color toColor(String color)');
  }
  return c;
}