decodeAlignment static method

Alignment? decodeAlignment(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the given value to an Alignment. If the given value is a Map then this expects the following JSON structure:

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

If the value is a String then supported values are:

  • bottomCenter
  • bottomLeft
  • bottomRight
  • center
  • centerLeft
  • centerRight
  • topCenter
  • topLeft
  • topRight

Implementation

static Alignment? decodeAlignment(
  dynamic value, {
  bool validate = true,
}) {
  Alignment? result;

  if (value is Alignment) {
    result = value;
  } else if (value is Map) {
    assert(SchemaValidator.validate(
      schemaId: '$_baseSchemaUrl/alignment',
      value: value,
      validate: validate,
    ));

    result = Alignment(
      JsonClass.parseDouble(value['x']) ?? 0.0,
      JsonClass.parseDouble(value['y']) ?? 0.0,
    );
  } else {
    _checkSupported(
      'Alignment',
      [
        'bottomCenter',
        'bottomLeft',
        'bottomRight',
        'center',
        'centerLeft',
        'centerRight',
        'topCenter',
        'topLeft',
        'topRight',
      ],
      value,
    );

    if (value != null) {
      assert(SchemaValidator.validate(
        schemaId: '$_baseSchemaUrl/alignment',
        value: value,
        validate: validate,
      ));
      switch (value) {
        case 'bottomCenter':
          result = Alignment.bottomCenter;
          break;
        case 'bottomLeft':
          result = Alignment.bottomLeft;
          break;
        case 'bottomRight':
          result = Alignment.bottomRight;
          break;
        case 'center':
          result = Alignment.center;
          break;
        case 'centerLeft':
          result = Alignment.centerLeft;
          break;
        case 'centerRight':
          result = Alignment.centerRight;
          break;
        case 'topCenter':
          result = Alignment.topCenter;
          break;
        case 'topLeft':
          result = Alignment.topLeft;
          break;
        case 'topRight':
          result = Alignment.topRight;
          break;
      }
    }
  }
  return result;
}