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

import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';

8
import 'framework.dart';
9
import 'notification_listener.dart';
10
import 'scroll_metrics.dart';
11

12 13 14
/// Mixin for [Notification]s that track how many [RenderAbstractViewport] they
/// have bubbled through.
///
Adam Barth's avatar
Adam Barth committed
15
/// This is used by [ScrollNotification] and [OverscrollIndicatorNotification].
16
mixin ViewportNotificationMixin on Notification {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  /// The number of viewports that this notification has bubbled through.
  ///
  /// Typically listeners only respond to notifications with a [depth] of zero.
  ///
  /// Specifically, this is the number of [Widget]s representing
  /// [RenderAbstractViewport] render objects through which this notification
  /// has bubbled.
  int get depth => _depth;
  int _depth = 0;

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('depth: $depth (${ depth == 0 ? "local" : "remote"})');
  }
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/// A mixin that allows [Element]s containing [Viewport] like widgets to correctly
/// modify the notification depth of a [ViewportNotificationMixin].
///
/// See also:
///   * [Viewport], which creates a custom [MultiChildRenderObjectElement] that mixes
///     this in.
mixin ViewportElementMixin  on NotifiableElementMixin {
  @override
  bool onNotification(Notification notification) {
    if (notification is ViewportNotificationMixin) {
      notification._depth += 1;
    }
    return false;
  }
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63
/// A [Notification] related to scrolling.
///
/// [Scrollable] widgets notify their ancestors about scrolling-related changes.
/// The notifications have the following lifecycle:
///
///  * A [ScrollStartNotification], which indicates that the widget has started
///    scrolling.
///  * Zero or more [ScrollUpdateNotification]s, which indicate that the widget
///    has changed its scroll position, mixed with zero or more
///    [OverscrollNotification]s, which indicate that the widget has not changed
///    its scroll position because the change would have caused its scroll
///    position to go outside its scroll bounds.
///  * Interspersed with the [ScrollUpdateNotification]s and
///    [OverscrollNotification]s are zero or more [UserScrollNotification]s,
64
///    which indicate that the user has changed the direction in which they are
65 66 67
///    scrolling.
///  * A [ScrollEndNotification], which indicates that the widget has stopped
///    scrolling.
68
///  * A [UserScrollNotification], with a [UserScrollNotification.direction] of
69 70 71 72 73 74 75
///    [ScrollDirection.idle].
///
/// Notifications bubble up through the tree, which means a given
/// [NotificationListener] will receive notifications for all descendant
/// [Scrollable] widgets. To focus on notifications from the nearest
/// [Scrollable] descendant, check that the [depth] property of the notification
/// is zero.
76 77 78 79 80 81 82 83 84 85 86 87
///
/// When a scroll notification is received by a [NotificationListener], the
/// listener will have already completed build and layout, and it is therefore
/// too late for that widget to call [State.setState]. Any attempt to adjust the
/// build or layout based on a scroll notification would result in a layout that
/// lagged one frame behind, which is a poor user experience. Scroll
/// notifications are therefore primarily useful for paint effects (since paint
/// happens after layout). The [GlowingOverscrollIndicator] and [Scrollbar]
/// widgets are examples of paint effects that use scroll notifications.
///
/// To drive layout based on the scroll position, consider listening to the
/// [ScrollPosition] directly (or indirectly via a [ScrollController]).
Adam Barth's avatar
Adam Barth committed
88
abstract class ScrollNotification extends LayoutChangedNotification with ViewportNotificationMixin {
89
  /// Initializes fields for subclasses.
Adam Barth's avatar
Adam Barth committed
90
  ScrollNotification({
91 92
    required this.metrics,
    required this.context,
93
  });
94

95
  /// A description of a [Scrollable]'s contents, useful for modeling the state
96
  /// of its viewport.
97
  final ScrollMetrics metrics;
98

99
  /// The build context of the widget that fired this notification.
100 101 102
  ///
  /// This can be used to find the scrollable's render objects to determine the
  /// size of the viewport, for instance.
103
  final BuildContext? context;
104 105 106 107

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
108
    description.add('$metrics');
109 110 111
  }
}

112 113 114 115 116 117
/// A notification that a [Scrollable] widget has started scrolling.
///
/// See also:
///
///  * [ScrollEndNotification], which indicates that scrolling has stopped.
///  * [ScrollNotification], which describes the notification lifecycle.
Adam Barth's avatar
Adam Barth committed
118
class ScrollStartNotification extends ScrollNotification {
119
  /// Creates a notification that a [Scrollable] widget has started scrolling.
120
  ScrollStartNotification({
121 122
    required super.metrics,
    required super.context,
123
    this.dragDetails,
124
  });
125

126 127 128 129
  /// If the [Scrollable] started scrolling because of a drag, the details about
  /// that drag start.
  ///
  /// Otherwise, null.
130
  final DragStartDetails? dragDetails;
131 132 133 134 135 136 137 138 139

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    if (dragDetails != null)
      description.add('$dragDetails');
  }
}

140 141 142 143 144 145 146 147
/// A notification that a [Scrollable] widget has changed its scroll position.
///
/// See also:
///
///  * [OverscrollNotification], which indicates that a [Scrollable] widget
///    has not changed its scroll position because the change would have caused
///    its scroll position to go outside its scroll bounds.
///  * [ScrollNotification], which describes the notification lifecycle.
Adam Barth's avatar
Adam Barth committed
148
class ScrollUpdateNotification extends ScrollNotification {
149 150
  /// Creates a notification that a [Scrollable] widget has changed its scroll
  /// position.
151
  ScrollUpdateNotification({
152 153
    required super.metrics,
    required BuildContext super.context,
154 155
    this.dragDetails,
    this.scrollDelta,
156
    int? depth,
157
  }) {
158 159 160 161
    if (depth != null) {
      _depth = depth;
    }
  }
162

163 164 165 166
  /// If the [Scrollable] changed its scroll position because of a drag, the
  /// details about that drag update.
  ///
  /// Otherwise, null.
167
  final DragUpdateDetails? dragDetails;
168

Adam Barth's avatar
Adam Barth committed
169
  /// The distance by which the [Scrollable] was scrolled, in logical pixels.
170
  final double? scrollDelta;
171 172 173 174 175 176 177 178 179 180

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('scrollDelta: $scrollDelta');
    if (dragDetails != null)
      description.add('$dragDetails');
  }
}

181 182 183 184 185 186 187 188 189
/// A notification that a [Scrollable] widget has not changed its scroll position
/// because the change would have caused its scroll position to go outside of
/// its scroll bounds.
///
/// See also:
///
///  * [ScrollUpdateNotification], which indicates that a [Scrollable] widget
///    has changed its scroll position.
///  * [ScrollNotification], which describes the notification lifecycle.
Adam Barth's avatar
Adam Barth committed
190
class OverscrollNotification extends ScrollNotification {
191 192
  /// Creates a notification that a [Scrollable] widget has changed its scroll
  /// position outside of its scroll bounds.
193
  OverscrollNotification({
194 195
    required super.metrics,
    required BuildContext super.context,
196
    this.dragDetails,
197
    required this.overscroll,
198
    this.velocity = 0.0,
199 200 201
  }) : assert(overscroll != null),
       assert(overscroll.isFinite),
       assert(overscroll != 0.0),
202
       assert(velocity != null);
203

204 205 206 207
  /// If the [Scrollable] overscrolled because of a drag, the details about that
  /// drag update.
  ///
  /// Otherwise, null.
208
  final DragUpdateDetails? dragDetails;
209

Adam Barth's avatar
Adam Barth committed
210
  /// The number of logical pixels that the [Scrollable] avoided scrolling.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  ///
  /// This will be negative for overscroll on the "start" side and positive for
  /// overscroll on the "end" side.
  final double overscroll;

  /// The velocity at which the [ScrollPosition] was changing when this
  /// overscroll happened.
  ///
  /// This will typically be 0.0 for touch-driven overscrolls, and positive
  /// for overscrolls that happened from a [BallisticScrollActivity] or
  /// [DrivenScrollActivity].
  final double velocity;

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('overscroll: ${overscroll.toStringAsFixed(1)}');
    description.add('velocity: ${velocity.toStringAsFixed(1)}');
    if (dragDetails != null)
      description.add('$dragDetails');
  }
}

234 235 236 237 238 239
/// A notification that a [Scrollable] widget has stopped scrolling.
///
/// See also:
///
///  * [ScrollStartNotification], which indicates that scrolling has started.
///  * [ScrollNotification], which describes the notification lifecycle.
Adam Barth's avatar
Adam Barth committed
240
class ScrollEndNotification extends ScrollNotification {
241
  /// Creates a notification that a [Scrollable] widget has stopped scrolling.
242
  ScrollEndNotification({
243 244
    required super.metrics,
    required BuildContext super.context,
245
    this.dragDetails,
246
  });
247

248 249 250 251 252 253 254 255
  /// If the [Scrollable] stopped scrolling because of a drag, the details about
  /// that drag end.
  ///
  /// Otherwise, null.
  ///
  /// If a drag ends with some residual velocity, a typical [ScrollPhysics] will
  /// start a ballistic scroll, which delays the [ScrollEndNotification] until
  /// the ballistic simulation completes, at which time [dragDetails] will
256
  /// be null. If the residual velocity is too small to trigger ballistic
257 258
  /// scrolling, then the [ScrollEndNotification] will be dispatched immediately
  /// and [dragDetails] will be non-null.
259
  final DragEndDetails? dragDetails;
260 261 262 263 264 265 266 267 268

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    if (dragDetails != null)
      description.add('$dragDetails');
  }
}

269 270 271 272 273 274
/// A notification that the user has changed the direction in which they are
/// scrolling.
///
/// See also:
///
///  * [ScrollNotification], which describes the notification lifecycle.
Adam Barth's avatar
Adam Barth committed
275
class UserScrollNotification extends ScrollNotification {
276 277
  /// Creates a notification that the user has changed the direction in which
  /// they are scrolling.
278
  UserScrollNotification({
279 280
    required super.metrics,
    required BuildContext super.context,
281
    required this.direction,
282
  });
283

284
  /// The direction in which the user is scrolling.
285 286 287 288 289 290 291 292
  final ScrollDirection direction;

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('direction: $direction');
  }
}
293 294 295

/// A predicate for [ScrollNotification], used to customize widgets that
/// listen to notifications from their children.
296
typedef ScrollNotificationPredicate = bool Function(ScrollNotification notification);
297

298
/// A [ScrollNotificationPredicate] that checks whether
299
/// `notification.depth == 0`, which means that the notification did not bubble
300 301 302 303
/// through any intervening scrolling widgets.
bool defaultScrollNotificationPredicate(ScrollNotification notification) {
  return notification.depth == 0;
}