scroll_view.dart 64.5 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4
// 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.

5 6
import 'dart:math' as math;

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter/rendering.dart';
8
import 'package:flutter/gestures.dart';
Adam Barth's avatar
Adam Barth committed
9 10

import 'basic.dart';
11
import 'framework.dart';
12
import 'media_query.dart';
13
import 'primary_scroll_controller.dart';
14
import 'scroll_controller.dart';
15
import 'scroll_physics.dart';
Adam Barth's avatar
Adam Barth committed
16 17
import 'scrollable.dart';
import 'sliver.dart';
18
import 'viewport.dart';
Adam Barth's avatar
Adam Barth committed
19

20 21 22 23 24 25 26 27 28 29 30 31 32
/// A widget that scrolls.
///
/// Scrollable widgets consist of three pieces:
///
///  1. A [Scrollable] widget, which listens for various user gestures and
///     implements the interaction design for scrolling.
///  2. A viewport widget, such as [Viewport] or [ShrinkWrappingViewport], which
///     implements the visual design for scrolling by displaying only a portion
///     of the widgets inside the scroll view.
///  3. One or more slivers, which are widgets that can be composed to created
///     various scrolling effects, such as lists, grids, and expanding headers.
///
/// [ScrollView] helps orchestrate these pieces by creating the [Scrollable] and
33
/// the viewport and deferring to its subclass to create the slivers.
34
///
35 36 37
/// To control the initial scroll offset of the scroll view, provide a
/// [controller] with its [ScrollController.initialScrollOffset] property set.
///
38 39 40 41 42 43 44 45 46 47
/// See also:
///
///  * [ListView], which is a commonly used [ScrollView] that displays a
///    scrolling, linear list of child widgets.
///  * [PageView], which is a scrolling list of child widgets that are each the
///    size of the viewport.
///  * [GridView], which is a [ScrollView] that displays a scrolling, 2D array
///    of child widgets.
///  * [CustomScrollView], which is a [ScrollView] that creates custom scroll
///    effects using slivers.
48
///  * [ScrollNotification] and [NotificationListener], which can be used to watch
49
///    the scroll position without using a [ScrollController].
50
abstract class ScrollView extends StatelessWidget {
51 52 53
  /// Creates a widget that scrolls.
  ///
  /// If the [primary] argument is true, the [controller] must be null.
54 55 56 57 58 59
  ///
  /// If the [shrinkWrap] argument is true, the [center] argument must be null.
  ///
  /// The [scrollDirection], [reverse], and [shrinkWrap] arguments must not be null.
  ///
  /// The [anchor] argument must be non-null and in the range 0.0 to 1.0.
60
  const ScrollView({
Adam Barth's avatar
Adam Barth committed
61
    Key key,
62 63
    this.scrollDirection = Axis.vertical,
    this.reverse = false,
64
    this.controller,
65
    bool primary,
66
    ScrollPhysics physics,
67
    this.shrinkWrap = false,
68 69
    this.center,
    this.anchor = 0.0,
70
    this.cacheExtent,
71
    this.semanticChildCount,
72
    this.dragStartBehavior = DragStartBehavior.start,
73 74
  }) : assert(scrollDirection != null),
       assert(reverse != null),
75
       assert(shrinkWrap != null),
76
       assert(dragStartBehavior != null),
77
       assert(!(controller != null && primary == true),
78
           'Primary ScrollViews obtain their ScrollController via inheritance from a PrimaryScrollController widget. '
79
           'You cannot both set primary to true and pass an explicit controller.'
80
       ),
81 82 83
       assert(!shrinkWrap || center == null),
       assert(anchor != null),
       assert(anchor >= 0.0 && anchor <= 1.0),
84
       assert(semanticChildCount == null || semanticChildCount >= 0),
85 86
       primary = primary ?? controller == null && identical(scrollDirection, Axis.vertical),
       physics = physics ?? (primary == true || (primary == null && controller == null && identical(scrollDirection, Axis.vertical)) ? const AlwaysScrollableScrollPhysics() : null),
87
       super(key: key);
Adam Barth's avatar
Adam Barth committed
88

89 90 91
  /// The axis along which the scroll view scrolls.
  ///
  /// Defaults to [Axis.vertical].
Adam Barth's avatar
Adam Barth committed
92 93
  final Axis scrollDirection;

94
  /// Whether the scroll view scrolls in the reading direction.
95 96 97 98 99 100
  ///
  /// For example, if the reading direction is left-to-right and
  /// [scrollDirection] is [Axis.horizontal], then the scroll view scrolls from
  /// left to right when [reverse] is false and from right to left when
  /// [reverse] is true.
  ///
Adam Barth's avatar
Adam Barth committed
101
  /// Similarly, if [scrollDirection] is [Axis.vertical], then the scroll view
102 103 104 105
  /// scrolls from top to bottom when [reverse] is false and from bottom to top
  /// when [reverse] is true.
  ///
  /// Defaults to false.
106 107
  final bool reverse;

108 109 110 111
  /// An object that can be used to control the position to which this scroll
  /// view is scrolled.
  ///
  /// Must be null if [primary] is true.
112 113 114 115 116 117 118 119
  ///
  /// A [ScrollController] serves several purposes. It can be used to control
  /// the initial scroll position (see [ScrollController.initialScrollOffset]).
  /// It can be used to control whether the scroll view should automatically
  /// save and restore its scroll position in the [PageStorage] (see
  /// [ScrollController.keepScrollOffset]). It can be used to read the current
  /// scroll position (see [ScrollController.offset]), or change it (see
  /// [ScrollController.animateTo]).
120 121
  final ScrollController controller;

122 123 124
  /// Whether this is the primary scroll view associated with the parent
  /// [PrimaryScrollController].
  ///
125 126 127 128 129
  /// When this is true, the scroll view is scrollable even if it does not have
  /// sufficient content to actually scroll. Otherwise, by default the user can
  /// only scroll the view if it has sufficient content. See [physics].
  ///
  /// On iOS, this also identifies the scroll view that will scroll to top in
130 131
  /// response to a tap in the status bar.
  ///
132 133
  /// Defaults to true when [scrollDirection] is [Axis.vertical] and
  /// [controller] is null.
134 135
  final bool primary;

136 137 138 139 140
  /// How the scroll view should respond to user input.
  ///
  /// For example, determines how the scroll view continues to animate after the
  /// user stops dragging the scroll view.
  ///
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  /// Defaults to matching platform conventions. Furthermore, if [primary] is
  /// false, then the user cannot scroll if there is insufficient content to
  /// scroll, while if [primary] is true, they can always attempt to scroll.
  ///
  /// To force the scroll view to always be scrollable even if there is
  /// insufficient content, as if [primary] was true but without necessarily
  /// setting it to true, provide an [AlwaysScrollableScrollPhysics] physics
  /// object, as in:
  ///
  /// ```dart
  ///   physics: const AlwaysScrollableScrollPhysics(),
  /// ```
  ///
  /// To force the scroll view to use the default platform conventions and not
  /// be scrollable if there is insufficient content, regardless of the value of
  /// [primary], provide an explicit [ScrollPhysics] object, as in:
  ///
  /// ```dart
  ///   physics: const ScrollPhysics(),
  /// ```
161 162 163 164 165 166 167 168 169
  ///
  /// The physics can be changed dynamically (by providing a new object in a
  /// subsequent build), but new physics will only take effect if the _class_ of
  /// the provided object changes. Merely constructing a new instance with a
  /// different configuration is insufficient to cause the physics to be
  /// reapplied. (This is because the final object used is generated
  /// dynamically, which can be relatively expensive, and it would be
  /// inefficient to speculatively create this object each frame to see if the
  /// physics should be updated.)
Adam Barth's avatar
Adam Barth committed
170 171
  final ScrollPhysics physics;

172 173 174 175 176 177 178 179 180 181 182 183 184 185
  /// Whether the extent of the scroll view in the [scrollDirection] should be
  /// determined by the contents being viewed.
  ///
  /// If the scroll view does not shrink wrap, then the scroll view will expand
  /// to the maximum allowed size in the [scrollDirection]. If the scroll view
  /// has unbounded constraints in the [scrollDirection], then [shrinkWrap] must
  /// be true.
  ///
  /// Shrink wrapping the content of the scroll view is significantly more
  /// expensive than expanding to the maximum allowed size because the content
  /// can expand and contract during scrolling, which means the size of the
  /// scroll view needs to be recomputed whenever the scroll position changes.
  ///
  /// Defaults to false.
186 187
  final bool shrinkWrap;

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  /// The first child in the [GrowthDirection.forward] growth direction.
  ///
  /// Children after [center] will be placed in the [axisDirection] relative to
  /// the [center]. Children before [center] will be placed in the opposite of
  /// the [axisDirection] relative to the [center].
  ///
  /// The [center] must be the key of one of the slivers built by [buildSlivers].
  ///
  /// Of the built-in subclasses of [ScrollView], only [CustomScrollView]
  /// supports [center]; for that class, the given key must be the key of one of
  /// the slivers in the [CustomScrollView.slivers] list.
  ///
  /// See also:
  ///
  ///  * [anchor], which controls where the [center] as aligned in the viewport.
  final Key center;

  /// The relative position of the zero scroll offset.
  ///
  /// For example, if [anchor] is 0.5 and the [axisDirection] is
  /// [AxisDirection.down] or [AxisDirection.up], then the zero scroll offset is
  /// vertically centered within the viewport. If the [anchor] is 1.0, and the
  /// [axisDirection] is [AxisDirection.right], then the zero scroll offset is
  /// on the left edge of the viewport.
  final double anchor;

214 215 216
  /// {@macro flutter.rendering.viewport.cacheExtent}
  final double cacheExtent;

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  /// The number of children that will contribute semantic information.
  ///
  /// Some subtypes of [ScrollView] can infer this value automatically. For
  /// example [ListView] will use the number of widgets in the child list,
  /// while the [new ListView.separated] constructor will use half that amount.
  ///
  /// For [CustomScrollView] and other types which do not receive a builder
  /// or list of widgets, the child count must be explicitly provided. If the
  /// number is unknown or unbounded this should be left unset or set to null.
  ///
  /// See also:
  ///
  ///  * [SemanticsConfiguration.scrollChildCount], the corresponding semantics property.
  final int semanticChildCount;

232 233 234
  /// {@macro flutter.widgets.scrollable.dragStartBehavior}
  final DragStartBehavior dragStartBehavior;

235 236 237 238 239
  /// Returns the [AxisDirection] in which the scroll view scrolls.
  ///
  /// Combines the [scrollDirection] with the [reverse] boolean to obtain the
  /// concrete [AxisDirection].
  ///
240
  /// If the [scrollDirection] is [Axis.horizontal], the ambient
241
  /// [Directionality] is also considered when selecting the concrete
242 243 244 245
  /// [AxisDirection]. For example, if the ambient [Directionality] is
  /// [TextDirection.rtl], then the non-reversed [AxisDirection] is
  /// [AxisDirection.left] and the reversed [AxisDirection] is
  /// [AxisDirection.right].
246 247
  @protected
  AxisDirection getDirection(BuildContext context) {
248
    return getAxisDirectionFromAxisReverseAndDirectionality(context, scrollDirection, reverse);
249 250
  }

251 252
  /// Build the list of widgets to place inside the viewport.
  ///
253 254
  /// Subclasses should override this method to build the slivers for the inside
  /// of the viewport.
255
  @protected
256
  List<Widget> buildSlivers(BuildContext context);
Adam Barth's avatar
Adam Barth committed
257

258 259 260 261 262
  /// Build the viewport.
  ///
  /// Subclasses may override this method to change how the viewport is built.
  /// The default implementation uses a [ShrinkWrappingViewport] if [shrinkWrap]
  /// is true, and a regular [Viewport] otherwise.
263 264 265 266 267 268 269 270
  ///
  /// The `offset` argument is the value obtained from
  /// [Scrollable.viewportBuilder].
  ///
  /// The `axisDirection` argument is the value obtained from [getDirection],
  /// which by default uses [scrollDirection] and [reverse].
  ///
  /// The `slivers` argument is the value obtained from [buildSlivers].
271 272 273 274 275 276 277 278
  @protected
  Widget buildViewport(
    BuildContext context,
    ViewportOffset offset,
    AxisDirection axisDirection,
    List<Widget> slivers,
  ) {
    if (shrinkWrap) {
279
      return ShrinkWrappingViewport(
280 281 282 283 284
        axisDirection: axisDirection,
        offset: offset,
        slivers: slivers,
      );
    }
285
    return Viewport(
286 287 288
      axisDirection: axisDirection,
      offset: offset,
      slivers: slivers,
289
      cacheExtent: cacheExtent,
290 291
      center: center,
      anchor: anchor,
292 293 294
    );
  }

295 296
  @override
  Widget build(BuildContext context) {
297 298
    final List<Widget> slivers = buildSlivers(context);
    final AxisDirection axisDirection = getDirection(context);
299

300
    final ScrollController scrollController = primary
Ian Hickson's avatar
Ian Hickson committed
301 302
      ? PrimaryScrollController.of(context)
      : controller;
303
    final Scrollable scrollable = Scrollable(
304
      dragStartBehavior: dragStartBehavior,
305
      axisDirection: axisDirection,
306
      controller: scrollController,
Adam Barth's avatar
Adam Barth committed
307
      physics: physics,
308
      semanticChildCount: semanticChildCount,
309
      viewportBuilder: (BuildContext context, ViewportOffset offset) {
310 311
        return buildViewport(context, offset, axisDirection, slivers);
      },
312
    );
313
    return primary && scrollController != null
314
      ? PrimaryScrollController.none(child: scrollable)
315
      : scrollable;
316
  }
317 318

  @override
319 320
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
321 322 323 324 325 326
    properties.add(EnumProperty<Axis>('scrollDirection', scrollDirection));
    properties.add(FlagProperty('reverse', value: reverse, ifTrue: 'reversed', showName: true));
    properties.add(DiagnosticsProperty<ScrollController>('controller', controller, showName: false, defaultValue: null));
    properties.add(FlagProperty('primary', value: primary, ifTrue: 'using primary controller', showName: true));
    properties.add(DiagnosticsProperty<ScrollPhysics>('physics', physics, showName: false, defaultValue: null));
    properties.add(FlagProperty('shrinkWrap', value: shrinkWrap, ifTrue: 'shrink-wrapping', showName: true));
327 328 329
  }
}

330 331 332 333 334 335 336 337
/// A [ScrollView] that creates custom scroll effects using slivers.
///
/// A [CustomScrollView] lets you supply [slivers] directly to create various
/// scrolling effects, such as lists, grids, and expanding headers. For example,
/// to create a scroll view that contains an expanding app bar followed by a
/// list and a grid, use a list of three slivers: [SliverAppBar], [SliverList],
/// and [SliverGrid].
///
338 339
/// [Widget]s in these [slivers] must produce [RenderSliver] objects.
///
340 341 342
/// To control the initial scroll offset of the scroll view, provide a
/// [controller] with its [ScrollController.initialScrollOffset] property set.
///
343 344
/// {@animation 400 376 https://flutter.github.io/assets-for-api-docs/assets/widgets/custom_scroll_view.mp4}
///
345
/// {@tool sample}
346 347 348 349 350
///
/// This sample code shows a scroll view that contains a flexible pinned app
/// bar, a grid, and an infinite list.
///
/// ```dart
351
/// CustomScrollView(
352 353 354 355
///   slivers: <Widget>[
///     const SliverAppBar(
///       pinned: true,
///       expandedHeight: 250.0,
356 357
///       flexibleSpace: FlexibleSpaceBar(
///         title: Text('Demo'),
358 359
///       ),
///     ),
360 361
///     SliverGrid(
///       gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
362 363 364 365 366
///         maxCrossAxisExtent: 200.0,
///         mainAxisSpacing: 10.0,
///         crossAxisSpacing: 10.0,
///         childAspectRatio: 4.0,
///       ),
367
///       delegate: SliverChildBuilderDelegate(
368
///         (BuildContext context, int index) {
369
///           return Container(
370
///             alignment: Alignment.center,
371
///             color: Colors.teal[100 * (index % 9)],
372
///             child: Text('Grid Item $index'),
373 374 375 376 377
///           );
///         },
///         childCount: 20,
///       ),
///     ),
378
///     SliverFixedExtentList(
379
///       itemExtent: 50.0,
380
///       delegate: SliverChildBuilderDelegate(
381
///         (BuildContext context, int index) {
382
///           return Container(
383
///             alignment: Alignment.center,
384
///             color: Colors.lightBlue[100 * (index % 9)],
385
///             child: Text('List Item $index'),
386 387 388 389 390 391 392
///           );
///         },
///       ),
///     ),
///   ],
/// )
/// ```
393
/// {@end-tool}
394
///
395 396 397 398 399
/// ## Accessibility
///
/// A [CustomScrollView] can allow Talkback/VoiceOver to make announcements
/// to the user when the scroll state changes. For example, on Android an
/// announcement might be read as "showing items 1 to 10 of 23". To produce
400
/// this announcement, the scroll view needs three pieces of information:
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
///
///   * The first visible child index.
///   * The total number of children.
///   * The total number of visible children.
///
/// The last value can be computed exactly by the framework, however the first
/// two must be provided. Most of the higher-level scrollable widgets provide
/// this information automatically. For example, [ListView] provides each child
/// widget with a semantic index automatically and sets the semantic child
/// count to the length of the list.
///
/// To determine visible indexes, the scroll view needs a way to associate the
/// generated semantics of each scrollable item with a semantic index. This can
/// be done by wrapping the child widgets in an [IndexedSemantics].
///
416
/// This semantic index is not necessarily the same as the index of the widget in
Ian Hickson's avatar
Ian Hickson committed
417 418
/// the scrollable, because some widgets may not contribute semantic
/// information. Consider a [new ListView.separated()]: every other widget is a
419 420 421
/// divider with no semantic information. In this case, only odd numbered
/// widgets have a semantic index (equal to the index ~/ 2). Furthermore, the
/// total number of children in this example would be half the number of
Ian Hickson's avatar
Ian Hickson committed
422 423
/// widgets. (The [new ListView.separated()] constructor handles this
/// automatically; this is only used here as an example.)
424 425 426 427 428
///
/// The total number of visible children can be provided by the constructor
/// parameter `semanticChildCount`. This should always be the same as the
/// number of widgets wrapped in [IndexedSemantics].
///
429 430 431 432 433 434 435 436 437 438
/// See also:
///
///  * [SliverList], which is a sliver that displays linear list of children.
///  * [SliverFixedExtentList], which is a more efficient sliver that displays
///    linear list of children that have the same extent along the scroll axis.
///  * [SliverGrid], which is a sliver that displays a 2D array of children.
///  * [SliverPadding], which is a sliver that adds blank space around another
///    sliver.
///  * [SliverAppBar], which is a sliver that displays a header that can expand
///    and float as the scroll view scrolls.
439
///  * [ScrollNotification] and [NotificationListener], which can be used to watch
440
///    the scroll position without using a [ScrollController].
441 442
///  * [IndexedSemantics], which allows annotating child lists with an index
///    for scroll announcements.
Adam Barth's avatar
Adam Barth committed
443
class CustomScrollView extends ScrollView {
444 445
  /// Creates a [ScrollView] that creates custom scroll effects using slivers.
  ///
446
  /// See the [new ScrollView] constructor for more details on these arguments.
447
  const CustomScrollView({
Adam Barth's avatar
Adam Barth committed
448
    Key key,
449 450
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
451
    ScrollController controller,
452
    bool primary,
Adam Barth's avatar
Adam Barth committed
453
    ScrollPhysics physics,
454
    bool shrinkWrap = false,
455 456
    Key center,
    double anchor = 0.0,
457
    double cacheExtent,
458
    this.slivers = const <Widget>[],
459
    int semanticChildCount,
460
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
Adam Barth's avatar
Adam Barth committed
461 462 463 464
  }) : super(
    key: key,
    scrollDirection: scrollDirection,
    reverse: reverse,
465
    controller: controller,
466
    primary: primary,
Adam Barth's avatar
Adam Barth committed
467 468
    physics: physics,
    shrinkWrap: shrinkWrap,
469 470
    center: center,
    anchor: anchor,
471
    cacheExtent: cacheExtent,
472
    semanticChildCount: semanticChildCount,
473
    dragStartBehavior: dragStartBehavior,
Adam Barth's avatar
Adam Barth committed
474 475
  );

476
  /// The slivers to place inside the viewport.
Adam Barth's avatar
Adam Barth committed
477 478 479 480 481 482
  final List<Widget> slivers;

  @override
  List<Widget> buildSlivers(BuildContext context) => slivers;
}

483
/// A [ScrollView] that uses a single child layout model.
484 485 486 487 488 489 490
///
/// See also:
///
///  * [ListView], which is a [BoxScrollView] that uses a linear layout model.
///  * [GridView], which is a [BoxScrollView] that uses a 2D layout model.
///  * [CustomScrollView], which can combine multiple child layout models into a
///    single scroll view.
491
abstract class BoxScrollView extends ScrollView {
492 493 494
  /// Creates a [ScrollView] uses a single child layout model.
  ///
  /// If the [primary] argument is true, the [controller] must be null.
495
  const BoxScrollView({
496
    Key key,
497 498
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
499
    ScrollController controller,
500
    bool primary,
501
    ScrollPhysics physics,
502
    bool shrinkWrap = false,
503
    this.padding,
504
    double cacheExtent,
505
    int semanticChildCount,
506
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
507 508 509 510
  }) : super(
    key: key,
    scrollDirection: scrollDirection,
    reverse: reverse,
511
    controller: controller,
512
    primary: primary,
513 514
    physics: physics,
    shrinkWrap: shrinkWrap,
515
    cacheExtent: cacheExtent,
516
    semanticChildCount: semanticChildCount,
517
    dragStartBehavior: dragStartBehavior,
518 519
  );

520
  /// The amount of space by which to inset the children.
521
  final EdgeInsetsGeometry padding;
522 523 524 525

  @override
  List<Widget> buildSlivers(BuildContext context) {
    Widget sliver = buildChildLayout(context);
526 527 528 529 530 531 532 533 534 535 536 537 538 539
    EdgeInsetsGeometry effectivePadding = padding;
    if (padding == null) {
      final MediaQueryData mediaQuery = MediaQuery.of(context, nullOk: true);
      if (mediaQuery != null) {
        // Automatically pad sliver with padding from MediaQuery.
        final EdgeInsets mediaQueryHorizontalPadding =
            mediaQuery.padding.copyWith(top: 0.0, bottom: 0.0);
        final EdgeInsets mediaQueryVerticalPadding =
            mediaQuery.padding.copyWith(left: 0.0, right: 0.0);
        // Consume the main axis padding with SliverPadding.
        effectivePadding = scrollDirection == Axis.vertical
            ? mediaQueryVerticalPadding
            : mediaQueryHorizontalPadding;
        // Leave behind the cross axis padding.
540
        sliver = MediaQuery(
541 542 543 544 545 546 547 548 549 550 551
          data: mediaQuery.copyWith(
            padding: scrollDirection == Axis.vertical
                ? mediaQueryHorizontalPadding
                : mediaQueryVerticalPadding,
          ),
          child: sliver,
        );
      }
    }

    if (effectivePadding != null)
552
      sliver = SliverPadding(padding: effectivePadding, sliver: sliver);
553 554 555
    return <Widget>[ sliver ];
  }

556
  /// Subclasses should override this method to build the layout model.
557 558 559 560
  @protected
  Widget buildChildLayout(BuildContext context);

  @override
561 562
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
563
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
564 565 566
  }
}

567
/// A scrollable list of widgets arranged linearly.
568
///
569 570
/// {@youtube 560 315 https://www.youtube.com/watch?v=KJpkjHGiI5A}
///
571 572 573 574 575 576 577 578 579 580
/// [ListView] is the most commonly used scrolling widget. It displays its
/// children one after another in the scroll direction. In the cross axis, the
/// children are required to fill the [ListView].
///
/// If non-null, the [itemExtent] forces the children to have the given extent
/// in the scroll direction. Specifying an [itemExtent] is more efficient than
/// letting the children determine their own extent because the scrolling
/// machinery can make use of the foreknowledge of the children's extent to save
/// work, for example when the scroll position changes drastically.
///
581
/// There are four options for constructing a [ListView]:
582
///
583
///  1. The default constructor takes an explicit [List<Widget>] of children. This
584 585 586 587 588
///     constructor is appropriate for list views with a small number of
///     children because constructing the [List] requires doing work for every
///     child that could possibly be displayed in the list view instead of just
///     those children that are actually visible.
///
589 590 591
///  2. The [ListView.builder] constructor takes an [IndexedWidgetBuilder], which
///     builds the children on demand. This constructor is appropriate for list views
///     with a large (or infinite) number of children because the builder is called
592 593
///     only for those children that are actually visible.
///
594 595 596 597 598 599 600
///  3. The [ListView.separated] constructor takes two [IndexedWidgetBuilder]s:
///     `itemBuilder` builds child items on demand, and `separatorBuilder`
///     similarly builds separator children which appear in between the child items.
///     This constructor is appropriate for list views with a fixed number of children.
///
///  4. The [ListView.custom] constructor takes a [SliverChildDelegate], which provides
///     the ability to customize additional aspects of the child model. For example,
601 602
///     a [SliverChildDelegate] can control the algorithm used to estimate the
///     size of children that are not actually visible.
603
///
604 605 606
/// To control the initial scroll offset of the scroll view, provide a
/// [controller] with its [ScrollController.initialScrollOffset] property set.
///
607 608 609 610
/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.
///
611
/// {@tool sample}
612 613 614
/// This example uses the default constructor for [ListView] which takes an
/// explicit [List<Widget>] of children. This [ListView]'s children are made up
/// of [Container]s with [Text].
615
///
616 617
/// ![A ListView of 3 amber colored containers with sample text.](https://flutter.github.io/assets-for-api-docs/assets/widgets/list_view.png)
///
618 619
/// ```dart
/// ListView(
620
///   padding: const EdgeInsets.all(8),
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
///   children: <Widget>[
///     Container(
///       height: 50,
///       color: Colors.amber[600],
///       child: const Center(child: Text('Entry A')),
///     ),
///     Container(
///       height: 50,
///       color: Colors.amber[500],
///       child: const Center(child: Text('Entry B')),
///     ),
///     Container(
///       height: 50,
///       color: Colors.amber[100],
///       child: const Center(child: Text('Entry C')),
///     ),
///   ],
/// )
/// ```
/// {@end-tool}
641
///
642 643 644 645
/// {@tool sample}
/// This example mirrors the previous one, creating the same list using the
/// [ListView.builder] constructor. Using the [IndexedWidgetBuilder], children
/// are built lazily and can be infinite in number.
646
///
647 648
/// ![A ListView of 3 amber colored containers with sample text.](https://flutter.github.io/assets-for-api-docs/assets/widgets/list_view_builder.png)
///
649
/// ```dart
650 651 652
/// final List<String> entries = <String>['A', 'B', 'C'];
/// final List<int> colorCodes = <int>[600, 500, 100];
///
653
/// ListView.builder(
654
///   padding: const EdgeInsets.all(8),
655
///   itemCount: entries.length,
656
///   itemBuilder: (BuildContext context, int index) {
657 658 659 660 661 662 663 664 665
///     return Container(
///       height: 50,
///       color: Colors.amber[colorCodes[index]],
///       child: Center(child: Text('Entry ${entries[index]}')),
///     );
///   }
/// );
/// ```
/// {@end-tool}
666
///
667 668 669 670 671
/// {@tool sample}
/// This example continues to build from our the previous ones, creating a
/// similar list using [ListView.separated]. Here, a [Divider] is used as a
/// separator.
///
672 673 674
/// ![A ListView of 3 amber colored containers with sample text and a Divider
/// between each of them.](https://flutter.github.io/assets-for-api-docs/assets/widgets/list_view_separated.png)
///
675 676 677 678 679
/// ```dart
/// final List<String> entries = <String>['A', 'B', 'C'];
/// final List<int> colorCodes = <int>[600, 500, 100];
///
/// ListView.separated(
680
///   padding: const EdgeInsets.all(8),
681 682 683 684 685 686 687
///   itemCount: entries.length,
///   itemBuilder: (BuildContext context, int index) {
///     return Container(
///       height: 50,
///       color: Colors.amber[colorCodes[index]],
///       child: Center(child: Text('Entry ${entries[index]}')),
///     );
688
///   },
689 690
///   separatorBuilder: (BuildContext context, int index) => const Divider(),
/// );
691
/// ```
692
/// {@end-tool}
693
///
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
/// ## 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 when using
/// the default constructor) or lazily provided ones (such as when using the
/// [ListView.builder] constructor).
///
/// ### 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 list 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 list 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 list 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 list child widget subtree
///    that needs to be preserved. The [KeepAlive] widget marks the child
725
///    subtree's top render object child for keepalive. When the associated top
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
///    render object is scrolled out of view, the list 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 [addAutomaticKeepAlives] and [addRepaintBoundaries]
///    are false since those parameters cause the [ListView] to wrap each child
///    widget subtree with other widgets.
///
///  * Using [AutomaticKeepAlive] widgets (inserted by default when
///    [addAutomaticKeepAlives] is true). Instead of unconditionally caching the
///    child element subtree when scrolling off-screen like [KeepAlive],
///    [AutomaticKeepAlive] can let whether to cache the subtree be determined
///    by descendant logic in the subtree.
///
///    As an example, the [EditableText] widget signals its list child element
///    subtree to stay alive while its text field has input focus. If it doesn't
743
///    have focus and no other descendants signaled for keepalive via a
744 745 746 747 748 749 750
///    [KeepAliveNotification], the list 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
///    [wantKeepAlive] getter and calling [updateKeepAlive].
///
751 752 753
/// ## Transitioning to [CustomScrollView]
///
/// A [ListView] is basically a [CustomScrollView] with a single [SliverList] in
754
/// its [CustomScrollView.slivers] property.
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
///
/// If [ListView] is no longer sufficient, for example because the scroll view
/// is to have both a list and a grid, or because the list is to be combined
/// with a [SliverAppBar], etc, it is straight-forward to port code from using
/// [ListView] to using [CustomScrollView] directly.
///
/// The [key], [scrollDirection], [reverse], [controller], [primary], [physics],
/// and [shrinkWrap] properties on [ListView] map directly to the identically
/// named properties on [CustomScrollView].
///
/// The [CustomScrollView.slivers] property should be a list containing either a
/// [SliverList] or a [SliverFixedExtentList]; the former if [itemExtent] on the
/// [ListView] was null, and the latter if [itemExtent] was not null.
///
/// The [childrenDelegate] property on [ListView] corresponds to the
/// [SliverList.delegate] (or [SliverFixedExtentList.delegate]) property. The
/// [new ListView] constructor's `children` argument corresponds to the
/// [childrenDelegate] being a [SliverChildListDelegate] with that same
/// argument. The [new ListView.builder] constructor's `itemBuilder` and
Ian Hickson's avatar
Ian Hickson committed
774 775
/// `itemCount` arguments correspond to the [childrenDelegate] being a
/// [SliverChildBuilderDelegate] with the equivalent arguments.
776 777 778 779 780
///
/// The [padding] property corresponds to having a [SliverPadding] in the
/// [CustomScrollView.slivers] property instead of the list itself, and having
/// the [SliverList] instead be a child of the [SliverPadding].
///
781 782 783
/// [CustomScrollView]s don't automatically avoid obstructions from [MediaQuery]
/// like [ListView]s do. To reproduce the behavior, wrap the slivers in
/// [SliverSafeArea]s.
784
///
785 786 787 788
/// Once code has been ported to use [CustomScrollView], other slivers, such as
/// [SliverGrid] or [SliverAppBar], can be put in the [CustomScrollView.slivers]
/// list.
///
789
/// {@tool sample}
790 791 792 793 794
///
/// Here are two brief snippets showing a [ListView] and its equivalent using
/// [CustomScrollView]:
///
/// ```dart
795
/// ListView(
796 797 798 799 800 801 802 803 804 805
///   shrinkWrap: true,
///   padding: const EdgeInsets.all(20.0),
///   children: <Widget>[
///     const Text('I\'m dedicating every day to you'),
///     const Text('Domestic life was never quite my style'),
///     const Text('When you smile, you knock me out, I fall apart'),
///     const Text('And I thought I was so smart'),
///   ],
/// )
/// ```
806 807
/// {@end-tool}
/// {@tool sample}
808 809
///
/// ```dart
810
/// CustomScrollView(
811 812
///   shrinkWrap: true,
///   slivers: <Widget>[
813
///     SliverPadding(
814
///       padding: const EdgeInsets.all(20.0),
815 816
///       sliver: SliverList(
///         delegate: SliverChildListDelegate(
817 818 819 820 821 822 823 824 825 826 827 828
///           <Widget>[
///             const Text('I\'m dedicating every day to you'),
///             const Text('Domestic life was never quite my style'),
///             const Text('When you smile, you knock me out, I fall apart'),
///             const Text('And I thought I was so smart'),
///           ],
///         ),
///       ),
///     ),
///   ],
/// )
/// ```
829
/// {@end-tool}
830
///
831 832
/// See also:
///
833 834 835 836 837 838 839
///  * [SingleChildScrollView], which is a scrollable widget that has a single
///    child.
///  * [PageView], which is a scrolling list of child widgets that are each the
///    size of the viewport.
///  * [GridView], which is scrollable, 2D array of widgets.
///  * [CustomScrollView], which is a scrollable widget that creates custom
///    scroll effects using slivers.
840 841
///  * [ListBody], which arranges its children in a similar manner, but without
///    scrolling.
842
///  * [ScrollNotification] and [NotificationListener], which can be used to watch
843
///    the scroll position without using a [ScrollController].
844
class ListView extends BoxScrollView {
845 846 847 848 849 850
  /// Creates a scrollable, linear array of widgets from an explicit [List].
  ///
  /// This constructor is appropriate for list views with a small number of
  /// children because constructing the [List] requires doing work for every
  /// child that could possibly be displayed in the list view instead of just
  /// those children that are actually visible.
851 852 853
  ///
  /// It is usually more efficient to create children on demand using [new
  /// ListView.builder].
854
  ///
855 856 857
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildListDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
858 859 860 861
  /// [SliverChildListDelegate.addRepaintBoundaries] property. The
  /// `addSemanticIndexes` argument corresponds to the
  /// [SliverChildListDelegate.addSemanticIndexes] property. None
  /// may be null.
862 863
  ListView({
    Key key,
864 865
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
866
    ScrollController controller,
867
    bool primary,
868
    ScrollPhysics physics,
869
    bool shrinkWrap = false,
870
    EdgeInsetsGeometry padding,
871
    this.itemExtent,
872 873
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
874
    bool addSemanticIndexes = true,
875
    double cacheExtent,
876
    List<Widget> children = const <Widget>[],
877
    int semanticChildCount,
878
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
879
  }) : childrenDelegate = SliverChildListDelegate(
880
         children,
881
         addAutomaticKeepAlives: addAutomaticKeepAlives,
882
         addRepaintBoundaries: addRepaintBoundaries,
883
         addSemanticIndexes: addSemanticIndexes,
884 885 886 887 888 889 890 891 892 893 894 895 896 897
       ),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount ?? children.length,
         dragStartBehavior: dragStartBehavior,
       );
898

899 900 901 902 903 904
  /// Creates a scrollable, linear array of widgets that are created on demand.
  ///
  /// This constructor is appropriate for list views with a large (or infinite)
  /// number of children because the builder is called only for those children
  /// that are actually visible.
  ///
905
  /// Providing a non-null `itemCount` improves the ability of the [ListView] to
906 907
  /// estimate the maximum scroll extent.
  ///
908 909 910 911 912 913 914 915 916
  /// The `itemBuilder` callback will be called only with indices greater than
  /// or equal to zero and less than `itemCount`.
  ///
  /// The `itemBuilder` should actually create the widget instances when called.
  /// Avoid using a builder that returns a previously-constructed widget; if the
  /// list view's children are created in advance, or all at once when the
  /// [ListView] itself is created, it is more efficient to use [new ListView].
  /// Even more efficient, however, is to create the instances on demand using
  /// this constructor's `itemBuilder` callback.
917
  ///
918 919 920
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildBuilderDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
921 922 923 924
  /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. The
  /// `addSemanticIndexes` argument corresponds to the
  /// [SliverChildBuilderDelegate.addSemanticIndexes] property. None may be
  /// null.
925 926 927 928
  ///
  /// [ListView.builder] by default does not support child reordering. If
  /// you are planning to change child order at a later time, consider using
  /// [ListView] or [ListView.custom].
929 930
  ListView.builder({
    Key key,
931 932
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
933
    ScrollController controller,
934
    bool primary,
935
    ScrollPhysics physics,
936
    bool shrinkWrap = false,
937
    EdgeInsetsGeometry padding,
938
    this.itemExtent,
939
    @required IndexedWidgetBuilder itemBuilder,
940
    int itemCount,
941 942
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
943
    bool addSemanticIndexes = true,
944
    double cacheExtent,
945
    int semanticChildCount,
946
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
947 948 949
  }) : assert(itemCount == null || itemCount >= 0),
       assert(semanticChildCount == null || semanticChildCount <= itemCount),
       childrenDelegate = SliverChildBuilderDelegate(
950 951
         itemBuilder,
         childCount: itemCount,
952
         addAutomaticKeepAlives: addAutomaticKeepAlives,
953
         addRepaintBoundaries: addRepaintBoundaries,
954
         addSemanticIndexes: addSemanticIndexes,
955 956 957 958 959 960 961 962 963 964 965 966 967 968
       ),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount ?? itemCount,
         dragStartBehavior: dragStartBehavior,
       );
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

  /// Creates a fixed-length scrollable linear array of list "items" separated
  /// by list item "separators".
  ///
  /// This constructor is appropriate for list views with a large number of
  /// item and separator children because the builders are only called for
  /// the children that are actually visible.
  ///
  /// The `itemBuilder` callback will be called with indices greater than
  /// or equal to zero and less than `itemCount`.
  ///
  /// Separators only appear between list items: separator 0 appears after item
  /// 0 and the last separator appears before the last item.
  ///
  /// The `separatorBuilder` callback will be called with indices greater than
  /// or equal to zero and less than `itemCount - 1`.
  ///
  /// The `itemBuilder` and `separatorBuilder` callbacks should actually create
  /// widget instances when called. Avoid using a builder that returns a
  /// previously-constructed widget; if the list view's children are created in
  /// advance, or all at once when the [ListView] itself is created, it is more
  /// efficient to use [new ListView].
  ///
992
  /// {@tool sample}
993 994 995 996 997
  ///
  /// This example shows how to create [ListView] whose [ListTile] list items
  /// are separated by [Divider]s.
  ///
  /// ```dart
998
  /// ListView.separated(
999
  ///   itemCount: 25,
1000
  ///   separatorBuilder: (BuildContext context, int index) => Divider(),
1001
  ///   itemBuilder: (BuildContext context, int index) {
1002 1003
  ///     return ListTile(
  ///       title: Text('item $index'),
1004 1005 1006 1007
  ///     );
  ///   },
  /// )
  /// ```
1008
  /// {@end-tool}
1009 1010 1011 1012
  ///
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildBuilderDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
1013 1014 1015 1016
  /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. The
  /// `addSemanticIndexes` argument corresponds to the
  /// [SliverChildBuilderDelegate.addSemanticIndexes] property. None may be
  /// null.
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  ListView.separated({
    Key key,
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
    ScrollController controller,
    bool primary,
    ScrollPhysics physics,
    bool shrinkWrap = false,
    EdgeInsetsGeometry padding,
    @required IndexedWidgetBuilder itemBuilder,
    @required IndexedWidgetBuilder separatorBuilder,
    @required int itemCount,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
1031
    bool addSemanticIndexes = true,
1032 1033 1034 1035 1036
    double cacheExtent,
  }) : assert(itemBuilder != null),
       assert(separatorBuilder != null),
       assert(itemCount != null && itemCount >= 0),
       itemExtent = null,
1037
       childrenDelegate = SliverChildBuilderDelegate(
1038 1039
         (BuildContext context, int index) {
           final int itemIndex = index ~/ 2;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
           Widget widget;
           if (index.isEven) {
             widget = itemBuilder(context, itemIndex);
           } else {
             widget = separatorBuilder(context, itemIndex);
             assert(() {
               if (widget == null) {
                 throw FlutterError('separatorBuilder cannot return null.');
               }
               return true;
             }());
           }
           return widget;
1053
         },
1054
         childCount: _computeSemanticChildCount(itemCount),
1055 1056
         addAutomaticKeepAlives: addAutomaticKeepAlives,
         addRepaintBoundaries: addRepaintBoundaries,
1057 1058 1059
         addSemanticIndexes: addSemanticIndexes,
         semanticIndexCallback: (Widget _, int index) {
           return index.isEven ? index ~/ 2 : null;
1060
         },
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
       ),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
         cacheExtent: cacheExtent,
         semanticChildCount: _computeSemanticChildCount(itemCount),
       );
1074

1075 1076 1077 1078
  /// Creates a scrollable, linear array of widgets with a custom child model.
  ///
  /// For example, a custom child model can control the algorithm used to
  /// estimate the size of children that are not actually visible.
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  ///
  /// {@tool sample}
  ///
  /// This [ListView] uses a custom [SliverChildBuilderDelegate] to support child
  /// reordering.
  ///
  /// ```dart
  /// class MyListView extends StatefulWidget {
  ///   @override
  ///   _MyListViewState createState() => _MyListViewState();
  /// }
  ///
  /// class _MyListViewState extends State<MyListView> {
  ///   List<String> items = <String>['1', '2', '3', '4', '5'];
  ///
  ///   void _reverse() {
  ///     setState(() {
  ///       items = items.reversed.toList();
  ///     });
  ///   }
  ///
  ///   @override
  ///   Widget build(BuildContext context) {
  ///     return Scaffold(
  ///       body: SafeArea(
  ///         child: ListView.custom(
  ///           childrenDelegate: SliverChildBuilderDelegate(
  ///             (BuildContext context, int index) {
  ///               return KeepAlive(
  ///                 data: items[index],
  ///                 key: ValueKey<String>(items[index]),
  ///               );
  ///             },
  ///             childCount: items.length,
  ///             findChildIndexCallback: (Key key) {
  ///               final ValueKey valueKey = key;
  ///               final String data = valueKey.value;
  ///               return items.indexOf(data);
  ///             }
  ///           ),
  ///         ),
  ///       ),
  ///       bottomNavigationBar: BottomAppBar(
  ///         child: Row(
  ///           mainAxisAlignment: MainAxisAlignment.center,
  ///           children: <Widget>[
  ///             FlatButton(
  ///               onPressed: () => _reverse(),
  ///               child: Text('Reverse items'),
  ///             ),
  ///           ],
  ///         ),
  ///       ),
  ///     );
  ///   }
  /// }
  ///
  /// class KeepAlive extends StatefulWidget {
  ///   const KeepAlive({Key key, this.data}) : super(key: key);
  ///
  ///   final String data;
  ///
  ///   @override
  ///   _KeepAliveState createState() => _KeepAliveState();
  /// }
  ///
  /// class _KeepAliveState extends State<KeepAlive> with AutomaticKeepAliveClientMixin{
  ///   @override
  ///   bool get wantKeepAlive => true;
  ///
  ///   @override
  ///   Widget build(BuildContext context) {
  ///     super.build(context);
  ///     return Text(widget.data);
  ///   }
  /// }
  /// ```
  /// {@end-tool}
1157
  const ListView.custom({
1158
    Key key,
1159 1160
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1161
    ScrollController controller,
1162
    bool primary,
1163
    ScrollPhysics physics,
1164
    bool shrinkWrap = false,
1165
    EdgeInsetsGeometry padding,
1166 1167
    this.itemExtent,
    @required this.childrenDelegate,
1168
    double cacheExtent,
1169
    int semanticChildCount,
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  }) : assert(childrenDelegate != null),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
Ian Hickson's avatar
Ian Hickson committed
1180
         cacheExtent: cacheExtent,
1181
         semanticChildCount: semanticChildCount,
1182
       );
1183

1184 1185 1186 1187 1188 1189 1190
  /// If non-null, forces the children to have the given extent in the scroll
  /// direction.
  ///
  /// Specifying an [itemExtent] is more efficient than letting the children
  /// determine their own extent because the scrolling machinery can make use of
  /// the foreknowledge of the children's extent to save work, for example when
  /// the scroll position changes drastically.
1191 1192
  final double itemExtent;

1193 1194 1195 1196 1197 1198
  /// A delegate that provides the children for the [ListView].
  ///
  /// The [ListView.custom] constructor lets you specify this delegate
  /// explicitly. The [ListView] and [ListView.builder] constructors create a
  /// [childrenDelegate] that wraps the given [List] and [IndexedWidgetBuilder],
  /// respectively.
1199
  final SliverChildDelegate childrenDelegate;
1200 1201 1202 1203

  @override
  Widget buildChildLayout(BuildContext context) {
    if (itemExtent != null) {
1204
      return SliverFixedExtentList(
1205 1206 1207 1208
        delegate: childrenDelegate,
        itemExtent: itemExtent,
      );
    }
1209
    return SliverList(delegate: childrenDelegate);
1210 1211 1212
  }

  @override
1213 1214
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
1215
    properties.add(DoubleProperty('itemExtent', itemExtent, defaultValue: null));
1216
  }
1217 1218 1219 1220 1221

  // Helper method to compute the semantic child count for the separated constructor.
  static int _computeSemanticChildCount(int itemCount) {
    return math.max(0, itemCount * 2 - 1);
  }
1222 1223
}

1224 1225
/// A scrollable, 2D array of widgets.
///
1226 1227 1228
/// The main axis direction of a grid is the direction in which it scrolls (the
/// [scrollDirection]).
///
1229 1230 1231
/// The most commonly used grid layouts are [GridView.count], which creates a
/// layout with a fixed number of tiles in the cross axis, and
/// [GridView.extent], which creates a layout with tiles that have a maximum
1232
/// cross-axis extent. A custom [SliverGridDelegate] can produce an arbitrary 2D
1233 1234 1235 1236
/// arrangement of children, including arrangements that are unaligned or
/// overlapping.
///
/// To create a grid with a large (or infinite) number of children, use the
1237 1238 1239 1240 1241
/// [GridView.builder] constructor with either a
/// [SliverGridDelegateWithFixedCrossAxisCount] or a
/// [SliverGridDelegateWithMaxCrossAxisExtent] for the [gridDelegate].
///
/// To use a custom [SliverChildDelegate], use [GridView.custom].
1242 1243
///
/// To create a linear array of children, use a [ListView].
1244
///
1245 1246 1247
/// To control the initial scroll offset of the scroll view, provide a
/// [controller] with its [ScrollController.initialScrollOffset] property set.
///
1248 1249 1250
/// ## Transitioning to [CustomScrollView]
///
/// A [GridView] is basically a [CustomScrollView] with a single [SliverGrid] in
1251
/// its [CustomScrollView.slivers] property.
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
///
/// If [GridView] is no longer sufficient, for example because the scroll view
/// is to have both a grid and a list, or because the grid is to be combined
/// with a [SliverAppBar], etc, it is straight-forward to port code from using
/// [GridView] to using [CustomScrollView] directly.
///
/// The [key], [scrollDirection], [reverse], [controller], [primary], [physics],
/// and [shrinkWrap] properties on [GridView] map directly to the identically
/// named properties on [CustomScrollView].
///
/// The [CustomScrollView.slivers] property should be a list containing just a
/// [SliverGrid].
///
/// The [childrenDelegate] property on [GridView] corresponds to the
/// [SliverGrid.delegate] property, and the [gridDelegate] property on the
/// [GridView] corresponds to the [SliverGrid.gridDelegate] property.
///
/// The [new GridView], [new GridView.count], and [new GridView.extent]
/// constructors' `children` arguments correspond to the [childrenDelegate]
/// being a [SliverChildListDelegate] with that same argument. The [new
/// GridView.builder] constructor's `itemBuilder` and `childCount` arguments
/// correspond to the [childrenDelegate] being a [SliverChildBuilderDelegate]
/// with the matching arguments.
///
/// The [new GridView.count] and [new GridView.extent] constructors create
/// custom grid delegates, and have equivalently named constructors on
/// [SliverGrid] to ease the transition: [new SliverGrid.count] and [new
/// SliverGrid.extent] respectively.
///
/// The [padding] property corresponds to having a [SliverPadding] in the
/// [CustomScrollView.slivers] property instead of the grid itself, and having
/// the [SliverGrid] instead be a child of the [SliverPadding].
///
1285 1286 1287 1288
/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.
///
1289
/// Once code has been ported to use [CustomScrollView], other slivers, such as
1290
/// [SliverList] or [SliverAppBar], can be put in the [CustomScrollView.slivers]
1291 1292
/// list.
///
1293
/// {@tool sample}
1294 1295 1296
/// This example demonstrates how to create a [GridView] with two columns. The
/// children are spaced apart using the [crossAxisSpacing] and [mainAxisSpacing]
/// properties.
1297
///
1298
/// ![The GridView displays six children with different background colors arranged in two columns](https://flutter.github.io/assets-for-api-docs/assets/widgets/grid_view.png)
1299 1300
///
/// ```dart
1301
/// GridView.count(
1302
///   primary: false,
1303 1304 1305
///   padding: const EdgeInsets.all(20),
///   crossAxisSpacing: 10,
///   mainAxisSpacing: 10,
1306 1307
///   crossAxisCount: 2,
///   children: <Widget>[
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('He\'d have you all unravel at the'),
///       color: Colors.teal[100],
///     ),
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('Heed not the rabble'),
///       color: Colors.teal[200],
///     ),
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('Sound of screams but the'),
///       color: Colors.teal[300],
///     ),
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('Who scream'),
///       color: Colors.teal[400],
///     ),
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('Revolution is coming...'),
///       color: Colors.teal[500],
///     ),
///     Container(
///       padding: const EdgeInsets.all(8),
///       child: const Text('Revolution, they...'),
///       color: Colors.teal[600],
///     ),
1338 1339 1340
///   ],
/// )
/// ```
1341
/// {@end-tool}
1342
///
1343
/// {@tool sample}
1344 1345 1346
/// This example shows how to create the same grid as the previous example
/// using a [CustomScrollView] and a [SliverGrid].
///
1347
/// ![The CustomScrollView contains a SliverGrid that displays six children with different background colors arranged in two columns](https://flutter.github.io/assets-for-api-docs/assets/widgets/grid_view_custom_scroll.png)
1348 1349
///
/// ```dart
1350
/// CustomScrollView(
1351 1352
///   primary: false,
///   slivers: <Widget>[
1353
///     SliverPadding(
1354
///       padding: const EdgeInsets.all(20),
1355
///       sliver: SliverGrid.count(
1356 1357
///         crossAxisSpacing: 10,
///         mainAxisSpacing: 10,
1358 1359
///         crossAxisCount: 2,
///         children: <Widget>[
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('He\'d have you all unravel at the'),
///             color: Colors.green[100],
///           ),
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('Heed not the rabble'),
///             color: Colors.green[200],
///           ),
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('Sound of screams but the'),
///             color: Colors.green[300],
///           ),
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('Who scream'),
///             color: Colors.green[400],
///           ),
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('Revolution is coming...'),
///             color: Colors.green[500],
///           ),
///           Container(
///             padding: const EdgeInsets.all(8),
///             child: const Text('Revolution, they...'),
///             color: Colors.green[600],
///           ),
1390 1391 1392 1393 1394 1395
///         ],
///       ),
///     ),
///   ],
/// )
/// ```
1396
/// {@end-tool}
1397
///
1398 1399
/// See also:
///
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
///  * [SingleChildScrollView], which is a scrollable widget that has a single
///    child.
///  * [ListView], which is scrollable, linear list of widgets.
///  * [PageView], which is a scrolling list of child widgets that are each the
///    size of the viewport.
///  * [CustomScrollView], which is a scrollable widget that creates custom
///    scroll effects using slivers.
///  * [SliverGridDelegateWithFixedCrossAxisCount], which creates a layout with
///    a fixed number of tiles in the cross axis.
///  * [SliverGridDelegateWithMaxCrossAxisExtent], which creates a layout with
///    tiles that have a maximum cross-axis extent.
1411
///  * [ScrollNotification] and [NotificationListener], which can be used to watch
1412
///    the scroll position without using a [ScrollController].
1413
class GridView extends BoxScrollView {
1414 1415 1416 1417
  /// Creates a scrollable, 2D array of widgets with a custom
  /// [SliverGridDelegate].
  ///
  /// The [gridDelegate] argument must not be null.
1418
  ///
1419 1420 1421 1422
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildListDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
  /// [SliverChildListDelegate.addRepaintBoundaries] property. Both must not be
1423
  /// null.
1424
  GridView({
1425
    Key key,
1426 1427
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1428
    ScrollController controller,
1429
    bool primary,
1430
    ScrollPhysics physics,
1431
    bool shrinkWrap = false,
1432
    EdgeInsetsGeometry padding,
1433
    @required this.gridDelegate,
1434 1435
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
1436
    bool addSemanticIndexes = true,
1437
    double cacheExtent,
1438
    List<Widget> children = const <Widget>[],
1439
    int semanticChildCount,
1440
  }) : assert(gridDelegate != null),
1441
       childrenDelegate = SliverChildListDelegate(
1442
         children,
1443
         addAutomaticKeepAlives: addAutomaticKeepAlives,
1444
         addRepaintBoundaries: addRepaintBoundaries,
1445
         addSemanticIndexes: addSemanticIndexes,
1446
       ),
1447 1448 1449 1450 1451 1452 1453 1454 1455
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
1456 1457
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount ?? children.length,
1458
       );
1459

1460 1461 1462 1463 1464 1465
  /// Creates a scrollable, 2D array of widgets that are created on demand.
  ///
  /// This constructor is appropriate for grid views with a large (or infinite)
  /// number of children because the builder is called only for those children
  /// that are actually visible.
  ///
1466
  /// Providing a non-null `itemCount` improves the ability of the [GridView] to
1467 1468
  /// estimate the maximum scroll extent.
  ///
1469 1470
  /// `itemBuilder` will be called only with indices greater than or equal to
  /// zero and less than `itemCount`.
1471 1472
  ///
  /// The [gridDelegate] argument must not be null.
1473
  ///
1474 1475 1476 1477 1478
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildBuilderDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
  /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. Both must not
  /// be null.
1479 1480
  GridView.builder({
    Key key,
1481 1482
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1483 1484 1485
    ScrollController controller,
    bool primary,
    ScrollPhysics physics,
1486
    bool shrinkWrap = false,
1487
    EdgeInsetsGeometry padding,
1488 1489 1490
    @required this.gridDelegate,
    @required IndexedWidgetBuilder itemBuilder,
    int itemCount,
1491 1492
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
1493
    bool addSemanticIndexes = true,
1494
    double cacheExtent,
1495
    int semanticChildCount,
1496
  }) : assert(gridDelegate != null),
1497
       childrenDelegate = SliverChildBuilderDelegate(
1498 1499
         itemBuilder,
         childCount: itemCount,
1500
         addAutomaticKeepAlives: addAutomaticKeepAlives,
1501
         addRepaintBoundaries: addRepaintBoundaries,
1502
         addSemanticIndexes: addSemanticIndexes,
1503
       ),
1504 1505 1506 1507 1508 1509 1510 1511 1512
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
1513 1514
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount ?? itemCount,
1515
       );
1516

1517 1518 1519
  /// Creates a scrollable, 2D array of widgets with both a custom
  /// [SliverGridDelegate] and a custom [SliverChildDelegate].
  ///
1520 1521
  /// To use an [IndexedWidgetBuilder] callback to build children, either use
  /// a [SliverChildBuilderDelegate] or use the [GridView.builder] constructor.
1522 1523
  ///
  /// The [gridDelegate] and [childrenDelegate] arguments must not be null.
1524
  const GridView.custom({
1525
    Key key,
1526 1527
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1528
    ScrollController controller,
1529
    bool primary,
1530
    ScrollPhysics physics,
1531
    bool shrinkWrap = false,
1532
    EdgeInsetsGeometry padding,
1533 1534
    @required this.gridDelegate,
    @required this.childrenDelegate,
1535
    double cacheExtent,
1536
    int semanticChildCount,
1537
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  }) : assert(gridDelegate != null),
       assert(childrenDelegate != null),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
1549 1550
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount,
1551
         dragStartBehavior: dragStartBehavior,
1552
       );
1553

1554 1555 1556 1557
  /// Creates a scrollable, 2D array of widgets with a fixed number of tiles in
  /// the cross axis.
  ///
  /// Uses a [SliverGridDelegateWithFixedCrossAxisCount] as the [gridDelegate].
1558
  ///
1559 1560 1561 1562
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildListDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
  /// [SliverChildListDelegate.addRepaintBoundaries] property. Both must not be
1563 1564
  /// null.
  ///
1565 1566 1567
  /// See also:
  ///
  ///  * [new SliverGrid.count], the equivalent constructor for [SliverGrid].
1568 1569
  GridView.count({
    Key key,
1570 1571
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1572
    ScrollController controller,
1573
    bool primary,
1574
    ScrollPhysics physics,
1575
    bool shrinkWrap = false,
1576
    EdgeInsetsGeometry padding,
1577
    @required int crossAxisCount,
1578 1579 1580 1581 1582
    double mainAxisSpacing = 0.0,
    double crossAxisSpacing = 0.0,
    double childAspectRatio = 1.0,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
1583
    bool addSemanticIndexes = true,
1584
    double cacheExtent,
1585
    List<Widget> children = const <Widget>[],
1586
    int semanticChildCount,
1587
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
1588
  }) : gridDelegate = SliverGridDelegateWithFixedCrossAxisCount(
1589 1590 1591 1592 1593
         crossAxisCount: crossAxisCount,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
1594
       childrenDelegate = SliverChildListDelegate(
1595
         children,
1596
         addAutomaticKeepAlives: addAutomaticKeepAlives,
1597
         addRepaintBoundaries: addRepaintBoundaries,
1598
         addSemanticIndexes: addSemanticIndexes,
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
       ),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
         cacheExtent: cacheExtent,
         semanticChildCount: semanticChildCount ?? children.length,
         dragStartBehavior: dragStartBehavior,
       );
1613

1614 1615
  /// Creates a scrollable, 2D array of widgets with tiles that each have a
  /// maximum cross-axis extent.
1616 1617
  ///
  /// Uses a [SliverGridDelegateWithMaxCrossAxisExtent] as the [gridDelegate].
1618
  ///
1619 1620 1621 1622
  /// The `addAutomaticKeepAlives` argument corresponds to the
  /// [SliverChildListDelegate.addAutomaticKeepAlives] property. The
  /// `addRepaintBoundaries` argument corresponds to the
  /// [SliverChildListDelegate.addRepaintBoundaries] property. Both must not be
1623 1624
  /// null.
  ///
1625 1626 1627
  /// See also:
  ///
  ///  * [new SliverGrid.extent], the equivalent constructor for [SliverGrid].
1628
  GridView.extent({
1629
    Key key,
1630 1631
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
1632
    ScrollController controller,
1633
    bool primary,
1634
    ScrollPhysics physics,
1635
    bool shrinkWrap = false,
1636
    EdgeInsetsGeometry padding,
1637
    @required double maxCrossAxisExtent,
1638 1639 1640 1641 1642
    double mainAxisSpacing = 0.0,
    double crossAxisSpacing = 0.0,
    double childAspectRatio = 1.0,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
1643
    bool addSemanticIndexes = true,
1644
    List<Widget> children = const <Widget>[],
1645
    int semanticChildCount,
1646
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
1647
  }) : gridDelegate = SliverGridDelegateWithMaxCrossAxisExtent(
1648 1649 1650 1651 1652
         maxCrossAxisExtent: maxCrossAxisExtent,
         mainAxisSpacing: mainAxisSpacing,
         crossAxisSpacing: crossAxisSpacing,
         childAspectRatio: childAspectRatio,
       ),
1653
       childrenDelegate = SliverChildListDelegate(
1654
         children,
1655
         addAutomaticKeepAlives: addAutomaticKeepAlives,
1656
         addRepaintBoundaries: addRepaintBoundaries,
1657
         addSemanticIndexes: addSemanticIndexes,
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
       ),
       super(
         key: key,
         scrollDirection: scrollDirection,
         reverse: reverse,
         controller: controller,
         primary: primary,
         physics: physics,
         shrinkWrap: shrinkWrap,
         padding: padding,
         semanticChildCount: semanticChildCount ?? children.length,
         dragStartBehavior: dragStartBehavior,
       );
1671

1672 1673
  /// A delegate that controls the layout of the children within the [GridView].
  ///
1674
  /// The [GridView], [GridView.builder], and [GridView.custom] constructors let you specify this
1675 1676
  /// delegate explicitly. The other constructors create a [gridDelegate]
  /// implicitly.
1677 1678
  final SliverGridDelegate gridDelegate;

1679 1680 1681 1682 1683
  /// A delegate that provides the children for the [GridView].
  ///
  /// The [GridView.custom] constructor lets you specify this delegate
  /// explicitly. The other constructors create a [childrenDelegate] that wraps
  /// the given child list.
1684
  final SliverChildDelegate childrenDelegate;
1685

1686
  @override
1687
  Widget buildChildLayout(BuildContext context) {
1688
    return SliverGrid(
1689
      delegate: childrenDelegate,
1690 1691
      gridDelegate: gridDelegate,
    );
Adam Barth's avatar
Adam Barth committed
1692 1693
  }
}