decodeRadius static method

Radius? decodeRadius(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the given value to a Radius. This can be a String, int, double, or an object. If this is an object, there must be a "type" attribute that is one of:

  • circular
  • elliptical
  • zero

When passed in as a String, int, or double then this will use JsonClass.maybeParseDouble to parse the number to send to Radius.circular.

Otherwise, if this is an object then the structure of the other attributes depends on the "type".

Type: circular

{
  "radius": "<double>",
  "type": "circular"
}

Type: elliptical

{
 "type": "elliptical",
  "x": "<double>",
  "y": "<double>"
}

Type: zero

{
  "type": "zero"
}

Implementation

static Radius? decodeRadius(
  dynamic value, {
  bool validate = true,
}) {
  Radius? result;
  if (value is Radius) {
    result = value;
  } else {
    final radius = JsonClass.maybeParseDouble(value);

    if (radius != null) {
      result = Radius.circular(radius);
    } else {
      assert(value == null || value['type'] is String);
      _checkSupported(
        'Radius.type',
        [
          'circular',
          'elliptical',
          'zero',
        ],
        value == null ? null : value['type'],
      );

      if (value != null) {
        assert(SchemaValidator.validate(
          schemaId: '$_baseSchemaUrl/radius',
          value: value,
          validate: validate,
        ));
        final String? type = value['type'];

        switch (type) {
          case 'circular':
            result =
                Radius.circular(JsonClass.maybeParseDouble(value['radius'])!);
            break;

          case 'elliptical':
            result = Radius.elliptical(
              JsonClass.maybeParseDouble(value['x']) ?? 0.0,
              JsonClass.maybeParseDouble(value['y']) ?? 0.0,
            );
            break;

          case 'zero':
            result = Radius.zero;
            break;
        }
      }
    }
  }

  return result;
}