viewport_offset.dart 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';

/// The direction of a scroll, relative to the positive scroll offset axis given
/// by an [AxisDirection] and a [GrowthDirection].
///
/// This contrasts to [GrowthDirection] in that it has a third value, [idle],
/// for the case where no scroll is occurring.
///
13 14 15
/// This is used by [RenderSliverFloatingPersistentHeader] to only expand when
/// the user is scrolling in the same direction as the detected scroll offset
/// change.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
enum ScrollDirection {
  /// No scrolling is underway.
  idle,

  /// Scrolling is happening in the positive scroll offset direction.
  ///
  /// For example, for the [GrowthDirection.forward] part of a vertical
  /// [AxisDirection.down] list, this means the content is moving up, exposing
  /// lower content.
  forward,

  /// Scrolling is happening in the negative scroll offset direction.
  ///
  /// For example, for the [GrowthDirection.forward] part of a vertical
  /// [AxisDirection.down] list, this means the content is moving down, exposing
  /// earlier content.
  reverse,
}

35 36
/// Returns the opposite of the given [ScrollDirection].
///
37
/// Specifically, returns [ScrollDirection.reverse] for [ScrollDirection.forward]
38 39 40 41 42 43 44 45 46 47 48 49 50 51
/// (and vice versa) and returns [ScrollDirection.idle] for
/// [ScrollDirection.idle].
ScrollDirection flipScrollDirection(ScrollDirection direction) {
  switch (direction) {
    case ScrollDirection.idle:
      return ScrollDirection.idle;
    case ScrollDirection.forward:
      return ScrollDirection.reverse;
    case ScrollDirection.reverse:
      return ScrollDirection.forward;
  }
  return null;
}

52 53 54 55 56 57
/// Which part of the content inside the viewport should be visible.
///
/// The [pixels] value determines the scroll offset that the viewport uses to
/// select which part of its content to display. As the user scrolls the
/// viewport, this value changes, which changes the content that is displayed.
///
58
/// This object is a [Listenable] that notifies its listeners when [pixels]
59
/// changes.
60 61 62 63 64 65
///
/// See also:
///
///  * [ScrollPosition], which is a commonly used concrete subclass.
///  * [RenderViewportBase], which is a render object that uses viewport
///    offsets.
66
abstract class ViewportOffset extends ChangeNotifier {
67 68 69
  /// Default constructor.
  ///
  /// Allows subclasses to construct this object directly.
70
  ViewportOffset();
71 72 73 74 75

  /// Creates a viewport offset with the given [pixels] value.
  ///
  /// The [pixels] value does not change unless the viewport issues a
  /// correction.
76
  factory ViewportOffset.fixed(double value) = _FixedViewportOffset;
77 78 79 80 81

  /// Creates a viewport offset with a [pixels] value of 0.0.
  ///
  /// The [pixels] value does not change unless the viewport issues a
  /// correction.
82 83 84 85 86 87 88
  factory ViewportOffset.zero() = _FixedViewportOffset.zero;

  /// The number of pixels to offset the children in the opposite of the axis direction.
  ///
  /// For example, if the axis direction is down, then the pixel value
  /// represents the number of logical pixels to move the children _up_ the
  /// screen. Similarly, if the axis direction is left, then the pixels value
89
  /// represents the number of logical pixels to move the children to _right_.
90 91 92
  ///
  /// This object notifies its listeners when this value changes (except when
  /// the value changes due to [correctBy]).
93 94 95 96
  double get pixels;

  /// Called when the viewport's extents are established.
  ///
Adam Barth's avatar
Adam Barth committed
97
  /// The argument is the dimension of the [RenderViewport] in the main axis
98 99 100
  /// (e.g. the height, for a vertical viewport).
  ///
  /// This may be called redundantly, with the same value, each frame. This is
Adam Barth's avatar
Adam Barth committed
101
  /// called during layout for the [RenderViewport]. If the viewport is
102 103 104 105 106 107 108 109
  /// configured to shrink-wrap its contents, it may be called several times,
  /// since the layout is repeated each time the scroll offset is corrected.
  ///
  /// If this is called, it is called before [applyContentDimensions]. If this
  /// is called, [applyContentDimensions] will be called soon afterwards in the
  /// same layout phase. If the viewport is not configured to shrink-wrap its
  /// contents, then this will only be called when the viewport recomputes its
  /// size (i.e. when its parent lays out), and not during normal scrolling.
110
  ///
111
  /// If applying the viewport dimensions changes the scroll offset, return
Adam Barth's avatar
Adam Barth committed
112
  /// false. Otherwise, return true. If you return false, the [RenderViewport]
113 114 115 116 117 118
  /// will be laid out again with the new scroll offset. This is expensive. (The
  /// return value is answering the question "did you accept these viewport
  /// dimensions unconditionally?"; if the new dimensions change the
  /// [ViewportOffset]'s actual [pixels] value, then the viewport will need to
  /// be laid out again.)
  bool applyViewportDimension(double viewportDimension);
119 120 121 122 123 124 125 126 127 128 129 130 131 132

  /// Called when the viewport's content extents are established.
  ///
  /// The arguments are the minimum and maximum scroll extents respectively. The
  /// minimum will be equal to or less than zero, the maximum will be equal to
  /// or greater than zero.
  ///
  /// The maximum scroll extent has the viewport dimension subtracted from it.
  /// For instance, if there is 100.0 pixels of scrollable content, and the
  /// viewport is 80.0 pixels high, then the minimum scroll extent will
  /// typically be 0.0 and the maximum scroll extent will typically be 20.0,
  /// because there's only 20.0 pixels of actual scroll slack.
  ///
  /// If applying the content dimensions changes the scroll offset, return
Adam Barth's avatar
Adam Barth committed
133
  /// false. Otherwise, return true. If you return false, the [RenderViewport]
134 135 136 137 138 139
  /// will be laid out again with the new scroll offset. This is expensive. (The
  /// return value is answering the question "did you accept these content
  /// dimensions unconditionally?"; if the new dimensions change the
  /// [ViewportOffset]'s actual [pixels] value, then the viewport will need to
  /// be laid out again.)
  ///
Adam Barth's avatar
Adam Barth committed
140
  /// This is called at least once each time the [RenderViewport] is laid out,
141 142 143 144 145 146 147 148 149
  /// even if the values have not changed. It may be called many times if the
  /// scroll offset is corrected (if this returns false). This is always called
  /// after [applyViewportDimension], if that method is called.
  bool applyContentDimensions(double minScrollExtent, double maxScrollExtent);

  /// Apply a layout-time correction to the scroll offset.
  ///
  /// This method should change the [pixels] value by `correction`, but without
  /// calling [notifyListeners]. It is called during layout by the
Adam Barth's avatar
Adam Barth committed
150
  /// [RenderViewport], before [applyContentDimensions]. After this method is
151 152 153 154
  /// called, the layout will be recomputed and that may result in this method
  /// being called again, though this should be very rare.
  void correctBy(double correction);

155 156 157 158
  /// Jumps the scroll position from its current value to the given value,
  /// without animation, and without checking if the new value is in range.
  void jumpTo(double pixels);

159
  /// The direction in which the user is trying to change [pixels], relative to
Adam Barth's avatar
Adam Barth committed
160
  /// the viewport's [RenderViewport.axisDirection].
161
  ///
162 163 164
  /// If the _user_ is not scrolling, this will return [ScrollDirection.idle]
  /// even if there is (for example) a [ScrollActivity] currently animating the
  /// position.
165
  ///
166 167 168 169 170
  /// This is exposed in [SliverConstraints.userScrollDirection], which is used
  /// by some slivers to determine how to react to a change in scroll offset.
  /// For example, [RenderSliverFloatingPersistentHeader] will only expand a
  /// floating app bar when the [userScrollDirection] is in the positive scroll
  /// offset direction.
171 172 173 174 175 176
  ScrollDirection get userScrollDirection;

  @override
  String toString() {
    final List<String> description = <String>[];
    debugFillDescription(description);
177
    return '${describeIdentity(this)}(${description.join(", ")})';
178 179
  }

180 181 182 183 184 185 186 187 188
  /// Add additional information to the given description for use by [toString].
  ///
  /// This method makes it easier for subclasses to coordinate to provide a
  /// high-quality [toString] implementation. The [toString] implementation on
  /// the [State] base class calls [debugFillDescription] to collect useful
  /// information from subclasses to incorporate into its return value.
  ///
  /// If you override this, make sure to start your method with a call to
  /// `super.debugFillDescription(description)`.
189 190
  @mustCallSuper
  void debugFillDescription(List<String> description) {
191
    description.add('offset: ${pixels?.toStringAsFixed(1)}');
192 193 194 195 196 197 198 199 200 201 202 203 204
  }
}

class _FixedViewportOffset extends ViewportOffset {
  _FixedViewportOffset(this._pixels);
  _FixedViewportOffset.zero() : _pixels = 0.0;

  double _pixels;

  @override
  double get pixels => _pixels;

  @override
205
  bool applyViewportDimension(double viewportDimension) => true;
206 207 208 209 210 211 212 213 214

  @override
  bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) => true;

  @override
  void correctBy(double correction) {
    _pixels += correction;
  }

215 216 217 218 219
  @override
  void jumpTo(double pixels) {
    // Do nothing, viewport is fixed.
  }

220 221 222
  @override
  ScrollDirection get userScrollDirection => ScrollDirection.idle;
}