sliver.dart 65.8 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 '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
// Examples can assume:
20
// late SliverGridDelegateWithMaxCrossAxisExtent _gridDelegate;
21 22 23 24 25 26 27 28 29 30 31 32 33

/// A callback which produces a semantic index given a widget and the local index.
///
/// Return a null value to prevent a widget from receiving an index.
///
/// A semantic index is used to tag child semantic nodes for accessibility
/// announcements in scroll view.
///
/// See also:
///
///  * [CustomScrollView], for an explanation of scroll semantics.
///  * [SliverChildBuilderDelegate], for an explanation of how this is used to
///    generate indexes.
34
typedef SemanticIndexCallback = int? Function(Widget widget, int localIndex);
35 36 37

int _kDefaultSemanticIndexCallback(Widget _, int localIndex) => localIndex;

38 39 40 41 42 43 44 45 46
/// 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
47
/// explicit child lists.
48
///
49
/// {@template flutter.widgets.SliverChildDelegate.lifecycle}
50 51 52 53 54 55 56
/// ## Child elements' lifecycle
///
/// ### Creation
///
/// While laying out the list, visible children's elements, states and render
/// objects will be created lazily based on existing widgets (such as in the
/// case of [SliverChildListDelegate]) or lazily provided ones (such as in the
57
/// case of [SliverChildBuilderDelegate]).
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
///
/// ### Destruction
///
/// When a child is scrolled out of view, the associated element subtree, states
/// and render objects are destroyed. A new child at the same position in the
/// sliver will be lazily recreated along with new elements, states and render
/// objects when it is scrolled back.
///
/// ### Destruction mitigation
///
/// In order to preserve state as child elements are scrolled in and out of
/// view, the following options are possible:
///
///  * Moving the ownership of non-trivial UI-state-driving business logic
///    out of the sliver child subtree. For instance, if a list contains posts
///    with their number of upvotes coming from a cached network response, store
///    the list of posts and upvote number in a data model outside the list. Let
///    the sliver child UI subtree be easily recreate-able from the
///    source-of-truth model object. Use [StatefulWidget]s in the child widget
///    subtree to store instantaneous UI state only.
///
///  * Letting [KeepAlive] be the root widget of the sliver child widget subtree
///    that needs to be preserved. The [KeepAlive] widget marks the child
81
///    subtree's top render object child for keepalive. When the associated top
82 83 84 85 86 87 88 89 90 91 92 93
///    render object is scrolled out of view, the sliver keeps the child's
///    render object (and by extension, its associated elements and states) in a
///    cache list instead of destroying them. When scrolled back into view, the
///    render object is repainted as-is (if it wasn't marked dirty in the
///    interim).
///
///    This only works if the [SliverChildDelegate] subclasses don't wrap the
///    child widget subtree with other widgets such as [AutomaticKeepAlive] and
///    [RepaintBoundary] via `addAutomaticKeepAlives` and
///    `addRepaintBoundaries`.
///
///  * Using [AutomaticKeepAlive] widgets (inserted by default in
94 95 96 97
///    [SliverChildListDelegate] or [SliverChildListDelegate]).
///    [AutomaticKeepAlive] allows descendant widgets to control whether the
///    subtree is actually kept alive or not. This behavior is in contrast with
///    [KeepAlive], which will unconditionally keep the subtree alive.
98 99 100
///
///    As an example, the [EditableText] widget signals its sliver child element
///    subtree to stay alive while its text field has input focus. If it doesn't
101
///    have focus and no other descendants signaled for keepalive via a
102 103 104 105 106
///    [KeepAliveNotification], the sliver child element subtree will be
///    destroyed when scrolled away.
///
///    [AutomaticKeepAlive] descendants typically signal it to be kept alive by
///    using the [AutomaticKeepAliveClientMixin], then implementing the
107 108
///    [AutomaticKeepAliveClientMixin.wantKeepAlive] getter and calling
///    [AutomaticKeepAliveClientMixin.updateKeepAlive].
109 110
/// {@endtemplate}
///
111 112 113 114 115 116
/// 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
117
abstract class SliverChildDelegate {
Ian Hickson's avatar
Ian Hickson committed
118 119
  /// 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
120
  const SliverChildDelegate();
Ian Hickson's avatar
Ian Hickson committed
121

122 123
  /// Returns the child with the given index.
  ///
124 125 126 127
  /// Should return null if asked to build a widget with a greater
  /// index than exists. If this returns null, [estimatedChildCount]
  /// must subsequently return a precise non-null value (which is then
  /// used to implement [RenderSliverBoxChildManager.childCount]).
128 129
  ///
  /// Subclasses typically override this function and wrap their children in
Ian Hickson's avatar
Ian Hickson committed
130 131 132 133 134
  /// [AutomaticKeepAlive], [IndexedSemantics], and [RepaintBoundary] widgets.
  ///
  /// The values returned by this method are cached. To indicate that the
  /// widgets have changed, a new delegate must be provided, and the new
  /// delegate's [shouldRebuild] method must return true.
135
  Widget? build(BuildContext context, int index);
Ian Hickson's avatar
Ian Hickson committed
136

137 138 139 140 141 142 143
  /// 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.
144
  ///
145 146 147
  /// This must return a precise number once [build] has returned null, as it
  /// used to implement [RenderSliverBoxChildManager.childCount].
  int? get estimatedChildCount => null;
Ian Hickson's avatar
Ian Hickson committed
148

149 150 151 152 153 154 155
  /// 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.
156
  double? estimateMaxScrollOffset(
Ian Hickson's avatar
Ian Hickson committed
157 158 159 160
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
161 162
  ) => null;

163 164 165 166 167 168 169 170
  /// 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.
171
  void didFinishLayout(int firstIndex, int lastIndex) { }
172

173 174 175 176 177 178 179 180 181
  /// 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.
182
  bool shouldRebuild(covariant SliverChildDelegate oldDelegate);
183

184 185
  /// Find index of child element with associated key.
  ///
186
  /// This will be called during `performRebuild` in [SliverMultiBoxAdaptorElement]
187 188
  /// to check if a child has moved to a different position. It should return the
  /// index of the child element with associated key, null if not found.
189
  int? findIndexByKey(Key key) => null;
190

191 192
  @override
  String toString() {
193
    final List<String> description = <String>[];
194
    debugFillDescription(description);
195
    return '${describeIdentity(this)}(${description.join(", ")})';
196 197
  }

198
  /// Add additional information to the given description for use by [toString].
199
  @protected
200
  @mustCallSuper
201 202
  void debugFillDescription(List<String> description) {
    try {
203
      final int? children = estimatedChildCount;
204 205 206
      if (children != null)
        description.add('estimated child count: $children');
    } catch (e) {
207
      // The exception is forwarded to widget inspector.
208 209 210
      description.add('estimated child count: EXCEPTION (${e.runtimeType})');
    }
  }
Ian Hickson's avatar
Ian Hickson committed
211 212
}

213
class _SaltedValueKey extends ValueKey<Key> {
214 215 216
  const _SaltedValueKey(Key key): assert(key != null), super(key);
}

217
/// Called to find the new index of a child based on its `key` in case of
218 219
/// reordering.
///
220 221
/// If the child with the `key` is no longer present, null is returned.
///
222
/// Used by [SliverChildBuilderDelegate.findChildIndexCallback].
223
typedef ChildIndexGetter = int? Function(Key key);
224

225 226 227 228
/// 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
229
/// children using a [NullableIndexedWidgetBuilder] callback, so that the children do
230 231 232
/// not even have to be built until they are displayed.
///
/// The widgets returned from the builder callback are automatically wrapped in
233 234 235
/// [AutomaticKeepAlive] widgets if [addAutomaticKeepAlives] is true (the
/// default) and in [RepaintBoundary] widgets if [addRepaintBoundaries] is true
/// (also the default).
236
///
237 238 239 240 241 242 243 244 245 246 247 248 249
/// ## Accessibility
///
/// The [CustomScrollView] requires that its semantic children are annotated
/// using [IndexedSemantics]. This is done by default in the delegate with
/// the `addSemanticIndexes` parameter set to true.
///
/// If multiple delegates are used in a single scroll view, then the indexes
/// will not be correct by default. The `semanticIndexOffset` can be used to
/// offset the semantic indexes of each delegate so that the indexes are
/// monotonically increasing. For example, if a scroll view contains two
/// delegates where the first has 10 children contributing semantics, then the
/// second delegate should offset its children by 10.
///
250
/// {@tool snippet}
251 252 253 254 255 256 257 258 259 260 261 262
///
/// This sample code shows how to use `semanticIndexOffset` to handle multiple
/// delegates in a single scroll view.
///
/// ```dart
/// CustomScrollView(
///   semanticChildCount: 4,
///   slivers: <Widget>[
///     SliverGrid(
///       gridDelegate: _gridDelegate,
///       delegate: SliverChildBuilderDelegate(
///         (BuildContext context, int index) {
263
///            return const Text('...');
264 265 266 267 268 269 270 271
///          },
///          childCount: 2,
///        ),
///      ),
///     SliverGrid(
///       gridDelegate: _gridDelegate,
///       delegate: SliverChildBuilderDelegate(
///         (BuildContext context, int index) {
272
///            return const Text('...');
273 274 275 276 277 278 279 280
///          },
///          childCount: 2,
///          semanticIndexOffset: 2,
///        ),
///      ),
///   ],
/// )
/// ```
281
/// {@end-tool}
282 283 284
///
/// In certain cases, only a subset of child widgets should be annotated
/// with a semantic index. For example, in [new ListView.separated()] the
285
/// separators do not have an index associated with them. This is done by
286 287 288
/// providing a `semanticIndexCallback` which returns null for separators
/// indexes and rounds the non-separator indexes down by half.
///
289
/// {@tool snippet}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
///
/// This sample code shows how to use `semanticIndexCallback` to handle
/// annotating a subset of child nodes with a semantic index. There is
/// a [Spacer] widget at odd indexes which should not have a semantic
/// index.
///
/// ```dart
/// CustomScrollView(
///   semanticChildCount: 5,
///   slivers: <Widget>[
///     SliverGrid(
///       gridDelegate: _gridDelegate,
///       delegate: SliverChildBuilderDelegate(
///         (BuildContext context, int index) {
///            if (index.isEven) {
305
///              return const Text('...');
306
///            }
307
///            return const Spacer();
308 309 310 311 312 313 314 315 316 317 318 319 320
///          },
///          semanticIndexCallback: (Widget widget, int localIndex) {
///            if (localIndex.isEven) {
///              return localIndex ~/ 2;
///            }
///            return null;
///          },
///          childCount: 10,
///        ),
///      ),
///   ],
/// )
/// ```
321
/// {@end-tool}
322
///
323 324 325 326
/// See also:
///
///  * [SliverChildListDelegate], which is a delegate that has an explicit list
///    of children.
327 328
///  * [IndexedSemantics], for an example of manually annotating child nodes
///    with semantic indexes.
329
class SliverChildBuilderDelegate extends SliverChildDelegate {
330
  /// Creates a delegate that supplies children for slivers using the given
331 332
  /// builder callback.
  ///
333 334 335
  /// The [builder], [addAutomaticKeepAlives], [addRepaintBoundaries],
  /// [addSemanticIndexes], and [semanticIndexCallback] arguments must not be
  /// null.
336 337
  ///
  /// If the order in which [builder] returns children ever changes, consider
338 339 340
  /// providing a [findChildIndexCallback]. This allows the delegate to find the
  /// new index for a child that was previously located at a different index to
  /// attach the existing state to the [Widget] at its new location.
341 342
  const SliverChildBuilderDelegate(
    this.builder, {
343
    this.findChildIndexCallback,
344
    this.childCount,
345 346
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
347 348 349
    this.addSemanticIndexes = true,
    this.semanticIndexCallback = _kDefaultSemanticIndexCallback,
    this.semanticIndexOffset = 0,
350
  }) : assert(builder != null),
351
       assert(addAutomaticKeepAlives != null),
352 353 354
       assert(addRepaintBoundaries != null),
       assert(addSemanticIndexes != null),
       assert(semanticIndexCallback != null);
355

356 357 358 359 360 361 362 363 364 365
  /// 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.
366
  final NullableIndexedWidgetBuilder builder;
367

368 369 370 371
  /// 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.
372
  final int? childCount;
373

374 375 376 377 378 379 380 381 382 383 384 385 386 387
  /// 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;

388 389 390 391 392 393 394 395 396 397 398
  /// 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;

399 400 401 402 403
  /// Whether to wrap each child in an [IndexedSemantics].
  ///
  /// Typically, children in a scrolling container must be annotated with a
  /// semantic index in order to generate the correct accessibility
  /// announcements. This should only be set to false if the indexes have
404
  /// already been provided by an [IndexedSemantics] widget.
405 406
  ///
  /// Defaults to true.
407 408
  ///
  /// See also:
409
  ///
410
  ///  * [IndexedSemantics], for an explanation of how to manually
411
  ///    provide semantic indexes.
412 413 414 415 416 417 418 419 420 421 422 423
  final bool addSemanticIndexes;

  /// An initial offset to add to the semantic indexes generated by this widget.
  ///
  /// Defaults to zero.
  final int semanticIndexOffset;

  /// A [SemanticIndexCallback] which is used when [addSemanticIndexes] is true.
  ///
  /// Defaults to providing an index for each widget.
  final SemanticIndexCallback semanticIndexCallback;

424 425 426 427 428 429
  /// Called to find the new index of a child based on its key in case of reordering.
  ///
  /// If not provided, a child widget may not map to its existing [RenderObject]
  /// when the order in which children are returned from [builder] changes.
  /// This may result in state-loss.
  ///
430 431
  /// This callback should take an input [Key], and it should return the
  /// index of the child element with that associated key, or null if not found.
432
  final ChildIndexGetter? findChildIndexCallback;
433 434

  @override
435
  int? findIndexByKey(Key key) {
436 437 438
    if (findChildIndexCallback == null)
      return null;
    assert(key != null);
439
    final Key childKey;
440 441 442 443 444 445
    if (key is _SaltedValueKey) {
      final _SaltedValueKey saltedValueKey = key;
      childKey = saltedValueKey.value;
    } else {
      childKey = key;
    }
446
    return findChildIndexCallback!(childKey);
447 448
  }

449
  @override
450
  @pragma('vm:notify-debugger-on-exception')
451
  Widget? build(BuildContext context, int index) {
452
    assert(builder != null);
453
    if (index < 0 || (childCount != null && index >= childCount!))
454
      return null;
455
    Widget? child;
456 457 458 459 460
    try {
      child = builder(context, index);
    } catch (exception, stackTrace) {
      child = _createErrorWidget(exception, stackTrace);
    }
461
    if (child == null) {
462
      return null;
463 464
    }
    final Key? key = child.key != null ? _SaltedValueKey(child.key!) : null;
465
    if (addRepaintBoundaries)
466
      child = RepaintBoundary(child: child);
467
    if (addSemanticIndexes) {
468
      final int? semanticIndex = semanticIndexCallback(child, index);
469 470 471
      if (semanticIndex != null)
        child = IndexedSemantics(index: semanticIndex + semanticIndexOffset, child: child);
    }
472
    if (addAutomaticKeepAlives)
473
      child = AutomaticKeepAlive(child: child);
474
    return KeyedSubtree(key: key, child: child);
475 476 477
  }

  @override
478
  int? get estimatedChildCount => childCount;
479 480

  @override
481
  bool shouldRebuild(covariant SliverChildBuilderDelegate oldDelegate) => true;
482 483
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
/// 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.
///
504
/// The widgets in the given [children] list are automatically wrapped in
505 506 507
/// [AutomaticKeepAlive] widgets if [addAutomaticKeepAlives] is true (the
/// default) and in [RepaintBoundary] widgets if [addRepaintBoundaries] is true
/// (also the default).
508
///
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
/// ## Accessibility
///
/// The [CustomScrollView] requires that its semantic children are annotated
/// using [IndexedSemantics]. This is done by default in the delegate with
/// the `addSemanticIndexes` parameter set to true.
///
/// If multiple delegates are used in a single scroll view, then the indexes
/// will not be correct by default. The `semanticIndexOffset` can be used to
/// offset the semantic indexes of each delegate so that the indexes are
/// monotonically increasing. For example, if a scroll view contains two
/// delegates where the first has 10 children contributing semantics, then the
/// second delegate should offset its children by 10.
///
/// In certain cases, only a subset of child widgets should be annotated
/// with a semantic index. For example, in [new ListView.separated()] the
524
/// separators do not have an index associated with them. This is done by
525 526 527 528 529 530
/// providing a `semanticIndexCallback` which returns null for separators
/// indexes and rounds the non-separator indexes down by half.
///
/// See [SliverChildBuilderDelegate] for sample code using
/// `semanticIndexOffset` and `semanticIndexCallback`.
///
531 532 533 534
/// See also:
///
///  * [SliverChildBuilderDelegate], which is a delegate that uses a builder
///    callback to construct the children.
Adam Barth's avatar
Adam Barth committed
535
class SliverChildListDelegate extends SliverChildDelegate {
536 537
  /// Creates a delegate that supplies children for slivers using the given
  /// list.
538
  ///
539 540 541
  /// The [children], [addAutomaticKeepAlives], [addRepaintBoundaries],
  /// [addSemanticIndexes], and [semanticIndexCallback] arguments must not be
  /// null.
542 543 544 545
  ///
  /// If the order of children` never changes, consider using the constant
  /// [SliverChildListDelegate.fixed] constructor.
  SliverChildListDelegate(
546 547 548 549 550 551 552 553 554 555
    this.children, {
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
    this.addSemanticIndexes = true,
    this.semanticIndexCallback = _kDefaultSemanticIndexCallback,
    this.semanticIndexOffset = 0,
  }) : assert(children != null),
       assert(addAutomaticKeepAlives != null),
       assert(addRepaintBoundaries != null),
       assert(addSemanticIndexes != null),
556
       assert(semanticIndexCallback != null),
557
       _keyToIndex = <Key?, int>{null: 0};
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

  /// Creates a constant version of the delegate that supplies children for
  /// slivers using the given list.
  ///
  /// If the order of the children will change, consider using the regular
  /// [SliverChildListDelegate] constructor.
  ///
  /// The [children], [addAutomaticKeepAlives], [addRepaintBoundaries],
  /// [addSemanticIndexes], and [semanticIndexCallback] arguments must not be
  /// null.
  const SliverChildListDelegate.fixed(
    this.children, {
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
    this.addSemanticIndexes = true,
    this.semanticIndexCallback = _kDefaultSemanticIndexCallback,
    this.semanticIndexOffset = 0,
  }) : assert(children != null),
       assert(addAutomaticKeepAlives != null),
       assert(addRepaintBoundaries != null),
       assert(addSemanticIndexes != null),
       assert(semanticIndexCallback != null),
       _keyToIndex = null;
Ian Hickson's avatar
Ian Hickson committed
581

582 583 584 585 586 587 588 589 590 591 592 593 594 595
  /// 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;

596 597 598 599 600 601 602 603 604 605 606
  /// 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;

607 608 609 610 611
  /// Whether to wrap each child in an [IndexedSemantics].
  ///
  /// Typically, children in a scrolling container must be annotated with a
  /// semantic index in order to generate the correct accessibility
  /// announcements. This should only be set to false if the indexes have
612
  /// already been provided by an [IndexedSemantics] widget.
613 614
  ///
  /// Defaults to true.
615 616
  ///
  /// See also:
617
  ///
618
  ///  * [IndexedSemantics], for an explanation of how to manually
619
  ///    provide semantic indexes.
620 621 622 623 624 625 626 627 628 629 630 631
  final bool addSemanticIndexes;

  /// An initial offset to add to the semantic indexes generated by this widget.
  ///
  /// Defaults to zero.
  final int semanticIndexOffset;

  /// A [SemanticIndexCallback] which is used when [addSemanticIndexes] is true.
  ///
  /// Defaults to providing an index for each widget.
  final SemanticIndexCallback semanticIndexCallback;

632
  /// The widgets to display.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  ///
  /// If this list is going to be mutated, it is usually wise to put a [Key] on
  /// each of the child widgets, so that the framework can match old
  /// configurations to new configurations and maintain the underlying render
  /// objects.
  ///
  /// Also, a [Widget] in Flutter is immutable, so directly modifying the
  /// [children] such as `someWidget.children.add(...)` or
  /// passing a reference of the original list value to the [children] parameter
  /// will result in incorrect behaviors. Whenever the
  /// children list is modified, a new list object should be provided.
  ///
  /// The following code corrects the problem mentioned above.
  ///
  /// ```dart
  /// class SomeWidgetState extends State<SomeWidget> {
  ///   List<Widget> _children;
  ///
  ///   void initState() {
  ///     _children = [];
  ///   }
  ///
  ///   void someHandler() {
  ///     setState(() {
  ///       // The key here allows Flutter to reuse the underlying render
  ///       // objects even if the children list is recreated.
  ///       _children.add(ChildWidget(key: UniqueKey()));
  ///     });
  ///   }
  ///
  ///   Widget build(BuildContext context) {
  ///     // Always create a new list of children as a Widget is immutable.
665
  ///     return PageView(children: List<Widget>.of(_children));
666 667 668
  ///   }
  /// }
  /// ```
Ian Hickson's avatar
Ian Hickson committed
669 670
  final List<Widget> children;

671 672 673 674
  /// A map to cache key to index lookup for children.
  ///
  /// _keyToIndex[null] is used as current index during the lazy loading process
  /// in [_findChildIndex]. _keyToIndex should never be used for looking up null key.
675
  final Map<Key?, int>? _keyToIndex;
676 677 678

  bool get _isConstantInstance => _keyToIndex == null;

679
  int? _findChildIndex(Key key) {
680 681 682 683
    if (_isConstantInstance) {
      return null;
    }
    // Lazily fill the [_keyToIndex].
684 685
    if (!_keyToIndex!.containsKey(key)) {
      int index = _keyToIndex![null]!;
686 687 688
      while (index < children.length) {
        final Widget child = children[index];
        if (child.key != null) {
689
          _keyToIndex![child.key] = index;
690 691 692
        }
        if (child.key == key) {
          // Record current index for next function call.
693
          _keyToIndex![null] = index + 1;
694 695 696 697
          return index;
        }
        index += 1;
      }
698
      _keyToIndex![null] = index;
699
    } else {
700
      return _keyToIndex![key];
701 702 703 704 705
    }
    return null;
  }

  @override
706
  int? findIndexByKey(Key key) {
707
    assert(key != null);
708
    final Key childKey;
709 710 711 712 713 714 715 716 717
    if (key is _SaltedValueKey) {
      final _SaltedValueKey saltedValueKey = key;
      childKey = saltedValueKey.value;
    } else {
      childKey = key;
    }
    return _findChildIndex(childKey);
  }

Ian Hickson's avatar
Ian Hickson committed
718
  @override
719
  Widget? build(BuildContext context, int index) {
Ian Hickson's avatar
Ian Hickson committed
720 721 722
    assert(children != null);
    if (index < 0 || index >= children.length)
      return null;
723
    Widget child = children[index];
724
    final Key? key = child.key != null? _SaltedValueKey(child.key!) : null;
725 726
    assert(
      child != null,
727
      "The sliver's children must not contain null values, but a null value was found at index $index",
728
    );
729
    if (addRepaintBoundaries)
730
      child = RepaintBoundary(child: child);
731
    if (addSemanticIndexes) {
732
      final int? semanticIndex = semanticIndexCallback(child, index);
733 734 735
      if (semanticIndex != null)
        child = IndexedSemantics(index: semanticIndex + semanticIndexOffset, child: child);
    }
736
    if (addAutomaticKeepAlives)
737
      child = AutomaticKeepAlive(child: child);
738
    return KeyedSubtree(key: key, child: child);
Ian Hickson's avatar
Ian Hickson committed
739 740
  }

741
  @override
742
  int? get estimatedChildCount => children.length;
743

Ian Hickson's avatar
Ian Hickson committed
744
  @override
745
  bool shouldRebuild(covariant SliverChildListDelegate oldDelegate) {
Ian Hickson's avatar
Ian Hickson committed
746 747 748 749
    return children != oldDelegate.children;
  }
}

750
/// A base class for sliver that have [KeepAlive] children.
751 752 753
///
/// See also:
///
754
/// * [KeepAlive], which marks whether its child widget should be kept alive.
755
/// * [SliverChildBuilderDelegate] and [SliverChildListDelegate], slivers
756
///    which make use of the keep alive functionality through the
757 758 759
///    `addAutomaticKeepAlives` property.
/// * [SliverGrid] and [SliverList], two sliver widgets that are commonly
///    wrapped with [KeepAlive] widgets to preserve their sliver child subtrees.
760 761 762
abstract class SliverWithKeepAliveWidget extends RenderObjectWidget {
  /// Initializes fields for subclasses.
  const SliverWithKeepAliveWidget({
763
    Key? key,
764 765 766 767 768 769
  }) : super(key : key);

  @override
  RenderSliverWithKeepAliveMixin createRenderObject(BuildContext context);
}

770 771 772
/// A base class for sliver that have multiple box children.
///
/// Helps subclasses build their children lazily using a [SliverChildDelegate].
Ian Hickson's avatar
Ian Hickson committed
773 774
///
/// The widgets returned by the [delegate] are cached and the delegate is only
775 776
/// consulted again if it changes and the new delegate's
/// [SliverChildDelegate.shouldRebuild] method returns true.
777
abstract class SliverMultiBoxAdaptorWidget extends SliverWithKeepAliveWidget {
778
  /// Initializes fields for subclasses.
779
  const SliverMultiBoxAdaptorWidget({
780 781
    Key? key,
    required this.delegate,
782 783
  }) : assert(delegate != null),
       super(key: key);
Ian Hickson's avatar
Ian Hickson committed
784

785
  /// {@template flutter.widgets.SliverMultiBoxAdaptorWidget.delegate}
786 787
  /// The delegate that provides the children for this widget.
  ///
Ian Hickson's avatar
Ian Hickson committed
788
  /// The children are constructed lazily using this delegate to avoid creating
789 790 791 792 793 794 795
  /// 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.
796
  /// {@endtemplate}
Adam Barth's avatar
Adam Barth committed
797
  final SliverChildDelegate delegate;
Ian Hickson's avatar
Ian Hickson committed
798 799

  @override
800
  SliverMultiBoxAdaptorElement createElement() => SliverMultiBoxAdaptorElement(this);
Ian Hickson's avatar
Ian Hickson committed
801 802

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

805 806 807 808 809
  /// 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.
  ///
810 811 812 813 814
  /// This is used by [SliverMultiBoxAdaptorElement] to implement part of the
  /// [RenderSliverBoxChildManager] API.
  ///
  /// The default implementation defers to [delegate] via its
  /// [SliverChildDelegate.estimateMaxScrollOffset] method.
815 816
  double? estimateMaxScrollOffset(
    SliverConstraints? constraints,
817 818 819 820 821 822 823 824 825 826 827 828 829 830
    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
831
  @override
832 833
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
834
    properties.add(DiagnosticsProperty<SliverChildDelegate>('delegate', delegate));
Ian Hickson's avatar
Ian Hickson committed
835 836 837
  }
}

838 839 840 841 842 843 844 845 846 847 848
/// 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.
///
849 850
/// {@youtube 560 315 https://www.youtube.com/watch?v=ORiTTaVY6mM}
///
851 852 853 854 855
/// 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.
///
856
/// {@macro flutter.widgets.SliverChildDelegate.lifecycle}
857
///
858 859
/// See also:
///
860 861
///  * <https://flutter.dev/docs/development/ui/advanced/slivers>, a description
///    of what slivers are and how to use them.
862 863
///  * [SliverFixedExtentList], which is more efficient for children with
///    the same extent in the main axis.
864
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
865
///    except that it uses a prototype list item instead of a pixel value to define
866
///    the main axis extent of each item.
867
///  * [SliverGrid], which places its children in arbitrary positions.
868
class SliverList extends SliverMultiBoxAdaptorWidget {
869
  /// Creates a sliver that places box children in a linear array.
870
  const SliverList({
871 872
    Key? key,
    required SliverChildDelegate delegate,
Adam Barth's avatar
Adam Barth committed
873
  }) : super(key: key, delegate: delegate);
Ian Hickson's avatar
Ian Hickson committed
874

875 876 877
  @override
  SliverMultiBoxAdaptorElement createElement() => SliverMultiBoxAdaptorElement(this, replaceMovedChildren: true);

Ian Hickson's avatar
Ian Hickson committed
878
  @override
879
  RenderSliverList createRenderObject(BuildContext context) {
880
    final SliverMultiBoxAdaptorElement element = context as SliverMultiBoxAdaptorElement;
881
    return RenderSliverList(childManager: element);
Adam Barth's avatar
Adam Barth committed
882 883
  }
}
Ian Hickson's avatar
Ian Hickson committed
884

885 886 887 888 889 890 891 892 893 894 895 896
/// 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.
///
897
/// {@tool snippet}
898 899 900 901 902
///
/// This example, which would be inserted into a [CustomScrollView.slivers]
/// list, shows an infinite number of items in varying shades of blue:
///
/// ```dart
903
/// SliverFixedExtentList(
904
///   itemExtent: 50.0,
905
///   delegate: SliverChildBuilderDelegate(
906
///     (BuildContext context, int index) {
907
///       return Container(
908
///         alignment: Alignment.center,
909
///         color: Colors.lightBlue[100 * (index % 9)],
910
///         child: Text('list item $index'),
911 912 913 914 915
///       );
///     },
///   ),
/// )
/// ```
916
/// {@end-tool}
917
///
918
/// {@macro flutter.widgets.SliverChildDelegate.lifecycle}
919
///
920 921
/// See also:
///
922
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
923
///    except that it uses a prototype list item instead of a pixel value to define
924
///    the main axis extent of each item.
925
///  * [SliverFillViewport], which determines the [itemExtent] based on
926 927 928
///    [SliverConstraints.viewportMainAxisExtent].
///  * [SliverList], which does not require its children to have the same
///    extent in the main axis.
929
class SliverFixedExtentList extends SliverMultiBoxAdaptorWidget {
930 931
  /// Creates a sliver that places box children with the same main axis extent
  /// in a linear array.
932
  const SliverFixedExtentList({
933 934 935
    Key? key,
    required SliverChildDelegate delegate,
    required this.itemExtent,
Adam Barth's avatar
Adam Barth committed
936 937
  }) : super(key: key, delegate: delegate);

938
  /// The extent the children are forced to have in the main axis.
Adam Barth's avatar
Adam Barth committed
939
  final double itemExtent;
Ian Hickson's avatar
Ian Hickson committed
940 941

  @override
942
  RenderSliverFixedExtentList createRenderObject(BuildContext context) {
943
    final SliverMultiBoxAdaptorElement element = context as SliverMultiBoxAdaptorElement;
944
    return RenderSliverFixedExtentList(childManager: element, itemExtent: itemExtent);
Ian Hickson's avatar
Ian Hickson committed
945 946 947
  }

  @override
948
  void updateRenderObject(BuildContext context, RenderSliverFixedExtentList renderObject) {
Adam Barth's avatar
Adam Barth committed
949
    renderObject.itemExtent = itemExtent;
Ian Hickson's avatar
Ian Hickson committed
950
  }
Adam Barth's avatar
Adam Barth committed
951 952
}

953 954 955 956 957 958
/// 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].
///
959 960 961
/// The main axis direction of a grid is the direction in which it scrolls; the
/// cross axis direction is the orthogonal direction.
///
962 963
/// {@youtube 560 315 https://www.youtube.com/watch?v=ORiTTaVY6mM}
///
964
/// {@tool snippet}
965 966 967 968 969
///
/// This example, which would be inserted into a [CustomScrollView.slivers]
/// list, shows twenty boxes in a pretty teal grid:
///
/// ```dart
970
/// SliverGrid(
971
///   gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
972 973 974 975 976
///     maxCrossAxisExtent: 200.0,
///     mainAxisSpacing: 10.0,
///     crossAxisSpacing: 10.0,
///     childAspectRatio: 4.0,
///   ),
977
///   delegate: SliverChildBuilderDelegate(
978
///     (BuildContext context, int index) {
979
///       return Container(
980
///         alignment: Alignment.center,
981
///         color: Colors.teal[100 * (index % 9)],
982
///         child: Text('grid item $index'),
983 984 985 986 987 988
///       );
///     },
///     childCount: 20,
///   ),
/// )
/// ```
989
/// {@end-tool}
990
///
991
/// {@macro flutter.widgets.SliverChildDelegate.lifecycle}
992
///
993 994 995 996 997
/// 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.
998
///  * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
999
///    except that it uses a prototype list item instead of a pixel value to define
1000
///    the main axis extent of each item.
1001
class SliverGrid extends SliverMultiBoxAdaptorWidget {
1002 1003
  /// Creates a sliver that places multiple box children in a two dimensional
  /// arrangement.
1004
  const SliverGrid({
1005 1006 1007
    Key? key,
    required SliverChildDelegate delegate,
    required this.gridDelegate,
1008 1009
  }) : super(key: key, delegate: delegate);

1010 1011 1012 1013 1014
  /// 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].
1015 1016 1017 1018
  ///
  /// See also:
  ///
  ///  * [new GridView.count], the equivalent constructor for [GridView] widgets.
1019
  SliverGrid.count({
1020 1021
    Key? key,
    required int crossAxisCount,
1022 1023 1024 1025
    double mainAxisSpacing = 0.0,
    double crossAxisSpacing = 0.0,
    double childAspectRatio = 1.0,
    List<Widget> children = const <Widget>[],
1026
  }) : gridDelegate = SliverGridDelegateWithFixedCrossAxisCount(
1027 1028 1029 1030 1031
         crossAxisCount: crossAxisCount,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
1032
       super(key: key, delegate: SliverChildListDelegate(children));
1033 1034

  /// Creates a sliver that places multiple box children in a two dimensional
1035
  /// arrangement with tiles that each have a maximum cross-axis extent.
1036 1037 1038
  ///
  /// Uses a [SliverGridDelegateWithMaxCrossAxisExtent] as the [gridDelegate],
  /// and a [SliverChildListDelegate] as the [delegate].
1039 1040 1041 1042
  ///
  /// See also:
  ///
  ///  * [new GridView.extent], the equivalent constructor for [GridView] widgets.
1043
  SliverGrid.extent({
1044 1045
    Key? key,
    required double maxCrossAxisExtent,
1046 1047 1048 1049
    double mainAxisSpacing = 0.0,
    double crossAxisSpacing = 0.0,
    double childAspectRatio = 1.0,
    List<Widget> children = const <Widget>[],
1050
  }) : gridDelegate = SliverGridDelegateWithMaxCrossAxisExtent(
1051 1052 1053 1054 1055
         maxCrossAxisExtent: maxCrossAxisExtent,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
1056
       super(key: key, delegate: SliverChildListDelegate(children));
1057

1058
  /// The delegate that controls the size and position of the children.
1059 1060 1061 1062
  final SliverGridDelegate gridDelegate;

  @override
  RenderSliverGrid createRenderObject(BuildContext context) {
1063
    final SliverMultiBoxAdaptorElement element = context as SliverMultiBoxAdaptorElement;
1064
    return RenderSliverGrid(childManager: element, gridDelegate: gridDelegate);
1065 1066 1067 1068 1069 1070 1071 1072 1073
  }

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

  @override
  double estimateMaxScrollOffset(
1074
    SliverConstraints? constraints,
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  ) {
    return super.estimateMaxScrollOffset(
      constraints,
      firstIndex,
      lastIndex,
      leadingScrollOffset,
      trailingScrollOffset,
1086
    ) ?? gridDelegate.getLayout(constraints!).computeMaxScrollOffset(delegate.estimatedChildCount!);
1087 1088 1089
  }
}

1090 1091 1092 1093
/// 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
1094
class SliverMultiBoxAdaptorElement extends RenderObjectElement implements RenderSliverBoxChildManager {
1095
  /// Creates an element that lazily builds children for the given widget.
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
  ///
  /// If `replaceMovedChildren` is set to true, a new child is proactively
  /// inflate for the index that was previously occupied by a child that moved
  /// to a new index. The layout offset of the moved child is copied over to the
  /// new child. RenderObjects, that depend on the layout offset of existing
  /// children during [RenderObject.performLayout] should set this to true
  /// (example: [RenderSliverList]). For RenderObjects that figure out the
  /// layout offset of their children without looking at the layout offset of
  /// existing children this should be set to false (example:
  /// [RenderSliverFixedExtentList]) to avoid inflating unnecessary children.
  SliverMultiBoxAdaptorElement(SliverMultiBoxAdaptorWidget widget, {bool replaceMovedChildren = false})
     : _replaceMovedChildren = replaceMovedChildren,
       super(widget);

  final bool _replaceMovedChildren;
Adam Barth's avatar
Adam Barth committed
1111 1112

  @override
1113
  SliverMultiBoxAdaptorWidget get widget => super.widget as SliverMultiBoxAdaptorWidget;
Ian Hickson's avatar
Ian Hickson committed
1114 1115

  @override
1116
  RenderSliverMultiBoxAdaptor get renderObject => super.renderObject as RenderSliverMultiBoxAdaptor;
Adam Barth's avatar
Adam Barth committed
1117 1118

  @override
1119
  void update(covariant SliverMultiBoxAdaptorWidget newWidget) {
Adam Barth's avatar
Adam Barth committed
1120
    final SliverMultiBoxAdaptorWidget oldWidget = widget;
Ian Hickson's avatar
Ian Hickson committed
1121
    super.update(newWidget);
Adam Barth's avatar
Adam Barth committed
1122 1123
    final SliverChildDelegate newDelegate = newWidget.delegate;
    final SliverChildDelegate oldDelegate = oldWidget.delegate;
Ian Hickson's avatar
Ian Hickson committed
1124 1125 1126 1127 1128
    if (newDelegate != oldDelegate &&
        (newDelegate.runtimeType != oldDelegate.runtimeType || newDelegate.shouldRebuild(oldDelegate)))
      performRebuild();
  }

1129 1130
  final SplayTreeMap<int, Element?> _childElements = SplayTreeMap<int, Element?>();
  RenderBox? _currentBeforeChild;
Ian Hickson's avatar
Ian Hickson committed
1131 1132 1133 1134 1135

  @override
  void performRebuild() {
    super.performRebuild();
    _currentBeforeChild = null;
1136
    bool childrenUpdated = false;
Adam Barth's avatar
Adam Barth committed
1137
    assert(_currentlyUpdatingChildIndex == null);
Ian Hickson's avatar
Ian Hickson committed
1138
    try {
1139
      final SplayTreeMap<int, Element?> newChildren = SplayTreeMap<int, Element?>();
1140
      final Map<int, double> indexToLayoutOffset = HashMap<int, double>();
1141
      void processElement(int index) {
Adam Barth's avatar
Adam Barth committed
1142
        _currentlyUpdatingChildIndex = index;
1143 1144 1145
        if (_childElements[index] != null && _childElements[index] != newChildren[index]) {
          // This index has an old child that isn't used anywhere and should be deactivated.
          _childElements[index] = updateChild(_childElements[index], null, index);
1146
          childrenUpdated = true;
1147
        }
1148
        final Element? newChild = updateChild(newChildren[index], _build(index), index);
Ian Hickson's avatar
Ian Hickson committed
1149
        if (newChild != null) {
1150
          childrenUpdated = childrenUpdated || _childElements[index] != newChild;
Ian Hickson's avatar
Ian Hickson committed
1151
          _childElements[index] = newChild;
1152
          final SliverMultiBoxAdaptorParentData parentData = newChild.renderObject!.parentData! as SliverMultiBoxAdaptorParentData;
1153 1154 1155 1156 1157
          if (index == 0) {
            parentData.layoutOffset = 0.0;
          } else if (indexToLayoutOffset.containsKey(index)) {
            parentData.layoutOffset = indexToLayoutOffset[index];
          }
1158
          if (!parentData.keptAlive)
1159
            _currentBeforeChild = newChild.renderObject as RenderBox?;
Ian Hickson's avatar
Ian Hickson committed
1160
        } else {
1161
          childrenUpdated = true;
Ian Hickson's avatar
Ian Hickson committed
1162 1163 1164
          _childElements.remove(index);
        }
      }
1165
      for (final int index in _childElements.keys.toList()) {
1166 1167 1168 1169
        final Key? key = _childElements[index]!.widget.key;
        final int? newIndex = key == null ? null : widget.delegate.findIndexByKey(key);
        final SliverMultiBoxAdaptorParentData? childParentData =
          _childElements[index]!.renderObject?.parentData as SliverMultiBoxAdaptorParentData?;
1170 1171

        if (childParentData != null && childParentData.layoutOffset != null)
1172
          indexToLayoutOffset[index] = childParentData.layoutOffset!;
1173

1174
        if (newIndex != null && newIndex != index) {
1175 1176 1177 1178
          // The layout offset of the child being moved is no longer accurate.
          if (childParentData != null)
            childParentData.layoutOffset = null;

1179
          newChildren[newIndex] = _childElements[index];
1180 1181 1182 1183
          if (_replaceMovedChildren) {
            // We need to make sure the original index gets processed.
            newChildren.putIfAbsent(index, () => null);
          }
1184 1185 1186 1187 1188 1189 1190 1191 1192
          // We do not want the remapped child to get deactivated during processElement.
          _childElements.remove(index);
        } else {
          newChildren.putIfAbsent(index, () => _childElements[index]);
        }
      }

      renderObject.debugChildIntegrityEnabled = false; // Moving children will temporary violate the integrity.
      newChildren.keys.forEach(processElement);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
      // An element rebuild only updates existing children. The underflow check
      // is here to make sure we look ahead one more child if we were at the end
      // of the child list before the update. By doing so, we can update the max
      // scroll offset during the layout phase. Otherwise, the layout phase may
      // be skipped, and the scroll view may be stuck at the previous max
      // scroll offset.
      //
      // This logic is not needed if any existing children has been updated,
      // because we will not skip the layout phase if that happens.
      if (!childrenUpdated && _didUnderflow) {
1203
        final int lastKey = _childElements.lastKey() ?? -1;
1204 1205 1206
        final int rightBoundary = lastKey + 1;
        newChildren[rightBoundary] = _childElements[rightBoundary];
        processElement(rightBoundary);
1207
      }
Ian Hickson's avatar
Ian Hickson committed
1208
    } finally {
Adam Barth's avatar
Adam Barth committed
1209
      _currentlyUpdatingChildIndex = null;
1210
      renderObject.debugChildIntegrityEnabled = true;
Ian Hickson's avatar
Ian Hickson committed
1211 1212 1213
    }
  }

1214
  Widget? _build(int index) {
1215
    return widget.delegate.build(this, index);
Ian Hickson's avatar
Ian Hickson committed
1216 1217
  }

Adam Barth's avatar
Adam Barth committed
1218
  @override
1219
  void createChild(int index, { required RenderBox? after }) {
Adam Barth's avatar
Adam Barth committed
1220
    assert(_currentlyUpdatingChildIndex == null);
1221
    owner!.buildScope(this, () {
Adam Barth's avatar
Adam Barth committed
1222
      final bool insertFirst = after == null;
Ian Hickson's avatar
Ian Hickson committed
1223
      assert(insertFirst || _childElements[index-1] != null);
1224 1225
      _currentBeforeChild = insertFirst ? null : (_childElements[index-1]!.renderObject as RenderBox?);
      Element? newChild;
Ian Hickson's avatar
Ian Hickson committed
1226
      try {
Adam Barth's avatar
Adam Barth committed
1227
        _currentlyUpdatingChildIndex = index;
Ian Hickson's avatar
Ian Hickson committed
1228 1229
        newChild = updateChild(_childElements[index], _build(index), index);
      } finally {
Adam Barth's avatar
Adam Barth committed
1230
        _currentlyUpdatingChildIndex = null;
Ian Hickson's avatar
Ian Hickson committed
1231 1232 1233 1234 1235 1236 1237 1238 1239
      }
      if (newChild != null) {
        _childElements[index] = newChild;
      } else {
        _childElements.remove(index);
      }
    });
  }

1240
  @override
1241
  Element? updateChild(Element? child, Widget? newWidget, Object? newSlot) {
1242 1243 1244
    final SliverMultiBoxAdaptorParentData? oldParentData = child?.renderObject?.parentData as SliverMultiBoxAdaptorParentData?;
    final Element? newChild = super.updateChild(child, newWidget, newSlot);
    final SliverMultiBoxAdaptorParentData? newParentData = newChild?.renderObject?.parentData as SliverMultiBoxAdaptorParentData?;
1245 1246 1247 1248 1249 1250 1251 1252

    // Preserve the old layoutOffset if the renderObject was swapped out.
    if (oldParentData != newParentData && oldParentData != null && newParentData != null) {
      newParentData.layoutOffset = oldParentData.layoutOffset;
    }
    return newChild;
  }

Ian Hickson's avatar
Ian Hickson committed
1253 1254 1255 1256 1257 1258
  @override
  void forgetChild(Element child) {
    assert(child != null);
    assert(child.slot != null);
    assert(_childElements.containsKey(child.slot));
    _childElements.remove(child.slot);
1259
    super.forgetChild(child);
Ian Hickson's avatar
Ian Hickson committed
1260 1261
  }

Adam Barth's avatar
Adam Barth committed
1262 1263 1264 1265
  @override
  void removeChild(RenderBox child) {
    final int index = renderObject.indexOf(child);
    assert(_currentlyUpdatingChildIndex == null);
Ian Hickson's avatar
Ian Hickson committed
1266
    assert(index >= 0);
1267
    owner!.buildScope(this, () {
Ian Hickson's avatar
Ian Hickson committed
1268 1269
      assert(_childElements.containsKey(index));
      try {
Adam Barth's avatar
Adam Barth committed
1270
        _currentlyUpdatingChildIndex = index;
1271
        final Element? result = updateChild(_childElements[index], null, index);
Ian Hickson's avatar
Ian Hickson committed
1272 1273
        assert(result == null);
      } finally {
Adam Barth's avatar
Adam Barth committed
1274
        _currentlyUpdatingChildIndex = null;
Ian Hickson's avatar
Ian Hickson committed
1275 1276 1277 1278 1279 1280
      }
      _childElements.remove(index);
      assert(!_childElements.containsKey(index));
    });
  }

1281
  static double _extrapolateMaxScrollOffset(
1282 1283 1284 1285
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
1286
    int childCount,
1287 1288 1289 1290 1291 1292 1293 1294 1295
  ) {
    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
1296
  @override
1297
  double estimateMaxScrollOffset(
1298 1299 1300 1301 1302
    SliverConstraints? constraints, {
    int? firstIndex,
    int? lastIndex,
    double? leadingScrollOffset,
    double? trailingScrollOffset,
Adam Barth's avatar
Adam Barth committed
1303
  }) {
1304
    final int? childCount = estimatedChildCount;
1305 1306
    if (childCount == null)
      return double.infinity;
1307 1308
    return widget.estimateMaxScrollOffset(
      constraints,
1309 1310 1311 1312
      firstIndex!,
      lastIndex!,
      leadingScrollOffset!,
      trailingScrollOffset!,
1313
    ) ?? _extrapolateMaxScrollOffset(
Adam Barth's avatar
Adam Barth committed
1314 1315 1316
      firstIndex,
      lastIndex,
      leadingScrollOffset,
1317
      trailingScrollOffset,
1318
      childCount,
Adam Barth's avatar
Adam Barth committed
1319 1320 1321
    );
  }

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
  /// The best available estimate of [childCount], or null if no estimate is available.
  ///
  /// This differs from [childCount] in that [childCount] never returns null (and must
  /// not be accessed if the child count is not yet available, meaning the [createChild]
  /// method has not been provided an index that does not create a child).
  ///
  /// See also:
  ///
  ///  * [SliverChildDelegate.estimatedChildCount], to which this getter defers.
  int? get estimatedChildCount => widget.delegate.estimatedChildCount;

1333
  @override
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  int get childCount {
    int? result = estimatedChildCount;
    if (result == null) {
      // Since childCount was called, we know that we reached the end of
      // the list (as in, _build return null once), so we know that the
      // list is finite.
      // Let's do an open-ended binary search to find the end of the list
      // manually.
      int lo = 0;
      int hi = 1;
      const int max = kIsWeb
        ? 9007199254740992 // max safe integer on JS (from 0 to this number x != x+1)
        : ((1 << 63) - 1);
      while (_build(hi - 1) != null) {
        lo = hi - 1;
        if (hi < max ~/ 2) {
          hi *= 2;
        } else if (hi < max) {
          hi = max;
        } else {
          throw FlutterError(
            'Could not find the number of children in ${widget.delegate}.\n'
1356
            "The childCount getter was called (implying that the delegate's builder returned null "
1357 1358
            'for a positive index), but even building the child with index $hi (the maximum '
            'possible integer) did not return null. Consider implementing childCount to avoid '
1359
            'the cost of searching for the final child.',
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
          );
        }
      }
      while (hi - lo > 1) {
        final int mid = (hi - lo) ~/ 2 + lo;
        if (_build(mid - 1) == null) {
          hi = mid;
        } else {
          lo = mid;
        }
      }
      result = lo;
    }
    return result;
  }
1375

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
  @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);
  }

1389
  int? _currentlyUpdatingChildIndex;
Adam Barth's avatar
Adam Barth committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

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

  @override
  void didAdoptChild(RenderBox child) {
    assert(_currentlyUpdatingChildIndex != null);
1400
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
Adam Barth's avatar
Adam Barth committed
1401 1402 1403
    childParentData.index = _currentlyUpdatingChildIndex;
  }

1404 1405 1406 1407 1408 1409 1410
  bool _didUnderflow = false;

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

Ian Hickson's avatar
Ian Hickson committed
1411
  @override
1412
  void insertRenderObjectChild(covariant RenderObject child, int slot) {
Adam Barth's avatar
Adam Barth committed
1413 1414
    assert(slot != null);
    assert(_currentlyUpdatingChildIndex == slot);
1415
    assert(renderObject.debugValidateChild(child));
1416
    renderObject.insert(child as RenderBox, after: _currentBeforeChild);
Ian Hickson's avatar
Ian Hickson committed
1417
    assert(() {
1418
      final SliverMultiBoxAdaptorParentData childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
Ian Hickson's avatar
Ian Hickson committed
1419 1420
      assert(slot == childParentData.index);
      return true;
1421
    }());
Ian Hickson's avatar
Ian Hickson committed
1422 1423 1424
  }

  @override
1425 1426 1427
  void moveRenderObjectChild(covariant RenderObject child, int oldSlot, int newSlot) {
    assert(newSlot != null);
    assert(_currentlyUpdatingChildIndex == newSlot);
1428
    renderObject.move(child as RenderBox, after: _currentBeforeChild);
Ian Hickson's avatar
Ian Hickson committed
1429 1430 1431
  }

  @override
1432
  void removeRenderObjectChild(covariant RenderObject child, int slot) {
Adam Barth's avatar
Adam Barth committed
1433
    assert(_currentlyUpdatingChildIndex != null);
1434
    renderObject.remove(child as RenderBox);
Ian Hickson's avatar
Ian Hickson committed
1435 1436 1437 1438
  }

  @override
  void visitChildren(ElementVisitor visitor) {
1439 1440
    // The toList() is to make a copy so that the underlying list can be modified by
    // the visitor:
1441 1442
    assert(!_childElements.values.any((Element? child) => child == null));
    _childElements.values.cast<Element>().toList().forEach(visitor);
Ian Hickson's avatar
Ian Hickson committed
1443
  }
1444 1445 1446

  @override
  void debugVisitOnstageChildren(ElementVisitor visitor) {
1447
    _childElements.values.cast<Element>().where((Element child) {
1448
      final SliverMultiBoxAdaptorParentData parentData = child.renderObject!.parentData! as SliverMultiBoxAdaptorParentData;
1449
      final double itemExtent;
1450 1451
      switch (renderObject.constraints.axis) {
        case Axis.horizontal:
1452
          itemExtent = child.renderObject!.paintBounds.width;
1453 1454
          break;
        case Axis.vertical:
1455
          itemExtent = child.renderObject!.paintBounds.height;
1456 1457 1458
          break;
      }

1459
      return parentData.layoutOffset != null &&
1460 1461
          parentData.layoutOffset! < renderObject.constraints.scrollOffset + renderObject.constraints.remainingPaintExtent &&
          parentData.layoutOffset! + itemExtent > renderObject.constraints.scrollOffset;
1462
    }).forEach(visitor);
1463
  }
Ian Hickson's avatar
Ian Hickson committed
1464
}
1465

Kate Lovett's avatar
Kate Lovett committed
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
/// A sliver widget that makes its sliver child partially transparent.
///
/// This class paints its sliver child into an intermediate buffer and then
/// blends the sliver back into the scene partially transparent.
///
/// For values of opacity other than 0.0 and 1.0, this class is relatively
/// expensive because it requires painting the sliver child into an intermediate
/// buffer. For the value 0.0, the sliver child is simply not painted at all.
/// For the value 1.0, the sliver child is painted immediately without an
/// intermediate buffer.
///
1477
/// {@tool snippet}
Kate Lovett's avatar
Kate Lovett committed
1478 1479 1480 1481 1482 1483
///
/// This example shows a [SliverList] when the `_visible` member field is true,
/// and hides it when it is false:
///
/// ```dart
/// bool _visible = true;
1484
/// List<Widget> listItems = const <Widget>[
Kate Lovett's avatar
Kate Lovett committed
1485
///   Text('Now you see me,'),
1486
///   Text("Now you don't!"),
Kate Lovett's avatar
Kate Lovett committed
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
/// ];
///
/// SliverOpacity(
///   opacity: _visible ? 1.0 : 0.0,
///   sliver: SliverList(
///     delegate: SliverChildListDelegate(listItems),
///   ),
/// )
/// ```
/// {@end-tool}
///
/// This is more efficient than adding and removing the sliver child widget
/// from the tree on demand.
///
/// See also:
///
///  * [Opacity], which can apply a uniform alpha effect to its child using the
///    RenderBox layout protocol.
///  * [AnimatedOpacity], which uses an animation internally to efficiently
///    animate [Opacity].
class SliverOpacity extends SingleChildRenderObjectWidget {
  /// Creates a sliver that makes its sliver child partially transparent.
  ///
  /// The [opacity] argument must not be null and must be between 0.0 and 1.0
  /// (inclusive).
  const SliverOpacity({
1513 1514
    Key? key,
    required this.opacity,
Kate Lovett's avatar
Kate Lovett committed
1515
    this.alwaysIncludeSemantics = false,
1516
    Widget? sliver,
1517 1518 1519
  }) : assert(opacity != null && opacity >= 0.0 && opacity <= 1.0),
       assert(alwaysIncludeSemantics != null),
       super(key: key, child: sliver);
Kate Lovett's avatar
Kate Lovett committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551

  /// The fraction to scale the sliver child's alpha value.
  ///
  /// An opacity of 1.0 is fully opaque. An opacity of 0.0 is fully transparent
  /// (i.e. invisible).
  ///
  /// The opacity must not be null.
  ///
  /// Values 1.0 and 0.0 are painted with a fast path. Other values
  /// require painting the sliver child into an intermediate buffer, which is
  /// expensive.
  final double opacity;

  /// Whether the semantic information of the sliver child is always included.
  ///
  /// Defaults to false.
  ///
  /// When true, regardless of the opacity settings, the sliver child semantic
  /// information is exposed as if the widget were fully visible. This is
  /// useful in cases where labels may be hidden during animations that
  /// would otherwise contribute relevant semantics.
  final bool alwaysIncludeSemantics;

  @override
  RenderSliverOpacity createRenderObject(BuildContext context) {
    return RenderSliverOpacity(
      opacity: opacity,
      alwaysIncludeSemantics: alwaysIncludeSemantics,
    );
  }

  @override
Kate Lovett's avatar
Kate Lovett committed
1552
  void updateRenderObject(BuildContext context, RenderSliverOpacity renderObject) {
Kate Lovett's avatar
Kate Lovett committed
1553 1554 1555 1556 1557 1558 1559 1560 1561
    renderObject
      ..opacity = opacity
      ..alwaysIncludeSemantics = alwaysIncludeSemantics;
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<double>('opacity', opacity));
1562 1563 1564 1565 1566
    properties.add(FlagProperty(
      'alwaysIncludeSemantics',
      value: alwaysIncludeSemantics,
      ifTrue: 'alwaysIncludeSemantics',
    ));
Kate Lovett's avatar
Kate Lovett committed
1567 1568 1569
  }
}

1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
/// A sliver widget that is invisible during hit testing.
///
/// When [ignoring] is true, this widget (and its subtree) is invisible
/// to hit testing. It still consumes space during layout and paints its sliver
/// child as usual. It just cannot be the target of located events, because it
/// returns false from [RenderSliver.hitTest].
///
/// When [ignoringSemantics] is true, the subtree will be invisible to
/// the semantics layer (and thus e.g. accessibility tools). If
/// [ignoringSemantics] is null, it uses the value of [ignoring].
class SliverIgnorePointer extends SingleChildRenderObjectWidget {
  /// Creates a sliver widget that is invisible to hit testing.
  ///
  /// The [ignoring] argument must not be null. If [ignoringSemantics] is null,
  /// this render object will be ignored for semantics if [ignoring] is true.
  const SliverIgnorePointer({
1586
    Key? key,
1587 1588
    this.ignoring = true,
    this.ignoringSemantics,
1589
    Widget? sliver,
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  }) : assert(ignoring != null),
       super(key: key, child: sliver);

  /// Whether this sliver is ignored during hit testing.
  ///
  /// Regardless of whether this sliver is ignored during hit testing, it will
  /// still consume space during layout and be visible during painting.
  final bool ignoring;

  /// Whether the semantics of this sliver is ignored when compiling the
  /// semantics tree.
  ///
  /// If null, defaults to value of [ignoring].
  ///
  /// See [SemanticsNode] for additional information about the semantics tree.
1605
  final bool? ignoringSemantics;
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

  @override
  RenderSliverIgnorePointer createRenderObject(BuildContext context) {
    return RenderSliverIgnorePointer(
      ignoring: ignoring,
      ignoringSemantics: ignoringSemantics,
    );
  }

  @override
  void updateRenderObject(BuildContext context, RenderSliverIgnorePointer renderObject) {
    renderObject
      ..ignoring = ignoring
      ..ignoringSemantics = ignoringSemantics;
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<bool>('ignoring', ignoring));
    properties.add(DiagnosticsProperty<bool>('ignoringSemantics', ignoringSemantics, defaultValue: null));
  }
}

Kate Lovett's avatar
Kate Lovett committed
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
/// A sliver that lays its sliver child out as if it was in the tree, but
/// without painting anything, without making the sliver child available for hit
/// testing, and without taking any room in the parent.
///
/// Animations continue to run in offstage sliver children, and therefore use
/// battery and CPU time, regardless of whether the animations end up being
/// visible.
///
/// To hide a sliver widget from view while it is
/// not needed, prefer removing the widget from the tree entirely rather than
/// keeping it alive in an [Offstage] subtree.
class SliverOffstage extends SingleChildRenderObjectWidget {
  /// Creates a sliver that visually hides its sliver child.
  const SliverOffstage({
1644
    Key? key,
Kate Lovett's avatar
Kate Lovett committed
1645
    this.offstage = true,
1646
    Widget? sliver,
Kate Lovett's avatar
Kate Lovett committed
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
  }) : assert(offstage != null),
       super(key: key, child: sliver);

  /// Whether the sliver child is hidden from the rest of the tree.
  ///
  /// If true, the sliver child is laid out as if it was in the tree, but
  /// without painting anything, without making the child available for hit
  /// testing, and without taking any room in the parent.
  ///
  /// If false, the sliver child is included in the tree as normal.
  final bool offstage;

  @override
  RenderSliverOffstage createRenderObject(BuildContext context) => RenderSliverOffstage(offstage: offstage);

  @override
  void updateRenderObject(BuildContext context, RenderSliverOffstage renderObject) {
    renderObject.offstage = offstage;
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<bool>('offstage', offstage));
  }

  @override
1674
  SingleChildRenderObjectElement createElement() => _SliverOffstageElement(this);
Kate Lovett's avatar
Kate Lovett committed
1675 1676 1677 1678 1679 1680
}

class _SliverOffstageElement extends SingleChildRenderObjectElement {
  _SliverOffstageElement(SliverOffstage widget) : super(widget);

  @override
1681
  SliverOffstage get widget => super.widget as SliverOffstage;
Kate Lovett's avatar
Kate Lovett committed
1682 1683 1684 1685 1686 1687 1688 1689

  @override
  void debugVisitOnstageChildren(ElementVisitor visitor) {
    if (!widget.offstage)
      super.debugVisitOnstageChildren(visitor);
  }
}

1690 1691 1692
/// Mark a child as needing to stay alive even when it's in a lazy list that
/// would otherwise remove it.
///
1693
/// This widget is for use in [SliverWithKeepAliveWidget]s, such as
1694
/// [SliverGrid] or [SliverList].
Ian Hickson's avatar
Ian Hickson committed
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
///
/// This widget is rarely used directly. The [SliverChildBuilderDelegate] and
/// [SliverChildListDelegate] delegates, used with [SliverList] and
/// [SliverGrid], as well as the scroll view counterparts [ListView] and
/// [GridView], have an `addAutomaticKeepAlives` feature, which is enabled by
/// default, and which causes [AutomaticKeepAlive] widgets to be inserted around
/// each child, causing [KeepAlive] widgets to be automatically added and
/// configured in response to [KeepAliveNotification]s.
///
/// Therefore, to keep a widget alive, it is more common to use those
/// notifications than to directly deal with [KeepAlive] widgets.
///
/// In practice, the simplest way to deal with these notifications is to mix
/// [AutomaticKeepAliveClientMixin] into one's [State]. See the documentation
/// for that mixin class for details.
1710
class KeepAlive extends ParentDataWidget<KeepAliveParentDataMixin> {
1711 1712 1713
  /// Marks a child as needing to remain alive.
  ///
  /// The [child] and [keepAlive] arguments must not be null.
1714
  const KeepAlive({
1715 1716 1717
    Key? key,
    required this.keepAlive,
    required Widget child,
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
  }) : 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) {
1729
    assert(renderObject.parentData is KeepAliveParentDataMixin);
1730
    final KeepAliveParentDataMixin parentData = renderObject.parentData! as KeepAliveParentDataMixin;
1731 1732
    if (parentData.keepAlive != keepAlive) {
      parentData.keepAlive = keepAlive;
1733
      final AbstractNode? targetParent = renderObject.parent;
1734 1735
      if (targetParent is RenderObject && !keepAlive)
        targetParent.markNeedsLayout(); // No need to redo layout if it became true.
1736 1737 1738
    }
  }

1739 1740 1741 1742 1743 1744 1745
  // 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;

1746 1747 1748
  @override
  Type get debugTypicalAncestorWidgetClass => SliverWithKeepAliveWidget;

1749
  @override
1750 1751
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
1752
    properties.add(DiagnosticsProperty<bool>('keepAlive', keepAlive));
1753 1754
  }
}
1755

1756
// Return a Widget for the given Exception
1757
Widget _createErrorWidget(Object exception, StackTrace stackTrace) {
1758 1759 1760 1761
  final FlutterErrorDetails details = FlutterErrorDetails(
    exception: exception,
    stack: stackTrace,
    library: 'widgets library',
1762
    context: ErrorDescription('building'),
1763 1764 1765 1766
  );
  FlutterError.reportError(details);
  return ErrorWidget.builder(details);
}