decodeEdgeInsetsGeometry static method

EdgeInsetsGeometry? decodeEdgeInsetsGeometry(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the value into an EdgeInsetsGeometry.

If the value is a String, double, or int then this will parse the number and pass it to EdgeInsets.all.

If the value is an array with two entities, this call EdgeInsets.symmetric with the first element passed as the horizontal and the second as the vertical.

If the value is an array with four entities, this call EdgeInsets.fromLTRB passing each element in order.

Finally, this may be a Map-like structure in the following JSON format:

{
  "bottom": <double>,
  "left": <double>,
  "right": <double>,
  "top": <double>
}

Implementation

static EdgeInsetsGeometry? decodeEdgeInsetsGeometry(
  dynamic value, {
  bool validate = true,
}) {
  EdgeInsetsGeometry? result;

  if (value is EdgeInsetsGeometry) {
    result = value;
  } else if (value != null) {
    if (value is String || value is double || value is int) {
      result = EdgeInsets.all(JsonClass.parseDouble(value)!);
    } else if (value is List) {
      assert(value.length == 2 || value.length == 4);
      if (value.length == 2) {
        result = EdgeInsets.symmetric(
          horizontal: JsonClass.parseDouble(value[0], 0)!,
          vertical: JsonClass.parseDouble(value[1], 0)!,
        );
      } else if (value.length == 4) {
        result = EdgeInsets.fromLTRB(
          JsonClass.parseDouble(value[0])!,
          JsonClass.parseDouble(value[1])!,
          JsonClass.parseDouble(value[2])!,
          JsonClass.parseDouble(value[3])!,
        );
      }
    } else {
      assert(SchemaValidator.validate(
        schemaId: '$_baseSchemaUrl/edge_insets_geometry',
        value: value,
        validate: validate,
      ));
      result = EdgeInsets.only(
        bottom: JsonClass.parseDouble(value['bottom'], 0.0)!,
        left: JsonClass.parseDouble(value['left'], 0.0)!,
        right: JsonClass.parseDouble(value['right'], 0.0)!,
        top: JsonClass.parseDouble(value['top'], 0.0)!,
      );
    }
  }

  return result;
}