From a57018a4bb98c7e161974bc76f1ebf8d2bdfb859 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 21 Apr 2017 14:09:53 -0700 Subject: [PATCH] Add GridView.builder (#9522) This constructor makes it easier to use a GridView with a builder callback. Fixes #8952 --- .../flutter/lib/src/widgets/page_view.dart | 2 +- .../flutter/lib/src/widgets/scroll_view.dart | 51 +++++++++++++++++-- .../flutter/test/widgets/grid_view_test.dart | 18 +++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/widgets/page_view.dart b/packages/flutter/lib/src/widgets/page_view.dart index ec6328a664..5065aa1f05 100644 --- a/packages/flutter/lib/src/widgets/page_view.dart +++ b/packages/flutter/lib/src/widgets/page_view.dart @@ -313,7 +313,7 @@ class PageView extends StatefulWidget { PageController controller, this.physics, this.onPageChanged, - IndexedWidgetBuilder itemBuilder, + @required IndexedWidgetBuilder itemBuilder, int itemCount, }) : controller = controller ?? _defaultPageController, childrenDelegate = new SliverChildBuilderDelegate(itemBuilder, childCount: itemCount), diff --git a/packages/flutter/lib/src/widgets/scroll_view.dart b/packages/flutter/lib/src/widgets/scroll_view.dart index 43f6c1be20..3eeac9b85a 100644 --- a/packages/flutter/lib/src/widgets/scroll_view.dart +++ b/packages/flutter/lib/src/widgets/scroll_view.dart @@ -386,7 +386,7 @@ class ListView extends BoxScrollView { bool shrinkWrap: false, EdgeInsets padding, this.itemExtent, - IndexedWidgetBuilder itemBuilder, + @required IndexedWidgetBuilder itemBuilder, int itemCount, }) : childrenDelegate = new SliverChildBuilderDelegate(itemBuilder, childCount: itemCount), super( key: key, @@ -473,8 +473,11 @@ class ListView extends BoxScrollView { /// overlapping. /// /// To create a grid with a large (or infinite) number of children, use the -/// [GridView.custom] constructor with either a [SliverChildBuilderDelegate] or -/// a custom [SliverChildDelegate]. +/// [GridView.builder] constructor with either a +/// [SliverGridDelegateWithFixedCrossAxisCount] or a +/// [SliverGridDelegateWithMaxCrossAxisExtent] for the [gridDelegate]. +/// +/// To use a custom [SliverChildDelegate], use [GridView.custom]. /// /// To create a linear array of children, use a [ListView]. /// @@ -520,11 +523,49 @@ class GridView extends BoxScrollView { assert(gridDelegate != null); } + /// 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. + /// + /// Providing a non-null [itemCount] improves the ability of the [GridView] to + /// estimate the maximum scroll extent. + /// + /// [itemBuilder] will be called only with indices greater than or equal to + /// zero and less than [itemCount]. + /// + /// The [gridDelegate] argument must not be null. + GridView.builder({ + Key key, + Axis scrollDirection: Axis.vertical, + bool reverse: false, + ScrollController controller, + bool primary, + ScrollPhysics physics, + bool shrinkWrap: false, + EdgeInsets padding, + @required this.gridDelegate, + @required IndexedWidgetBuilder itemBuilder, + int itemCount, + }) : childrenDelegate = new SliverChildBuilderDelegate(itemBuilder, childCount: itemCount), super( + key: key, + scrollDirection: scrollDirection, + reverse: reverse, + controller: controller, + primary: primary, + physics: physics, + shrinkWrap: shrinkWrap, + padding: padding, + ) { + assert(gridDelegate != null); + } + /// Creates a scrollable, 2D array of widgets with both a custom /// [SliverGridDelegate] and a custom [SliverChildDelegate]. /// - /// To use an [IndexedWidgetBuilder] callback to build children, use the - /// [SliverChildBuilderDelegate]. + /// To use an [IndexedWidgetBuilder] callback to build children, either use + /// a [SliverChildBuilderDelegate] or use the [GridView.builder] constructor. /// /// The [gridDelegate] and [childrenDelegate] arguments must not be null. GridView.custom({ diff --git a/packages/flutter/test/widgets/grid_view_test.dart b/packages/flutter/test/widgets/grid_view_test.dart index 8a81c2a4a0..16a756c497 100644 --- a/packages/flutter/test/widgets/grid_view_test.dart +++ b/packages/flutter/test/widgets/grid_view_test.dart @@ -369,6 +369,24 @@ void main() { expect(find.text('19'), findsOneWidget); }); + testWidgets('GridView.builder control test', (WidgetTester tester) async { + await tester.pumpWidget(new GridView.builder( + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 4, + ), + shrinkWrap: true, + itemCount: 20, + itemBuilder: (BuildContext context, int index) { + return new Container( + child: new Text('$index'), + ); + }, + )); + expect(find.text('0'), findsOneWidget); + expect(find.text('11'), findsOneWidget); + expect(find.text('12'), findsNothing); + }); + // TODO(ianh): can you tap a grid cell that is slightly off the bottom of the screen? // (try it with the flutter_gallery Grid demo) }