decodeMaterialColor static method

MaterialColor? decodeMaterialColor(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the given value to an MaterialColor. This expects the given value to be of the following structure:

{
  "primary": "<Color>",
  "swatches": "<Map<String, Color>"
}

See also:

Implementation

static MaterialColor? decodeMaterialColor(
  dynamic value, {
  bool validate = true,
}) {
  MaterialColor? result;

  if (value is MaterialColor) {
    result = value;
  } else if (value != null) {
    assert(SchemaValidator.validate(
      schemaId: '$_baseSchemaUrl/material_color',
      value: value,
      validate: validate,
    ));
    final swatches = <int, Color>{};

    final swatchesIn = value['swatches'];
    swatchesIn.forEach(
      (key, value) => swatches[JsonClass.maybeParseInt(key)!] = decodeColor(
        value,
        validate: false,
      )!,
    );

    result = MaterialColor(
      decodeColor(
        value['primary'],
        validate: false,
      )!
          .value,
      swatches,
    );
  }

  return result;
}