From 41a13a3a9059a0f22c670e0194584d4393f44cf9 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Fri, 7 Oct 2022 12:49:12 -0700 Subject: [PATCH] Add `SliverGrid.builder` constructor (#113116) --- .../flutter/lib/src/widgets/scroll_view.dart | 7 +-- packages/flutter/lib/src/widgets/sliver.dart | 43 +++++++++++++++++++ .../flutter/test/widgets/slivers_test.dart | 39 +++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/scroll_view.dart b/packages/flutter/lib/src/widgets/scroll_view.dart index f02905c006..0a7f4c9a44 100644 --- a/packages/flutter/lib/src/widgets/scroll_view.dart +++ b/packages/flutter/lib/src/widgets/scroll_view.dart @@ -1757,13 +1757,14 @@ class GridView extends BoxScrollView { /// /// {@macro flutter.widgets.PageView.findChildIndexCallback} /// - /// The [gridDelegate] argument must not be null. + /// The [gridDelegate] argument is required. /// /// The `addAutomaticKeepAlives` argument corresponds to the /// [SliverChildBuilderDelegate.addAutomaticKeepAlives] property. The /// `addRepaintBoundaries` argument corresponds to the - /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. Both must not - /// be null. + /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. The + /// `addSemanticIndexes` argument corresponds to the + /// [SliverChildBuilderDelegate.addSemanticIndexes] property. GridView.builder({ super.key, super.scrollDirection, diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index 077f5d963b..b3b154c0d6 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -1166,6 +1166,49 @@ class SliverGrid extends SliverMultiBoxAdaptorWidget { required this.gridDelegate, }); + /// A sliver that creates a 2D array of widgets that are created on demand. + /// + /// This constructor is appropriate for sliver grids 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 [SliverGrid] + /// to estimate the maximum scroll extent. + /// + /// `itemBuilder` will be called only with indices greater than or equal to + /// zero and less than `itemCount`. + /// + /// {@macro flutter.widgets.ListView.builder.itemBuilder} + /// + /// {@macro flutter.widgets.PageView.findChildIndexCallback} + /// + /// The [gridDelegate] argument is required. + /// + /// The `addAutomaticKeepAlives` argument corresponds to the + /// [SliverChildBuilderDelegate.addAutomaticKeepAlives] property. The + /// `addRepaintBoundaries` argument corresponds to the + /// [SliverChildBuilderDelegate.addRepaintBoundaries] property. The + /// `addSemanticIndexes` argument corresponds to the + /// [SliverChildBuilderDelegate.addSemanticIndexes] property. + SliverGrid.builder({ + super.key, + required this.gridDelegate, + required NullableIndexedWidgetBuilder itemBuilder, + ChildIndexGetter? findChildIndexCallback, + int? itemCount, + bool addAutomaticKeepAlives = true, + bool addRepaintBoundaries = true, + bool addSemanticIndexes = true, + }) : assert(gridDelegate != null), + super(delegate: SliverChildBuilderDelegate( + itemBuilder, + findChildIndexCallback: findChildIndexCallback, + childCount: itemCount, + addAutomaticKeepAlives: addAutomaticKeepAlives, + addRepaintBoundaries: addRepaintBoundaries, + addSemanticIndexes: addSemanticIndexes, + )); + /// Creates a sliver that places multiple box children in a two dimensional /// arrangement with a fixed number of tiles in the cross axis. /// diff --git a/packages/flutter/test/widgets/slivers_test.dart b/packages/flutter/test/widgets/slivers_test.dart index fee8c0fc98..0067cc409a 100644 --- a/packages/flutter/test/widgets/slivers_test.dart +++ b/packages/flutter/test/widgets/slivers_test.dart @@ -991,6 +991,45 @@ void main() { expect(firstTapped, 1); expect(secondTapped, 1); }); + + testWidgets('SliverGrid.builder can build children', (WidgetTester tester) async { + int firstTapped = 0; + int secondTapped = 0; + final Key key = UniqueKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: key, + body: CustomScrollView( + slivers: [ + SliverGrid.builder( + itemCount: 2, + itemBuilder: (BuildContext context, int index) { + return Material( + color: index.isEven ? Colors.yellow : Colors.red, + child: InkWell( + onTap: () { + index.isEven ? firstTapped++ : secondTapped++; + }, + child: Text('Index $index'), + ), + ); + }, + gridDelegate: _TestArbitrarySliverGridDelegate(), + ), + ], + ), + ), + )); + + // Verify correct hit testing + await tester.tap(find.text('Index 0')); + expect(firstTapped, 1); + expect(secondTapped, 0); + firstTapped = 0; + await tester.tap(find.text('Index 1')); + expect(firstTapped, 0); + expect(secondTapped, 1); + }); } bool isRight(Offset a, Offset b) => b.dx > a.dx;