scroll_configuration.dart 4.24 KB
Newer Older
1
// Copyright 2016 The Chromium 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 38 39 40
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
        return child;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
41
        return GlowingOverscrollIndicator(
42 43
          child: child,
          axisDirection: axisDirection,
44
          color: _kDefaultGlowColor,
45 46 47 48 49
        );
    }
    return null;
  }

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

Adam Barth's avatar
Adam Barth committed
65 66 67 68 69 70 71 72 73 74
  /// 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.
75
  bool shouldNotify(covariant ScrollBehavior oldDelegate) => false;
76 77 78

  @override
  String toString() => '$runtimeType';
79 80
}

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

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

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

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

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