Fix SliverGridRegularTileLayout.computeMaxScrollOffset for 0 children (#123348)

Fix SliverGridRegularTileLayout.computeMaxScrollOffset for 0 children
This commit is contained in:
Kate Lovett 2023-03-28 19:13:50 -05:00 committed by GitHub
parent bb02b52bd8
commit 659ba386f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View file

@ -225,6 +225,11 @@ class SliverGridRegularTileLayout extends SliverGridLayout {
@override
double computeMaxScrollOffset(int childCount) {
if (childCount == 0) {
// There are no children in the grid. The max scroll offset should be
// zero.
return 0.0;
}
final int mainAxisCount = ((childCount - 1) ~/ crossAxisCount) + 1;
final double mainAxisSpacing = mainAxisStride - childMainAxisExtent;
return mainAxisStride * mainAxisCount - mainAxisSpacing;

View file

@ -1294,6 +1294,55 @@ void main() {
expect(firstTapped, 0);
expect(secondTapped, 1);
});
testWidgets('SliverGridRegularTileLayout.computeMaxScrollOffset handles 0 children', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/59663
final ScrollController controller = ScrollController();
// SliverGridDelegateWithFixedCrossAxisCount
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: CustomScrollView(
controller: controller,
slivers: <Widget>[
SliverGrid.builder(
itemCount: 0,
itemBuilder: (_, __) => Container(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
mainAxisSpacing: 10,
childAspectRatio: 2.1,
),
),
],
),
),
));
// Verify correct scroll extent
expect(controller.position.maxScrollExtent, 0.0);
// SliverGridDelegateWithMaxCrossAxisExtent
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: CustomScrollView(
controller: controller,
slivers: <Widget>[
SliverGrid.builder(
itemCount: 0,
itemBuilder: (_, __) => Container(),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 30,
),
),
],
),
),
));
// Verify correct scroll extent
expect(controller.position.maxScrollExtent, 0.0);
});
}
bool isRight(Offset a, Offset b) => b.dx > a.dx;