scroll_configuration.dart 4.31 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/rendering.dart';
7

8
import 'framework.dart';
9
import 'overscroll_indicator.dart';
10
import 'scroll_physics.dart';
11

12
const Color _kDefaultGlowColor = Color(0xFFFFFFFF);
13 14 15 16 17

/// Describes how [Scrollable] widgets should behave.
///
/// Used by [ScrollConfiguration] to configure the [Scrollable] widgets in a
/// subtree.
18
@immutable
Adam Barth's avatar
Adam Barth committed
19
class ScrollBehavior {
20
  /// Creates a description of how [Scrollable] widgets should behave.
Adam Barth's avatar
Adam Barth committed
21
  const ScrollBehavior();
22 23 24 25 26 27

  /// The platform whose scroll physics should be implemented.
  ///
  /// Defaults to the current platform.
  TargetPlatform getPlatform(BuildContext context) => defaultTargetPlatform;

28
  /// Wraps the given widget, which scrolls in the given [AxisDirection].
29
  ///
30 31 32
  /// For example, on Android, this method wraps the given widget with a
  /// [GlowingOverscrollIndicator] to provide visual feedback when the user
  /// overscrolls.
33
  Widget buildViewportChrome(BuildContext context, Widget child, AxisDirection axisDirection) {
34 35
    // When modifying this function, consider modifying the implementation in
    // _MaterialScrollBehavior as well.
36 37
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
38
      case TargetPlatform.macOS:
39 40 41
        return child;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
42
        return GlowingOverscrollIndicator(
43 44
          child: child,
          axisDirection: axisDirection,
45
          color: _kDefaultGlowColor,
46 47 48 49 50
        );
    }
    return null;
  }

51
  /// The scroll physics to use for the platform given by [getPlatform].
52
  ///
53 54
  /// Defaults to [BouncingScrollPhysics] on iOS and [ClampingScrollPhysics] on
  /// Android.
55 56 57
  ScrollPhysics getScrollPhysics(BuildContext context) {
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
58
      case TargetPlatform.macOS:
59 60 61 62 63 64 65 66
        return const BouncingScrollPhysics();
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
        return const ClampingScrollPhysics();
    }
    return null;
  }

Adam Barth's avatar
Adam Barth committed
67 68 69 70 71 72 73 74 75 76
  /// Called whenever a [ScrollConfiguration] is rebuilt with a new
  /// [ScrollBehavior] of the same [runtimeType].
  ///
  /// If the new instance represents different information than the old
  /// instance, then the method should return true, otherwise it should return
  /// false.
  ///
  /// If this method returns true, all the widgets that inherit from the
  /// [ScrollConfiguration] will rebuild using the new [ScrollBehavior]. If this
  /// method returns false, the rebuilds might be optimized away.
77
  bool shouldNotify(covariant ScrollBehavior oldDelegate) => false;
78 79 80

  @override
  String toString() => '$runtimeType';
81 82
}

83 84 85
/// Controls how [Scrollable] widgets behave in a subtree.
///
/// The scroll configuration determines the [ScrollPhysics] and viewport
86
/// decorations used by descendants of [child].
Adam Barth's avatar
Adam Barth committed
87
class ScrollConfiguration extends InheritedWidget {
88 89 90
  /// Creates a widget that controls how [Scrollable] widgets behave in a subtree.
  ///
  /// The [behavior] and [child] arguments must not be null.
Adam Barth's avatar
Adam Barth committed
91
  const ScrollConfiguration({
92 93 94 95 96
    Key key,
    @required this.behavior,
    @required Widget child,
  }) : super(key: key, child: child);

97
  /// How [Scrollable] widgets that are descendants of [child] should behave.
Adam Barth's avatar
Adam Barth committed
98
  final ScrollBehavior behavior;
99

100
  /// The [ScrollBehavior] for [Scrollable] widgets in the given [BuildContext].
101 102 103
  ///
  /// If no [ScrollConfiguration] widget is in scope of the given `context`,
  /// a default [ScrollBehavior] instance is returned.
Adam Barth's avatar
Adam Barth committed
104
  static ScrollBehavior of(BuildContext context) {
105
    final ScrollConfiguration configuration = context.dependOnInheritedWidgetOfExactType<ScrollConfiguration>();
Adam Barth's avatar
Adam Barth committed
106
    return configuration?.behavior ?? const ScrollBehavior();
107 108 109
  }

  @override
Adam Barth's avatar
Adam Barth committed
110
  bool updateShouldNotify(ScrollConfiguration oldWidget) {
111
    assert(behavior != null);
Adam Barth's avatar
Adam Barth committed
112 113
    return behavior.runtimeType != oldWidget.behavior.runtimeType
        || (behavior != oldWidget.behavior && behavior.shouldNotify(oldWidget.behavior));
114
  }
115 116

  @override
117 118
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
119
    properties.add(DiagnosticsProperty<ScrollBehavior>('behavior', behavior));
120
  }
121
}