fromJson method

  1. @override
Color? fromJson(
  1. String? json
)

Implementation

@override
Color? fromJson(String? json) {
  if (json == null) return null;

  var i = 0;

  if (json.startsWith('#') == true) {
    json = json.substring(1);
  }

  if (json.length == 3) {
    json = json.substring(0, 1) +
        json.substring(0, 1) +
        json.substring(1, 2) +
        json.substring(1, 2) +
        json.substring(2, 3) +
        json.substring(2, 3);
  }

  if (json.length == 6 || json.length == 8) {
    i = int.parse(json, radix: 16);

    if (json.length != 8) {
      i = 0xff000000 + i;
    }

    return Color(i);
  }

  throw 'Unsuported_Json_Value';
}