scroll_configuration.dart 4.47 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.linux:
39
      case TargetPlatform.macOS:
40
      case TargetPlatform.windows:
41 42 43
        return child;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
44
        return GlowingOverscrollIndicator(
45 46
          child: child,
          axisDirection: axisDirection,
47
          color: _kDefaultGlowColor,
48 49 50 51 52
        );
    }
    return null;
  }

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

Adam Barth's avatar
Adam Barth committed
71 72 73 74 75 76 77 78 79 80
  /// 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.
81
  bool shouldNotify(covariant ScrollBehavior oldDelegate) => false;
82 83

  @override
84
  String toString() => objectRuntimeType(this, 'ScrollBehavior');
85 86
}

87 88 89
/// Controls how [Scrollable] widgets behave in a subtree.
///
/// The scroll configuration determines the [ScrollPhysics] and viewport
90
/// decorations used by descendants of [child].
Adam Barth's avatar
Adam Barth committed
91
class ScrollConfiguration extends InheritedWidget {
92 93 94
  /// 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
95
  const ScrollConfiguration({
96 97 98 99 100
    Key key,
    @required this.behavior,
    @required Widget child,
  }) : super(key: key, child: child);

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

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

  @override
Adam Barth's avatar
Adam Barth committed
114
  bool updateShouldNotify(ScrollConfiguration oldWidget) {
115
    assert(behavior != null);
Adam Barth's avatar
Adam Barth committed
116 117
    return behavior.runtimeType != oldWidget.behavior.runtimeType
        || (behavior != oldWidget.behavior && behavior.shouldNotify(oldWidget.behavior));
118
  }
119 120

  @override
121 122
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
123
    properties.add(DiagnosticsProperty<ScrollBehavior>('behavior', behavior));
124
  }
125
}