decodeBorderRadius static method

BorderRadius? decodeBorderRadius(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the given value to a BorderRadius. The value may be a String, an int, a double, or a Map-like object.

When the value is a String, int, or double then the value will be parsed via JsonClass.parseDouble and the result will be passed to BorderRadius.circular.

If the value is a Map-like object then the expected structure depends on on the value passed in for the "type" attribute. The expected "type" values must be one of:

  • all
  • circular
  • horizontal
  • only
  • vertical

Type: all expects a structure:

{
  "radius": <Radius>,
  "type": "all"
}

Type: circular expects a structure:

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

Type: horizontal expects a structure:

{
  "left": <Radius>,
  "right": <Radius>,
  "type": "horizontal"
}

Type: only expects a structure:

{
  "bottomLeft": <Radius>,
  "bottomRight": <Radius>,
  "topLeft": <Radius>,
  "topRight": <Radius>,
  "type": "only"
}

Type: vertical expects a structure:

{
  "bottom": <Radius>,
  "top": <Radius>,
  "type": "vertical"
}

See also:

Implementation

static BorderRadius? decodeBorderRadius(
  dynamic value, {
  bool validate = true,
}) {
  BorderRadius? result;

  if (value is BorderRadius) {
    result = value;
  } else {
    var radius = JsonClass.parseDouble(value);
    if (radius != null) {
      result = BorderRadius.circular(radius);
    } else {
      assert(value == null || value['type'] is String);
      _checkSupported(
        'BorderRadius',
        [
          'all',
          'circular',
          'horizontal',
          'only',
          'vertical',
        ],
        value == null ? null : value['type'],
      );

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

        switch (type) {
          case 'all':
            result = BorderRadius.all(
                decodeRadius(value['radius']) ?? Radius.zero);
            break;
          case 'circular':
            result = BorderRadius.circular(
              JsonClass.parseDouble(value['radius'])!,
            );
            break;
          case 'horizontal':
            result = BorderRadius.horizontal(
              left: decodeRadius(
                    value['left'],
                    validate: false,
                  ) ??
                  Radius.zero,
              right: decodeRadius(
                    value['right'],
                    validate: false,
                  ) ??
                  Radius.zero,
            );
            break;
          case 'only':
            result = BorderRadius.only(
              bottomLeft: decodeRadius(
                    value['bottomLeft'],
                    validate: false,
                  ) ??
                  Radius.zero,
              bottomRight: decodeRadius(
                    value['bottomRight'],
                    validate: false,
                  ) ??
                  Radius.zero,
              topLeft: decodeRadius(
                    value['topLeft'],
                    validate: false,
                  ) ??
                  Radius.zero,
              topRight: decodeRadius(
                    value['topRight'],
                    validate: false,
                  ) ??
                  Radius.zero,
            );
            break;
          case 'vertical':
            result = BorderRadius.vertical(
              bottom: decodeRadius(
                    value['bottom'],
                    validate: false,
                  ) ??
                  Radius.zero,
              top: decodeRadius(
                    value['top'],
                    validate: false,
                  ) ??
                  Radius.zero,
            );
            break;
        }
      }
    }
  }

  return result;
}