sliver.dart 34 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9
// 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 'dart:collection' show SplayTreeMap, HashMap;

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

10
import 'automatic_keep_alive.dart';
Ian Hickson's avatar
Ian Hickson committed
11
import 'basic.dart';
12
import 'framework.dart';
13

14 15 16 17 18
export 'package:flutter/rendering.dart' show
  SliverGridDelegate,
  SliverGridDelegateWithFixedCrossAxisCount,
  SliverGridDelegateWithMaxCrossAxisExtent;

19 20 21 22 23 24 25 26 27
/// A delegate that supplies children for slivers.
///
/// Many slivers lazily construct their box children to avoid creating more
/// children than are visible through the [Viewport]. Rather than receiving
/// their children as an explicit [List], they receive their children using a
/// [SliverChildDelegate].
///
/// It's uncommon to subclass [SliverChildDelegate]. Instead, consider using one
/// of the existing subclasses that provide adaptors to builder callbacks or
28
/// explicit child lists.
29 30 31 32 33 34 35
///
/// See also:
///
///  * [SliverChildBuilderDelegate], which is a delegate that uses a builder
///    callback to construct the children.
///  * [SliverChildListDelegate], which is a delegate that has an explicit list
///    of children.
Adam Barth's avatar
Adam Barth committed
36
abstract class SliverChildDelegate {
Ian Hickson's avatar
Ian Hickson committed
37 38
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
Adam Barth's avatar
Adam Barth committed
39
  const SliverChildDelegate();
Ian Hickson's avatar
Ian Hickson committed
40

41 42 43
  /// Returns the child with the given index.
  ///
  /// Should return null if asked to build a widget with a greater index than
44 45
  /// exists. If this returns null, [estimatedChildCount] must subsequently
  /// return a precise non-null value.
46 47
  ///
  /// Subclasses typically override this function and wrap their children in
48
  /// [AutomaticKeepAlive] and [RepaintBoundary] widgets.
Ian Hickson's avatar
Ian Hickson committed
49 50
  Widget build(BuildContext context, int index);

51 52 53 54 55 56 57
  /// Returns an estimate of the number of children this delegate will build.
  ///
  /// Used to estimate the maximum scroll offset if [estimateMaxScrollOffset]
  /// returns null.
  ///
  /// Return null if there are an unbounded number of children or if it would
  /// be too difficult to estimate the number of children.
58 59
  ///
  /// This must return a precise number once [build] has returned null.
60
  int get estimatedChildCount => null;
Ian Hickson's avatar
Ian Hickson committed
61

62 63 64 65 66 67 68
  /// Returns an estimate of the max scroll extent for all the children.
  ///
  /// Subclasses should override this function if they have additional
  /// information about their max scroll extent.
  ///
  /// The default implementation returns null, which causes the caller to
  /// extrapolate the max scroll offset from the given parameters.
69
  double estimateMaxScrollOffset(
Ian Hickson's avatar
Ian Hickson committed
70 71 72 73
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
74 75
  ) => null;

76 77 78 79 80 81 82 83 84 85
  /// Called at the end of layout to indicate that layout is now complete.
  ///
  /// The `firstIndex` argument is the index of the first child that was
  /// included in the current layout. The `lastIndex` argument is the index of
  /// the last child that was included in the current layout.
  ///
  /// Useful for subclasses that which to track which children are included in
  /// the underlying render tree.
  void didFinishLayout(int firstIndex, int lastIndex) {}

86 87 88 89 90 91 92 93 94
  /// Called whenever a new instance of the child delegate class is
  /// provided to the sliver.
  ///
  /// If the new instance represents different information than the old
  /// instance, then the method should return true, otherwise it should return
  /// false.
  ///
  /// If the method returns false, then the [build] call might be optimized
  /// away.
95
  bool shouldRebuild(covariant SliverChildDelegate oldDelegate);
96 97 98

  @override
  String toString() {
99
    final List<String> description = <String>[];
100
    debugFillDescription(description);
101
    return '${describeIdentity(this)}(${description.join(", ")})';
102 103
  }

104
  /// Add additional information to the given description for use by [toString].
105
  @protected
106
  @mustCallSuper
107 108 109 110 111 112 113 114 115
  void debugFillDescription(List<String> description) {
    try {
      final int children = estimatedChildCount;
      if (children != null)
        description.add('estimated child count: $children');
    } catch (e) {
      description.add('estimated child count: EXCEPTION (${e.runtimeType})');
    }
  }
Ian Hickson's avatar
Ian Hickson committed
116 117
}

118 119 120 121
/// A delegate that supplies children for slivers using a builder callback.
///
/// Many slivers lazily construct their box children to avoid creating more
/// children than are visible through the [Viewport]. This delegate provides
122 123 124 125
/// children using an [IndexedWidgetBuilder] callback, so that the children do
/// not even have to be built until they are displayed.
///
/// The widgets returned from the builder callback are automatically wrapped in
126 127 128
/// [AutomaticKeepAlive] widgets if [addAutomaticKeepAlives] is true (the
/// default) and in [RepaintBoundary] widgets if [addRepaintBoundaries] is true
/// (also the default).
129 130 131 132 133
///
/// See also:
///
///  * [SliverChildListDelegate], which is a delegate that has an explicit list
///    of children.
134
class SliverChildBuilderDelegate extends SliverChildDelegate {
135
  /// Creates a delegate that supplies children for slivers using the given
136 137
  /// builder callback.
  ///
138 139
  /// The [builder], [addAutomaticKeepAlives], and [addRepaintBoundaries]
  /// arguments must not be null.
140 141 142
  const SliverChildBuilderDelegate(
    this.builder, {
    this.childCount,
143
    this.addAutomaticKeepAlives: true,
144 145
    this.addRepaintBoundaries: true,
  }) : assert(builder != null),
146
       assert(addAutomaticKeepAlives != null),
147
       assert(addRepaintBoundaries != null);
148

149 150 151 152 153 154 155 156 157 158
  /// Called to build children for the sliver.
  ///
  /// Will be called only for indices greater than or equal to zero and less
  /// than [childCount] (if [childCount] is non-null).
  ///
  /// Should return null if asked to build a widget with a greater index than
  /// exists.
  ///
  /// The delegate wraps the children returned by this builder in
  /// [RepaintBoundary] widgets.
159 160
  final IndexedWidgetBuilder builder;

161 162 163 164
  /// The total number of children this delegate can provide.
  ///
  /// If null, the number of children is determined by the least index for which
  /// [builder] returns null.
165 166
  final int childCount;

167 168 169 170 171 172 173 174 175 176 177 178 179 180
  /// Whether to wrap each child in an [AutomaticKeepAlive].
  ///
  /// Typically, children in lazy list are wrapped in [AutomaticKeepAlive]
  /// widgets so that children can use [KeepAliveNotification]s to preserve
  /// their state when they would otherwise be garbage collected off-screen.
  ///
  /// This feature (and [addRepaintBoundaries]) must be disabled if the children
  /// are going to manually maintain their [KeepAlive] state. It may also be
  /// more efficient to disable this feature if it is known ahead of time that
  /// none of the children will ever try to keep themselves alive.
  ///
  /// Defaults to true.
  final bool addAutomaticKeepAlives;

181 182 183 184 185 186 187 188 189 190 191
  /// Whether to wrap each child in a [RepaintBoundary].
  ///
  /// Typically, children in a scrolling container are wrapped in repaint
  /// boundaries so that they do not need to be repainted as the list scrolls.
  /// If the children are easy to repaint (e.g., solid color blocks or a short
  /// snippet of text), it might be more efficient to not add a repaint boundary
  /// and simply repaint the children during scrolling.
  ///
  /// Defaults to true.
  final bool addRepaintBoundaries;

192 193 194 195 196
  @override
  Widget build(BuildContext context, int index) {
    assert(builder != null);
    if (index < 0 || (childCount != null && index >= childCount))
      return null;
197
    Widget child = builder(context, index);
198 199
    if (child == null)
      return null;
200 201 202 203 204
    if (addRepaintBoundaries)
      child = new RepaintBoundary.wrap(child, index);
    if (addAutomaticKeepAlives)
      child = new AutomaticKeepAlive(child: child);
    return child;
205 206 207 208 209 210
  }

  @override
  int get estimatedChildCount => childCount;

  @override
211
  bool shouldRebuild(covariant SliverChildBuilderDelegate oldDelegate) => true;
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/// A delegate that supplies children for slivers using an explicit list.
///
/// Many slivers lazily construct their box children to avoid creating more
/// children than are visible through the [Viewport]. This delegate provides
/// children using an explicit list, which is convenient but reduces the benefit
/// of building children lazily.
///
/// In general building all the widgets in advance is not efficient. It is
/// better to create a delegate that builds them on demand using
/// [SliverChildBuilderDelegate] or by subclassing [SliverChildDelegate]
/// directly.
///
/// This class is provided for the cases where either the list of children is
/// known well in advance (ideally the children are themselves compile-time
/// constants, for example), and therefore will not be built each time the
/// delegate itself is created, or the list is small, such that it's likely
/// always visible (and thus there is nothing to be gained by building it on
/// demand). For example, the body of a dialog box might fit both of these
/// conditions.
///
234
/// The widgets in the given [children] list are automatically wrapped in
235 236 237
/// [AutomaticKeepAlive] widgets if [addAutomaticKeepAlives] is true (the
/// default) and in [RepaintBoundary] widgets if [addRepaintBoundaries] is true
/// (also the default).
238
///
239 240 241 242
/// See also:
///
///  * [SliverChildBuilderDelegate], which is a delegate that uses a builder
///    callback to construct the children.
Adam Barth's avatar
Adam Barth committed
243
class SliverChildListDelegate extends SliverChildDelegate {
244 245
  /// Creates a delegate that supplies children for slivers using the given
  /// list.
246
  ///
247 248
  /// The [children], [addAutomaticKeepAlives], and [addRepaintBoundaries]
  /// arguments must not be null.
249 250
  const SliverChildListDelegate(
    this.children, {
251
    this.addAutomaticKeepAlives: true,
252 253
    this.addRepaintBoundaries: true,
  }) : assert(children != null),
254
       assert(addAutomaticKeepAlives != null),
255
       assert(addRepaintBoundaries != null);
Ian Hickson's avatar
Ian Hickson committed
256

257 258 259 260 261 262 263 264 265 266 267 268 269 270
  /// Whether to wrap each child in an [AutomaticKeepAlive].
  ///
  /// Typically, children in lazy list are wrapped in [AutomaticKeepAlive]
  /// widgets so that children can use [KeepAliveNotification]s to preserve
  /// their state when they would otherwise be garbage collected off-screen.
  ///
  /// This feature (and [addRepaintBoundaries]) must be disabled if the children
  /// are going to manually maintain their [KeepAlive] state. It may also be
  /// more efficient to disable this feature if it is known ahead of time that
  /// none of the children will ever try to keep themselves alive.
  ///
  /// Defaults to true.
  final bool addAutomaticKeepAlives;

271 272 273 274 275 276 277 278 279 280 281 282
  /// Whether to wrap each child in a [RepaintBoundary].
  ///
  /// Typically, children in a scrolling container are wrapped in repaint
  /// boundaries so that they do not need to be repainted as the list scrolls.
  /// If the children are easy to repaint (e.g., solid color blocks or a short
  /// snippet of text), it might be more efficient to not add a repaint boundary
  /// and simply repaint the children during scrolling.
  ///
  /// Defaults to true.
  final bool addRepaintBoundaries;

  /// The widgets to display.
Ian Hickson's avatar
Ian Hickson committed
283 284 285 286 287 288 289
  final List<Widget> children;

  @override
  Widget build(BuildContext context, int index) {
    assert(children != null);
    if (index < 0 || index >= children.length)
      return null;
290
    Widget child = children[index];
291
    assert(child != null);
292 293 294 295 296
    if (addRepaintBoundaries)
      child = new RepaintBoundary.wrap(child, index);
    if (addAutomaticKeepAlives)
      child = new AutomaticKeepAlive(child: child);
    return child;
Ian Hickson's avatar
Ian Hickson committed
297 298
  }

299 300 301
  @override
  int get estimatedChildCount => children.length;

Ian Hickson's avatar
Ian Hickson committed
302
  @override
303
  bool shouldRebuild(covariant SliverChildListDelegate oldDelegate) {
Ian Hickson's avatar
Ian Hickson committed
304 305 306 307
    return children != oldDelegate.children;
  }
}

308 309 310
/// A base class for sliver that have multiple box children.
///
/// Helps subclasses build their children lazily using a [SliverChildDelegate].
Adam Barth's avatar
Adam Barth committed
311
abstract class SliverMultiBoxAdaptorWidget extends RenderObjectWidget {
312
  /// Initializes fields for subclasses.
313
  const SliverMultiBoxAdaptorWidget({
Ian Hickson's avatar
Ian Hickson committed
314 315
    Key key,
    @required this.delegate,
316 317
  }) : assert(delegate != null),
       super(key: key);
Ian Hickson's avatar
Ian Hickson committed
318

319 320 321 322 323 324 325 326 327 328
  /// The delegate that provides the children for this widget.
  ///
  /// The children are constructed lazily using this widget to avoid creating
  /// more children than are visible through the [Viewport].
  ///
  /// See also:
  ///
  ///  * [SliverChildBuilderDelegate] and [SliverChildListDelegate], which are
  ///    commonly used subclasses of [SliverChildDelegate] that use a builder
  ///    callback and an explicit child list, respectively.
Adam Barth's avatar
Adam Barth committed
329
  final SliverChildDelegate delegate;
Ian Hickson's avatar
Ian Hickson committed
330 331

  @override
Adam Barth's avatar
Adam Barth committed
332
  SliverMultiBoxAdaptorElement createElement() => new SliverMultiBoxAdaptorElement(this);
Ian Hickson's avatar
Ian Hickson committed
333 334

  @override
Adam Barth's avatar
Adam Barth committed
335
  RenderSliverMultiBoxAdaptor createRenderObject(BuildContext context);
Ian Hickson's avatar
Ian Hickson committed
336

337 338 339 340 341
  /// Returns an estimate of the max scroll extent for all the children.
  ///
  /// Subclasses should override this function if they have additional
  /// information about their max scroll extent.
  ///
342 343 344 345 346
  /// This is used by [SliverMultiBoxAdaptorElement] to implement part of the
  /// [RenderSliverBoxChildManager] API.
  ///
  /// The default implementation defers to [delegate] via its
  /// [SliverChildDelegate.estimateMaxScrollOffset] method.
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  double estimateMaxScrollOffset(
    SliverConstraints constraints,
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  ) {
    assert(lastIndex >= firstIndex);
    return delegate.estimateMaxScrollOffset(
      firstIndex,
      lastIndex,
      leadingScrollOffset,
      trailingScrollOffset,
    );
  }

Ian Hickson's avatar
Ian Hickson committed
363
  @override
364 365 366
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(new DiagnosticsProperty<SliverChildDelegate>('delegate', delegate));
Ian Hickson's avatar
Ian Hickson committed
367 368 369
  }
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/// A sliver that places multiple box children in a linear array along the main
/// axis.
///
/// Each child is forced to have the [SliverConstraints.crossAxisExtent] in the
/// cross axis but determines its own main axis extent.
///
/// [SliverList] determines its scroll offset by "dead reckoning" because
/// children outside the visible part of the sliver are not materialized, which
/// means [SliverList] cannot learn their main axis extent. Instead, newly
/// materialized children are placed adjacent to existing children.
///
/// If the children have a fixed extent in the main axis, consider using
/// [SliverFixedExtentList] rather than [SliverList] because
/// [SliverFixedExtentList] does not need to perform layout on its children to
/// obtain their extent in the main axis and is therefore more efficient.
///
/// See also:
///
///  * [SliverFixedExtentList], which is more efficient for children with
///    the same extent in the main axis.
390
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
391
///    except that it uses a prototype list item instead of a pixel value to define
392
///    the main axis extent of each item.
393
///  * [SliverGrid], which places its children in arbitrary positions.
394
class SliverList extends SliverMultiBoxAdaptorWidget {
395
  /// Creates a sliver that places box children in a linear array.
396
  const SliverList({
Adam Barth's avatar
Adam Barth committed
397 398 399
    Key key,
    @required SliverChildDelegate delegate,
  }) : super(key: key, delegate: delegate);
Ian Hickson's avatar
Ian Hickson committed
400 401

  @override
402
  RenderSliverList createRenderObject(BuildContext context) {
Adam Barth's avatar
Adam Barth committed
403
    final SliverMultiBoxAdaptorElement element = context;
404
    return new RenderSliverList(childManager: element);
Adam Barth's avatar
Adam Barth committed
405 406
  }
}
Ian Hickson's avatar
Ian Hickson committed
407

408 409 410 411 412 413 414 415 416 417 418 419
/// A sliver that places multiple box children with the same main axis extent in
/// a linear array.
///
/// [SliverFixedExtentList] places its children in a linear array along the main
/// axis starting at offset zero and without gaps. Each child is forced to have
/// the [itemExtent] in the main axis and the
/// [SliverConstraints.crossAxisExtent] in the cross axis.
///
/// [SliverFixedExtentList] is more efficient than [SliverList] because
/// [SliverFixedExtentList] does not need to perform layout on its children to
/// obtain their extent in the main axis.
///
420 421 422 423 424 425 426 427 428 429 430
/// ## Sample code
///
/// This example, which would be inserted into a [CustomScrollView.slivers]
/// list, shows an infinite number of items in varying shades of blue:
///
/// ```dart
/// new SliverFixedExtentList(
///   itemExtent: 50.0,
///   delegate: new SliverChildBuilderDelegate(
///     (BuildContext context, int index) {
///       return new Container(
431
///         alignment: Alignment.center,
432 433 434 435 436 437 438 439
///         color: Colors.lightBlue[100 * (index % 9)],
///         child: new Text('list item $index'),
///       );
///     },
///   ),
/// )
/// ```
///
440 441
/// See also:
///
442
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
443
///    except that it uses a prototype list item instead of a pixel value to define
444
///    the main axis extent of each item.
445
///  * [SliverFillViewport], which determines the [itemExtent] based on
446 447 448
///    [SliverConstraints.viewportMainAxisExtent].
///  * [SliverList], which does not require its children to have the same
///    extent in the main axis.
449
class SliverFixedExtentList extends SliverMultiBoxAdaptorWidget {
450 451
  /// Creates a sliver that places box children with the same main axis extent
  /// in a linear array.
452
  const SliverFixedExtentList({
Adam Barth's avatar
Adam Barth committed
453 454 455 456 457
    Key key,
    @required SliverChildDelegate delegate,
    @required this.itemExtent,
  }) : super(key: key, delegate: delegate);

458
  /// The extent the children are forced to have in the main axis.
Adam Barth's avatar
Adam Barth committed
459
  final double itemExtent;
Ian Hickson's avatar
Ian Hickson committed
460 461

  @override
462
  RenderSliverFixedExtentList createRenderObject(BuildContext context) {
Adam Barth's avatar
Adam Barth committed
463
    final SliverMultiBoxAdaptorElement element = context;
464
    return new RenderSliverFixedExtentList(childManager: element, itemExtent: itemExtent);
Ian Hickson's avatar
Ian Hickson committed
465 466 467
  }

  @override
468
  void updateRenderObject(BuildContext context, RenderSliverFixedExtentList renderObject) {
Adam Barth's avatar
Adam Barth committed
469
    renderObject.itemExtent = itemExtent;
Ian Hickson's avatar
Ian Hickson committed
470
  }
Adam Barth's avatar
Adam Barth committed
471 472
}

473 474 475 476 477 478
/// A sliver that places multiple box children in a two dimensional arrangement.
///
/// [SliverGrid] places its children in arbitrary positions determined by
/// [gridDelegate]. Each child is forced to have the size specified by the
/// [gridDelegate].
///
479 480 481
/// The main axis direction of a grid is the direction in which it scrolls; the
/// cross axis direction is the orthogonal direction.
///
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
/// ## Sample code
///
/// This example, which would be inserted into a [CustomScrollView.slivers]
/// list, shows twenty boxes in a pretty teal grid:
///
/// ```dart
/// new SliverGrid(
///   gridDelegate: new SliverGridDelegateWithMaxCrossAxisExtent(
///     maxCrossAxisExtent: 200.0,
///     mainAxisSpacing: 10.0,
///     crossAxisSpacing: 10.0,
///     childAspectRatio: 4.0,
///   ),
///   delegate: new SliverChildBuilderDelegate(
///     (BuildContext context, int index) {
///       return new Container(
498
///         alignment: Alignment.center,
499 500 501 502 503 504 505 506 507
///         color: Colors.teal[100 * (index % 9)],
///         child: new Text('grid item $index'),
///       );
///     },
///     childCount: 20,
///   ),
/// )
/// ```
///
508 509 510 511 512
/// See also:
///
///  * [SliverList], which places its children in a linear array.
///  * [SliverFixedExtentList], which places its children in a linear
///    array with a fixed extent in the main axis.
513
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
514
///    except that it uses a prototype list item instead of a pixel value to define
515
///    the main axis extent of each item.
516
class SliverGrid extends SliverMultiBoxAdaptorWidget {
517 518
  /// Creates a sliver that places multiple box children in a two dimensional
  /// arrangement.
519
  const SliverGrid({
520 521 522 523 524
    Key key,
    @required SliverChildDelegate delegate,
    @required this.gridDelegate,
  }) : super(key: key, delegate: delegate);

525 526 527 528 529
  /// Creates a sliver that places multiple box children in a two dimensional
  /// arrangement with a fixed number of tiles in the cross axis.
  ///
  /// Uses a [SliverGridDelegateWithFixedCrossAxisCount] as the [gridDelegate],
  /// and a [SliverChildListDelegate] as the [delegate].
530 531 532 533
  ///
  /// See also:
  ///
  ///  * [new GridView.count], the equivalent constructor for [GridView] widgets.
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  SliverGrid.count({
    Key key,
    @required int crossAxisCount,
    double mainAxisSpacing: 0.0,
    double crossAxisSpacing: 0.0,
    double childAspectRatio: 1.0,
    List<Widget> children: const <Widget>[],
  }) : gridDelegate = new SliverGridDelegateWithFixedCrossAxisCount(
         crossAxisCount: crossAxisCount,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
       super(key: key, delegate: new SliverChildListDelegate(children));

  /// Creates a sliver that places multiple box children in a two dimensional
550
  /// arrangement with tiles that each have a maximum cross-axis extent.
551 552 553
  ///
  /// Uses a [SliverGridDelegateWithMaxCrossAxisExtent] as the [gridDelegate],
  /// and a [SliverChildListDelegate] as the [delegate].
554 555 556 557
  ///
  /// See also:
  ///
  ///  * [new GridView.extent], the equivalent constructor for [GridView] widgets.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  SliverGrid.extent({
    Key key,
    @required double maxCrossAxisExtent,
    double mainAxisSpacing: 0.0,
    double crossAxisSpacing: 0.0,
    double childAspectRatio: 1.0,
    List<Widget> children: const <Widget>[],
  }) : gridDelegate = new SliverGridDelegateWithMaxCrossAxisExtent(
         maxCrossAxisExtent: maxCrossAxisExtent,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
       super(key: key, delegate: new SliverChildListDelegate(children));

573
  /// The delegate that controls the size and position of the children.
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
  final SliverGridDelegate gridDelegate;

  @override
  RenderSliverGrid createRenderObject(BuildContext context) {
    final SliverMultiBoxAdaptorElement element = context;
    return new RenderSliverGrid(childManager: element, gridDelegate: gridDelegate);
  }

  @override
  void updateRenderObject(BuildContext context, RenderSliverGrid renderObject) {
    renderObject.gridDelegate = gridDelegate;
  }

  @override
  double estimateMaxScrollOffset(
    SliverConstraints constraints,
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  ) {
    return super.estimateMaxScrollOffset(
      constraints,
      firstIndex,
      lastIndex,
      leadingScrollOffset,
      trailingScrollOffset,
601
    ) ?? gridDelegate.getLayout(constraints).computeMaxScrollOffset(delegate.estimatedChildCount);
602 603 604
  }
}

605 606
/// A sliver that contains a multiple box children that each fill the viewport.
///
607 608 609
/// [SliverFillViewport] places its children in a linear array along the main
/// axis. Each child is sized to fill the viewport, both in the main and cross
/// axis.
610 611 612
///
/// See also:
///
613 614
///  * [SliverFixedExtentList], which has a configurable
///    [SliverFixedExtentList.itemExtent].
615
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
616
///    except that it uses a prototype list item instead of a pixel value to define
617
///    the main axis extent of each item.
618 619
///  * [SliverList], which does not require its children to have the same
///    extent in the main axis.
620
class SliverFillViewport extends SliverMultiBoxAdaptorWidget {
621
  /// Creates a sliver whose box children that each fill the viewport.
622
  const SliverFillViewport({
Adam Barth's avatar
Adam Barth committed
623 624
    Key key,
    @required SliverChildDelegate delegate,
625
    this.viewportFraction: 1.0,
626 627 628
  }) : assert(viewportFraction != null),
       assert(viewportFraction > 0.0),
       super(key: key, delegate: delegate);
629

630 631 632 633 634
  /// The fraction of the viewport that each child should fill in the main axis.
  ///
  /// If this fraction is less than 1.0, more than one child will be visible at
  /// once. If this fraction is greater than 1.0, each child will be larger than
  /// the viewport in the main axis.
635
  final double viewportFraction;
Adam Barth's avatar
Adam Barth committed
636 637

  @override
638
  RenderSliverFillViewport createRenderObject(BuildContext context) {
Adam Barth's avatar
Adam Barth committed
639
    final SliverMultiBoxAdaptorElement element = context;
640
    return new RenderSliverFillViewport(childManager: element, viewportFraction: viewportFraction);
641 642 643
  }

  @override
644
  void updateRenderObject(BuildContext context, RenderSliverFillViewport renderObject) {
645
    renderObject.viewportFraction = viewportFraction;
Adam Barth's avatar
Adam Barth committed
646 647 648
  }
}

649 650 651 652
/// An element that lazily builds children for a [SliverMultiBoxAdaptorWidget].
///
/// Implements [RenderSliverBoxChildManager], which lets this element manage
/// the children of subclasses of [RenderSliverMultiBoxAdaptor].
Adam Barth's avatar
Adam Barth committed
653
class SliverMultiBoxAdaptorElement extends RenderObjectElement implements RenderSliverBoxChildManager {
654
  /// Creates an element that lazily builds children for the given widget.
Adam Barth's avatar
Adam Barth committed
655 656 657 658
  SliverMultiBoxAdaptorElement(SliverMultiBoxAdaptorWidget widget) : super(widget);

  @override
  SliverMultiBoxAdaptorWidget get widget => super.widget;
Ian Hickson's avatar
Ian Hickson committed
659 660

  @override
Adam Barth's avatar
Adam Barth committed
661 662 663
  RenderSliverMultiBoxAdaptor get renderObject => super.renderObject;

  @override
664
  void update(covariant SliverMultiBoxAdaptorWidget newWidget) {
Adam Barth's avatar
Adam Barth committed
665
    final SliverMultiBoxAdaptorWidget oldWidget = widget;
Ian Hickson's avatar
Ian Hickson committed
666
    super.update(newWidget);
Adam Barth's avatar
Adam Barth committed
667 668
    final SliverChildDelegate newDelegate = newWidget.delegate;
    final SliverChildDelegate oldDelegate = oldWidget.delegate;
Ian Hickson's avatar
Ian Hickson committed
669 670 671 672 673
    if (newDelegate != oldDelegate &&
        (newDelegate.runtimeType != oldDelegate.runtimeType || newDelegate.shouldRebuild(oldDelegate)))
      performRebuild();
  }

674 675 676 677 678 679 680
  // We inflate widgets at two different times:
  //  1. When we ourselves are told to rebuild (see performRebuild).
  //  2. When our render object needs a new child (see createChild).
  // In both cases, we cache the results of calling into our delegate to get the widget,
  // so that if we do case 2 later, we don't call the builder again.
  // Any time we do case 1, though, we reset the cache.

681
  final Map<int, Widget> _childWidgets = new HashMap<int, Widget>();
682
  final SplayTreeMap<int, Element> _childElements = new SplayTreeMap<int, Element>();
Ian Hickson's avatar
Ian Hickson committed
683 684 685 686
  RenderBox _currentBeforeChild;

  @override
  void performRebuild() {
687
    _childWidgets.clear(); // Reset the cache, as described above.
Ian Hickson's avatar
Ian Hickson committed
688 689
    super.performRebuild();
    _currentBeforeChild = null;
Adam Barth's avatar
Adam Barth committed
690
    assert(_currentlyUpdatingChildIndex == null);
Ian Hickson's avatar
Ian Hickson committed
691
    try {
692 693 694 695 696 697 698 699 700
      int firstIndex = _childElements.firstKey();
      int lastIndex = _childElements.lastKey();
      if (_childElements.isEmpty) {
        firstIndex = 0;
        lastIndex = 0;
      } else if (_didUnderflow) {
        lastIndex += 1;
      }
      for (int index = firstIndex; index <= lastIndex; ++index) {
Adam Barth's avatar
Adam Barth committed
701
        _currentlyUpdatingChildIndex = index;
702
        final Element newChild = updateChild(_childElements[index], _build(index), index);
Ian Hickson's avatar
Ian Hickson committed
703 704 705 706 707 708 709 710
        if (newChild != null) {
          _childElements[index] = newChild;
          _currentBeforeChild = newChild.renderObject;
        } else {
          _childElements.remove(index);
        }
      }
    } finally {
Adam Barth's avatar
Adam Barth committed
711
      _currentlyUpdatingChildIndex = null;
Ian Hickson's avatar
Ian Hickson committed
712 713 714 715
    }
  }

  Widget _build(int index) {
716
    return _childWidgets.putIfAbsent(index, () => widget.delegate.build(this, index));
Ian Hickson's avatar
Ian Hickson committed
717 718
  }

Adam Barth's avatar
Adam Barth committed
719 720 721
  @override
  void createChild(int index, { @required RenderBox after }) {
    assert(_currentlyUpdatingChildIndex == null);
Ian Hickson's avatar
Ian Hickson committed
722
    owner.buildScope(this, () {
Adam Barth's avatar
Adam Barth committed
723
      final bool insertFirst = after == null;
Ian Hickson's avatar
Ian Hickson committed
724 725 726 727
      assert(insertFirst || _childElements[index-1] != null);
      _currentBeforeChild = insertFirst ? null : _childElements[index-1].renderObject;
      Element newChild;
      try {
Adam Barth's avatar
Adam Barth committed
728
        _currentlyUpdatingChildIndex = index;
Ian Hickson's avatar
Ian Hickson committed
729 730
        newChild = updateChild(_childElements[index], _build(index), index);
      } finally {
Adam Barth's avatar
Adam Barth committed
731
        _currentlyUpdatingChildIndex = null;
Ian Hickson's avatar
Ian Hickson committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
      }
      if (newChild != null) {
        _childElements[index] = newChild;
      } else {
        _childElements.remove(index);
      }
    });
  }

  @override
  void forgetChild(Element child) {
    assert(child != null);
    assert(child.slot != null);
    assert(_childElements.containsKey(child.slot));
    _childElements.remove(child.slot);
  }

Adam Barth's avatar
Adam Barth committed
749 750 751 752
  @override
  void removeChild(RenderBox child) {
    final int index = renderObject.indexOf(child);
    assert(_currentlyUpdatingChildIndex == null);
Ian Hickson's avatar
Ian Hickson committed
753 754 755 756
    assert(index >= 0);
    owner.buildScope(this, () {
      assert(_childElements.containsKey(index));
      try {
Adam Barth's avatar
Adam Barth committed
757 758
        _currentlyUpdatingChildIndex = index;
        final Element result = updateChild(_childElements[index], null, index);
Ian Hickson's avatar
Ian Hickson committed
759 760
        assert(result == null);
      } finally {
Adam Barth's avatar
Adam Barth committed
761
        _currentlyUpdatingChildIndex = null;
Ian Hickson's avatar
Ian Hickson committed
762 763 764 765 766 767
      }
      _childElements.remove(index);
      assert(!_childElements.containsKey(index));
    });
  }

768 769 770 771 772 773
  double _extrapolateMaxScrollOffset(
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  ) {
774
    final int childCount = this.childCount;
775
    if (childCount == null)
776
      return double.infinity;
777 778 779 780 781 782 783 784
    if (lastIndex == childCount - 1)
      return trailingScrollOffset;
    final int reifiedCount = lastIndex - firstIndex + 1;
    final double averageExtent = (trailingScrollOffset - leadingScrollOffset) / reifiedCount;
    final int remainingCount = childCount - lastIndex - 1;
    return trailingScrollOffset + averageExtent * remainingCount;
  }

Adam Barth's avatar
Adam Barth committed
785
  @override
786
  double estimateMaxScrollOffset(SliverConstraints constraints, {
Adam Barth's avatar
Adam Barth committed
787 788 789 790 791
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  }) {
792 793 794 795 796 797 798
    return widget.estimateMaxScrollOffset(
      constraints,
      firstIndex,
      lastIndex,
      leadingScrollOffset,
      trailingScrollOffset,
    ) ?? _extrapolateMaxScrollOffset(
Adam Barth's avatar
Adam Barth committed
799 800 801
      firstIndex,
      lastIndex,
      leadingScrollOffset,
802
      trailingScrollOffset,
Adam Barth's avatar
Adam Barth committed
803 804 805
    );
  }

806 807 808
  @override
  int get childCount => widget.delegate.estimatedChildCount;

809 810 811 812 813 814 815 816 817 818 819 820 821
  @override
  void didStartLayout() {
    assert(debugAssertChildListLocked());
  }

  @override
  void didFinishLayout() {
    assert(debugAssertChildListLocked());
    final int firstIndex = _childElements.firstKey() ?? 0;
    final int lastIndex = _childElements.lastKey() ?? 0;
    widget.delegate.didFinishLayout(firstIndex, lastIndex);
  }

Adam Barth's avatar
Adam Barth committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
  int _currentlyUpdatingChildIndex;

  @override
  bool debugAssertChildListLocked() {
    assert(_currentlyUpdatingChildIndex == null);
    return true;
  }

  @override
  void didAdoptChild(RenderBox child) {
    assert(_currentlyUpdatingChildIndex != null);
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
    childParentData.index = _currentlyUpdatingChildIndex;
  }

837 838 839 840 841 842 843
  bool _didUnderflow = false;

  @override
  void setDidUnderflow(bool value) {
    _didUnderflow = value;
  }

Ian Hickson's avatar
Ian Hickson committed
844
  @override
845
  void insertChildRenderObject(covariant RenderObject child, int slot) {
Adam Barth's avatar
Adam Barth committed
846 847
    assert(slot != null);
    assert(_currentlyUpdatingChildIndex == slot);
848
    assert(renderObject.debugValidateChild(child));
Ian Hickson's avatar
Ian Hickson committed
849 850
    renderObject.insert(child, after: _currentBeforeChild);
    assert(() {
851
      final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
Ian Hickson's avatar
Ian Hickson committed
852 853
      assert(slot == childParentData.index);
      return true;
854
    }());
Ian Hickson's avatar
Ian Hickson committed
855 856 857
  }

  @override
858
  void moveChildRenderObject(covariant RenderObject child, int slot) {
Ian Hickson's avatar
Ian Hickson committed
859 860 861 862 863 864
    // TODO(ianh): At some point we should be better about noticing when a
    // particular LocalKey changes slot, and handle moving the nodes around.
    assert(false);
  }

  @override
865
  void removeChildRenderObject(covariant RenderObject child) {
Adam Barth's avatar
Adam Barth committed
866
    assert(_currentlyUpdatingChildIndex != null);
Ian Hickson's avatar
Ian Hickson committed
867 868 869 870 871 872 873 874 875 876 877
    renderObject.remove(child);
  }

  @override
  void visitChildren(ElementVisitor visitor) {
   // The toList() is to make a copy so that the underlying list can be modified by
   // the visitor:
   assert(!_childElements.values.any((Element child) => child == null));
    _childElements.values.toList().forEach(visitor);
  }
}
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

/// A sliver that contains a single box child that fills the remaining space in
/// the viewport.
///
/// [SliverFillRemaining] sizes its child to fill the viewport in the cross axis
/// and to fill the remaining space in the viewport in the main axis.
///
/// Typically this will be the last sliver in a viewport, since (by definition)
/// there is never any room for anything beyond this sliver.
///
/// See also:
///
///  * [SliverFillViewport], which sizes its children based on the
///    size of the viewport, regardless of what else is in the scroll view.
///  * [SliverList], which shows a list of variable-sized children in a
///    viewport.
class SliverFillRemaining extends SingleChildRenderObjectWidget {
895
  /// Creates a sliver that fills the remaining space in the viewport.
896
  const SliverFillRemaining({
897 898 899 900 901 902 903
    Key key,
    Widget child,
  }) : super(key: key, child: child);

  @override
  RenderSliverFillRemaining createRenderObject(BuildContext context) => new RenderSliverFillRemaining();
}
904 905 906 907 908 909 910 911 912 913

/// Mark a child as needing to stay alive even when it's in a lazy list that
/// would otherwise remove it.
///
/// This widget is for use in [SliverMultiBoxAdaptorWidget]s, such as
/// [SliverGrid] or [SliverList].
class KeepAlive extends ParentDataWidget<SliverMultiBoxAdaptorWidget> {
  /// Marks a child as needing to remain alive.
  ///
  /// The [child] and [keepAlive] arguments must not be null.
914
  const KeepAlive({
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
    Key key,
    @required this.keepAlive,
    @required Widget child,
  }) : assert(child != null),
       assert(keepAlive != null),
       super(key: key, child: child);

  /// Whether to keep the child alive.
  ///
  /// If this is false, it is as if this widget was omitted.
  final bool keepAlive;

  @override
  void applyParentData(RenderObject renderObject) {
    assert(renderObject.parentData is SliverMultiBoxAdaptorParentData);
    final SliverMultiBoxAdaptorParentData parentData = renderObject.parentData;
    if (parentData.keepAlive != keepAlive) {
      parentData.keepAlive = keepAlive;
      final AbstractNode targetParent = renderObject.parent;
934 935
      if (targetParent is RenderObject && !keepAlive)
        targetParent.markNeedsLayout(); // No need to redo layout if it became true.
936 937 938
    }
  }

939 940 941 942 943 944 945
  // We only return true if [keepAlive] is true, because turning _off_ keep
  // alive requires a layout to do the garbage collection (but turning it on
  // requires nothing, since by definition the widget is already alive and won't
  // go away _unless_ we do a layout).
  @override
  bool debugCanApplyOutOfTurn() => keepAlive;

946
  @override
947 948 949
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(new DiagnosticsProperty<bool>('keepAlive', keepAlive));
950 951
  }
}