sliver_persistent_header.dart 14.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5 6 7 8 9
// 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';
import 'package:flutter/rendering.dart';

import 'framework.dart';

10
/// Delegate for configuring a [SliverPersistentHeader].
11
abstract class SliverPersistentHeaderDelegate {
Ian Hickson's avatar
Ian Hickson committed
12 13
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
14
  const SliverPersistentHeaderDelegate();
Ian Hickson's avatar
Ian Hickson committed
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  /// The widget to place inside the [SliverPersistentHeader].
  ///
  /// The `context` is the [BuildContext] of the sliver.
  ///
  /// The `shrinkOffset` is a distance from [maxExtent] towards [minExtent]
  /// representing the current amount by which the sliver has been shrunk. When
  /// the `shrinkOffset` is zero, the contents will be rendered with a dimension
  /// of [maxExtent] in the main axis. When `shrinkOffset` equals the difference
  /// between [maxExtent] and [minExtent] (a positive number), the contents will
  /// be rendered with a dimension of [minExtent] in the main axis. The
  /// `shrinkOffset` will always be a positive number in that range.
  ///
  /// The `overlapsContent` argument is true if subsequent slivers (if any) will
  /// be rendered beneath this one, and false if the sliver will not have any
  /// contents below it. Typically this is used to decide whether to draw a
  /// shadow to simulate the sliver being above the contents below it. Typically
  /// this is true when `shrinkOffset` is at its greatest value and false
  /// otherwise, but that is not guaranteed. See [NestedScrollView] for an
  /// example of a case where `overlapsContent`'s value can be unrelated to
  /// `shrinkOffset`.
36 37
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent);

38 39 40 41 42 43 44 45 46
  /// The smallest size to allow the header to reach, when it shrinks at the
  /// start of the viewport.
  ///
  /// This must return a value equal to or less than [maxExtent].
  ///
  /// This value should not change over the lifetime of the delegate. It should
  /// be based entirely on the constructor arguments passed to the delegate. See
  /// [shouldRebuild], which must return true if a new delegate would return a
  /// different value.
47
  double get minExtent;
Ian Hickson's avatar
Ian Hickson committed
48

49 50 51 52 53 54 55 56 57
  /// The size of the header when it is not shrinking at the top of the
  /// viewport.
  ///
  /// This must return a value equal to or greater than [minExtent].
  ///
  /// This value should not change over the lifetime of the delegate. It should
  /// be based entirely on the constructor arguments passed to the delegate. See
  /// [shouldRebuild], which must return true if a new delegate would return a
  /// different value.
Ian Hickson's avatar
Ian Hickson committed
58 59
  double get maxExtent;

60 61 62 63
  /// Specifies how floating headers should animate in and out of view.
  ///
  /// If the value of this property is null, then floating headers will
  /// not animate into place.
64 65 66 67 68
  ///
  /// This is only used for floating headers (those with
  /// [SliverPersistentHeader.floating] set to true).
  ///
  /// Defaults to null.
69
  FloatingHeaderSnapConfiguration get snapConfiguration => null;
70

71 72 73 74 75 76 77 78 79 80 81
  /// Specifies an [AsyncCallback] and offset for execution.
  ///
  /// If the value of this property is null, then callback will not be
  /// triggered.
  ///
  /// This is only used for stretching headers (those with
  /// [SliverAppBar.stretch] set to true).
  ///
  /// Defaults to null.
  OverScrollHeaderStretchConfiguration get stretchConfiguration => null;

82 83 84 85 86 87 88 89 90 91
  /// Whether this delegate is meaningfully different from the old delegate.
  ///
  /// If this returns false, then the header might not be rebuilt, even though
  /// the instance of the delegate changed.
  ///
  /// This must return true if `oldDelegate` and this object would return
  /// different values for [minExtent], [maxExtent], [snapConfiguration], or
  /// would return a meaningfully different widget tree from [build] for the
  /// same arguments.
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate);
Ian Hickson's avatar
Ian Hickson committed
92 93
}

94 95 96 97 98
/// A sliver whose size varies when the sliver is scrolled to the edge
/// of the viewport opposite the sliver's [GrowthDirection].
///
/// In the normal case of a [CustomScrollView] with no centered sliver, this
/// sliver will vary its size when scrolled to the leading edge of the viewport.
99 100 101
///
/// This is the layout primitive that [SliverAppBar] uses for its
/// shrinking/growing effect.
102
class SliverPersistentHeader extends StatelessWidget {
103 104 105 106
  /// Creates a sliver that varies its size when it is scrolled to the start of
  /// a viewport.
  ///
  /// The [delegate], [pinned], and [floating] arguments must not be null.
107
  const SliverPersistentHeader({
Ian Hickson's avatar
Ian Hickson committed
108 109
    Key key,
    @required this.delegate,
110 111
    this.pinned = false,
    this.floating = false,
112 113 114 115
  }) : assert(delegate != null),
       assert(pinned != null),
       assert(floating != null),
       super(key: key);
Ian Hickson's avatar
Ian Hickson committed
116

117 118 119 120 121 122 123 124 125
  /// Configuration for the sliver's layout.
  ///
  /// The delegate provides the following information:
  ///
  ///  * The minimum and maximum dimensions of the sliver.
  ///
  ///  * The builder for generating the widgets of the sliver.
  ///
  ///  * The instructions for snapping the scroll offset, if [floating] is true.
126
  final SliverPersistentHeaderDelegate delegate;
Ian Hickson's avatar
Ian Hickson committed
127

128 129 130 131 132
  /// Whether to stick the header to the start of the viewport once it has
  /// reached its minimum size.
  ///
  /// If this is false, the header will continue scrolling off the screen after
  /// it has shrunk to its minimum extent.
Ian Hickson's avatar
Ian Hickson committed
133 134
  final bool pinned;

135 136 137 138 139 140 141 142
  /// Whether the header should immediately grow again if the user reverses
  /// scroll direction.
  ///
  /// If this is false, the header only grows again once the user reaches the
  /// part of the viewport that contains the sliver.
  ///
  /// The [delegate]'s [SliverPersistentHeaderDelegate.snapConfiguration] is
  /// ignored unless [floating] is true.
Ian Hickson's avatar
Ian Hickson committed
143 144 145 146
  final bool floating;

  @override
  Widget build(BuildContext context) {
147
    if (floating && pinned)
148
      return _SliverFloatingPinnedPersistentHeader(delegate: delegate);
Ian Hickson's avatar
Ian Hickson committed
149
    if (pinned)
150
      return _SliverPinnedPersistentHeader(delegate: delegate);
Ian Hickson's avatar
Ian Hickson committed
151
    if (floating)
152 153
      return _SliverFloatingPersistentHeader(delegate: delegate);
    return _SliverScrollingPersistentHeader(delegate: delegate);
Ian Hickson's avatar
Ian Hickson committed
154 155 156
  }

  @override
157 158
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
159 160 161 162 163 164
    properties.add(
      DiagnosticsProperty<SliverPersistentHeaderDelegate>(
        'delegate',
        delegate,
      )
    );
165 166 167 168
    final List<String> flags = <String>[
      if (pinned) 'pinned',
      if (floating) 'floating',
    ];
Ian Hickson's avatar
Ian Hickson committed
169 170
    if (flags.isEmpty)
      flags.add('normal');
171
    properties.add(IterableProperty<String>('mode', flags));
Ian Hickson's avatar
Ian Hickson committed
172 173 174
  }
}

175 176
class _SliverPersistentHeaderElement extends RenderObjectElement {
  _SliverPersistentHeaderElement(_SliverPersistentHeaderRenderObjectWidget widget) : super(widget);
Ian Hickson's avatar
Ian Hickson committed
177 178

  @override
179
  _SliverPersistentHeaderRenderObjectWidget get widget => super.widget as _SliverPersistentHeaderRenderObjectWidget;
Ian Hickson's avatar
Ian Hickson committed
180 181

  @override
182
  _RenderSliverPersistentHeaderForWidgetsMixin get renderObject => super.renderObject as _RenderSliverPersistentHeaderForWidgetsMixin;
Ian Hickson's avatar
Ian Hickson committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196

  @override
  void mount(Element parent, dynamic newSlot) {
    super.mount(parent, newSlot);
    renderObject._element = this;
  }

  @override
  void unmount() {
    super.unmount();
    renderObject._element = null;
  }

  @override
197 198
  void update(_SliverPersistentHeaderRenderObjectWidget newWidget) {
    final _SliverPersistentHeaderRenderObjectWidget oldWidget = widget;
Ian Hickson's avatar
Ian Hickson committed
199
    super.update(newWidget);
200 201
    final SliverPersistentHeaderDelegate newDelegate = newWidget.delegate;
    final SliverPersistentHeaderDelegate oldDelegate = oldWidget.delegate;
Ian Hickson's avatar
Ian Hickson committed
202 203 204 205 206 207 208
    if (newDelegate != oldDelegate &&
        (newDelegate.runtimeType != oldDelegate.runtimeType || newDelegate.shouldRebuild(oldDelegate)))
      renderObject.triggerRebuild();
  }

  @override
  void performRebuild() {
209
    super.performRebuild();
Ian Hickson's avatar
Ian Hickson committed
210 211 212 213 214
    renderObject.triggerRebuild();
  }

  Element child;

215
  void _build(double shrinkOffset, bool overlapsContent) {
Ian Hickson's avatar
Ian Hickson committed
216
    owner.buildScope(this, () {
217 218 219 220 221 222 223 224 225
      child = updateChild(
        child,
        widget.delegate.build(
          this,
          shrinkOffset,
          overlapsContent
        ),
        null,
      );
Ian Hickson's avatar
Ian Hickson committed
226 227 228 229 230 231 232 233 234 235
    });
  }

  @override
  void forgetChild(Element child) {
    assert(child == this.child);
    this.child = null;
  }

  @override
236
  void insertChildRenderObject(covariant RenderBox child, dynamic slot) {
237
    assert(renderObject.debugValidateChild(child));
Ian Hickson's avatar
Ian Hickson committed
238 239 240 241
    renderObject.child = child;
  }

  @override
242
  void moveChildRenderObject(covariant RenderObject child, dynamic slot) {
Ian Hickson's avatar
Ian Hickson committed
243 244 245 246
    assert(false);
  }

  @override
247
  void removeChildRenderObject(covariant RenderObject child) {
Ian Hickson's avatar
Ian Hickson committed
248 249 250 251 252
    renderObject.child = null;
  }

  @override
  void visitChildren(ElementVisitor visitor) {
253 254
    if (child != null)
      visitor(child);
Ian Hickson's avatar
Ian Hickson committed
255 256 257
  }
}

258
abstract class _SliverPersistentHeaderRenderObjectWidget extends RenderObjectWidget {
259
  const _SliverPersistentHeaderRenderObjectWidget({
Ian Hickson's avatar
Ian Hickson committed
260 261
    Key key,
    @required this.delegate,
262 263
  }) : assert(delegate != null),
       super(key: key);
Ian Hickson's avatar
Ian Hickson committed
264

265
  final SliverPersistentHeaderDelegate delegate;
Ian Hickson's avatar
Ian Hickson committed
266 267

  @override
268
  _SliverPersistentHeaderElement createElement() => _SliverPersistentHeaderElement(this);
Ian Hickson's avatar
Ian Hickson committed
269 270

  @override
271
  _RenderSliverPersistentHeaderForWidgetsMixin createRenderObject(BuildContext context);
Ian Hickson's avatar
Ian Hickson committed
272 273

  @override
274
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
275
    super.debugFillProperties(description);
276 277 278 279 280 281
    description.add(
      DiagnosticsProperty<SliverPersistentHeaderDelegate>(
        'delegate',
        delegate,
      )
    );
Ian Hickson's avatar
Ian Hickson committed
282 283 284
  }
}

285
mixin _RenderSliverPersistentHeaderForWidgetsMixin on RenderSliverPersistentHeader {
286
  _SliverPersistentHeaderElement _element;
Ian Hickson's avatar
Ian Hickson committed
287

288 289 290
  @override
  double get minExtent => _element.widget.delegate.minExtent;

Ian Hickson's avatar
Ian Hickson committed
291 292 293 294
  @override
  double get maxExtent => _element.widget.delegate.maxExtent;

  @override
295
  void updateChild(double shrinkOffset, bool overlapsContent) {
Ian Hickson's avatar
Ian Hickson committed
296
    assert(_element != null);
297
    _element._build(shrinkOffset, overlapsContent);
Ian Hickson's avatar
Ian Hickson committed
298 299 300 301
  }

  @protected
  void triggerRebuild() {
302
    markNeedsLayout();
Ian Hickson's avatar
Ian Hickson committed
303 304 305
  }
}

306
class _SliverScrollingPersistentHeader extends _SliverPersistentHeaderRenderObjectWidget {
307
  const _SliverScrollingPersistentHeader({
Ian Hickson's avatar
Ian Hickson committed
308
    Key key,
309
    @required SliverPersistentHeaderDelegate delegate,
310 311 312 313
  }) : super(
    key: key,
    delegate: delegate,
  );
Ian Hickson's avatar
Ian Hickson committed
314 315

  @override
316
  _RenderSliverPersistentHeaderForWidgetsMixin createRenderObject(BuildContext context) {
317 318 319
    return _RenderSliverScrollingPersistentHeaderForWidgets(
      stretchConfiguration: delegate.stretchConfiguration
    );
Ian Hickson's avatar
Ian Hickson committed
320 321 322
  }
}

323
class _RenderSliverScrollingPersistentHeaderForWidgets extends RenderSliverScrollingPersistentHeader
324 325 326 327 328 329 330 331 332
  with _RenderSliverPersistentHeaderForWidgetsMixin {
  _RenderSliverScrollingPersistentHeaderForWidgets({
    RenderBox child,
    OverScrollHeaderStretchConfiguration stretchConfiguration,
  }) : super(
    child: child,
    stretchConfiguration: stretchConfiguration,
  );
}
Ian Hickson's avatar
Ian Hickson committed
333

334
class _SliverPinnedPersistentHeader extends _SliverPersistentHeaderRenderObjectWidget {
335
  const _SliverPinnedPersistentHeader({
Ian Hickson's avatar
Ian Hickson committed
336
    Key key,
337
    @required SliverPersistentHeaderDelegate delegate,
338 339 340 341
  }) : super(
    key: key,
    delegate: delegate,
  );
Ian Hickson's avatar
Ian Hickson committed
342 343

  @override
344
  _RenderSliverPersistentHeaderForWidgetsMixin createRenderObject(BuildContext context) {
345 346 347
    return _RenderSliverPinnedPersistentHeaderForWidgets(
      stretchConfiguration: delegate.stretchConfiguration
    );
Ian Hickson's avatar
Ian Hickson committed
348 349 350
  }
}

351 352 353 354 355 356 357 358 359 360
class _RenderSliverPinnedPersistentHeaderForWidgets extends RenderSliverPinnedPersistentHeader
  with _RenderSliverPersistentHeaderForWidgetsMixin {
  _RenderSliverPinnedPersistentHeaderForWidgets({
    RenderBox child,
    OverScrollHeaderStretchConfiguration stretchConfiguration,
  }) : super(
    child: child,
    stretchConfiguration: stretchConfiguration,
  );
}
Ian Hickson's avatar
Ian Hickson committed
361

362
class _SliverFloatingPersistentHeader extends _SliverPersistentHeaderRenderObjectWidget {
363
  const _SliverFloatingPersistentHeader({
Ian Hickson's avatar
Ian Hickson committed
364
    Key key,
365
    @required SliverPersistentHeaderDelegate delegate,
366 367 368 369
  }) : super(
    key: key,
    delegate: delegate,
  );
Ian Hickson's avatar
Ian Hickson committed
370 371

  @override
372
  _RenderSliverPersistentHeaderForWidgetsMixin createRenderObject(BuildContext context) {
373 374 375 376
    return _RenderSliverFloatingPersistentHeaderForWidgets(
      snapConfiguration: delegate.snapConfiguration,
      stretchConfiguration: delegate.stretchConfiguration,
    );
377 378 379 380 381
  }

  @override
  void updateRenderObject(BuildContext context, _RenderSliverFloatingPersistentHeaderForWidgets renderObject) {
    renderObject.snapConfiguration = delegate.snapConfiguration;
382
    renderObject.stretchConfiguration = delegate.stretchConfiguration;
Ian Hickson's avatar
Ian Hickson committed
383 384 385
  }
}

386 387
class _RenderSliverFloatingPinnedPersistentHeaderForWidgets extends RenderSliverFloatingPinnedPersistentHeader
  with _RenderSliverPersistentHeaderForWidgetsMixin {
388 389 390
  _RenderSliverFloatingPinnedPersistentHeaderForWidgets({
    RenderBox child,
    FloatingHeaderSnapConfiguration snapConfiguration,
391 392 393 394 395 396
    OverScrollHeaderStretchConfiguration stretchConfiguration,
  }) : super(
    child: child,
    snapConfiguration: snapConfiguration,
    stretchConfiguration: stretchConfiguration,
  );
397
}
398 399

class _SliverFloatingPinnedPersistentHeader extends _SliverPersistentHeaderRenderObjectWidget {
400
  const _SliverFloatingPinnedPersistentHeader({
401 402
    Key key,
    @required SliverPersistentHeaderDelegate delegate,
403 404 405 406
  }) : super(
    key: key,
    delegate: delegate,
  );
407 408 409

  @override
  _RenderSliverPersistentHeaderForWidgetsMixin createRenderObject(BuildContext context) {
410 411 412 413
    return _RenderSliverFloatingPinnedPersistentHeaderForWidgets(
      snapConfiguration: delegate.snapConfiguration,
      stretchConfiguration: delegate.stretchConfiguration,
    );
414 415 416 417 418
  }

  @override
  void updateRenderObject(BuildContext context, _RenderSliverFloatingPinnedPersistentHeaderForWidgets renderObject) {
    renderObject.snapConfiguration = delegate.snapConfiguration;
419
    renderObject.stretchConfiguration = delegate.stretchConfiguration;
420 421 422
  }
}

423 424
class _RenderSliverFloatingPersistentHeaderForWidgets extends RenderSliverFloatingPersistentHeader
  with _RenderSliverPersistentHeaderForWidgetsMixin {
425 426 427
  _RenderSliverFloatingPersistentHeaderForWidgets({
    RenderBox child,
    FloatingHeaderSnapConfiguration snapConfiguration,
428 429 430 431 432 433
    OverScrollHeaderStretchConfiguration stretchConfiguration,
  }) : super(
    child: child,
    snapConfiguration: snapConfiguration,
    stretchConfiguration: stretchConfiguration,
  );
434
}