Add SliverGrid.builder constructor (#113116)

This commit is contained in:
Greg Spencer 2022-10-07 12:49:12 -07:00 committed by GitHub
parent 3eef3ff331
commit 41a13a3a90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 3 deletions

View file

@ -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,

View file

@ -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.
///

View file

@ -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: <Widget>[
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;