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 one of the following JSON formats:

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

Implementation

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

  if (value is String && value.contains(',')) {
    value = value.split(',');
  }

  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);
      // LR,TB
      if (value.length == 1) {
        result = EdgeInsets.all(
          JsonClass.maybeParseDouble(value[0]) ?? 0.0,
        );
      }
      // LR,TB
      else if (value.length == 2) {
        result = EdgeInsets.symmetric(
          horizontal: JsonClass.maybeParseDouble(value[0]) ?? 0.0,
          vertical: JsonClass.maybeParseDouble(value[1]) ?? 0.0,
        );
      }
      // T,LR,B
      else if (value.length == 3) {
        result = EdgeInsets.fromLTRB(
          JsonClass.maybeParseDouble(value[1]) ?? 0.0,
          JsonClass.maybeParseDouble(value[0]) ?? 0.0,
          JsonClass.maybeParseDouble(value[1]) ?? 0.0,
          JsonClass.maybeParseDouble(value[2]) ?? 0.0,
        );
      }
      // L,T,R,B
      else if (value.length == 4) {
        result = EdgeInsets.fromLTRB(
          JsonClass.maybeParseDouble(value[0]) ?? 0.0,
          JsonClass.maybeParseDouble(value[1]) ?? 0.0,
          JsonClass.maybeParseDouble(value[2]) ?? 0.0,
          JsonClass.maybeParseDouble(value[3]) ?? 0.0,
        );
      }
    } else {
      assert(SchemaValidator.validate(
        schemaId: '$_baseSchemaUrl/edge_insets_geometry',
        value: value,
        validate: validate,
      ));
      final end = JsonClass.maybeParseDouble(value['end']);
      final start = JsonClass.maybeParseDouble(value['start']);

      if (start != null || end != null) {
        result = EdgeInsetsDirectional.only(
          bottom: JsonClass.maybeParseDouble(value['bottom']) ?? 0.0,
          end: end ?? 0.0,
          start: start ?? 0.0,
          top: JsonClass.maybeParseDouble(value['top']) ?? 0.0,
        );
      } else {
        result = EdgeInsets.only(
          bottom: JsonClass.maybeParseDouble(value['bottom']) ?? 0.0,
          left: JsonClass.maybeParseDouble(value['left']) ?? 0.0,
          right: JsonClass.maybeParseDouble(value['right']) ?? 0.0,
          top: JsonClass.maybeParseDouble(value['top']) ?? 0.0,
        );
      }
    }
  }

  return result;
}