decodeScrollPhysics static method

ScrollPhysics? decodeScrollPhysics(
  1. dynamic value, {
  2. bool validate = true,
})

Decodes the value to a ScrollPhysics. If value is not null then it must contain a property named "type" with one of the following values:

  • always
  • bouncing
  • clamping
  • fixedExtent
  • never
  • page
  • rangeMaintaining

This expects the JSON representation to follow the structure:

{
  "parent": <ScrollPhysics>,
  "type": <String>
}

Implementation

static ScrollPhysics? decodeScrollPhysics(
  dynamic value, {
  bool validate = true,
}) {
  ScrollPhysics? result;
  if (value is ScrollPhysics) {
    result = value;
  } else {
    assert(value == null || value['type'] is String);
    _checkSupported(
      'ScrollPhysics.type',
      [
        'always',
        'bouncing',
        'clamping',
        'fixedExtent',
        'never',
        'page',
        'rangeMaintaining',
      ],
      value == null ? null : value['type'],
    );

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

      switch (type) {
        case 'always':
          result = AlwaysScrollableScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'bouncing':
          result = BouncingScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'clamping':
          result = ClampingScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'fixedExtent':
          result = FixedExtentScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'never':
          result = NeverScrollableScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'page':
          result = PageScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;

        case 'rangeMaintaining':
          result = RangeMaintainingScrollPhysics(
            parent: decodeScrollPhysics(
              value['parent'],
              validate: false,
            ),
          );
          break;
      }
    }
  }

  return result;
}