Skip to content

Commit 659ba38

Browse files
authored
Fix SliverGridRegularTileLayout.computeMaxScrollOffset for 0 children (flutter#123348)
Fix SliverGridRegularTileLayout.computeMaxScrollOffset for 0 children
1 parent bb02b52 commit 659ba38

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/flutter/lib/src/rendering/sliver_grid.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ class SliverGridRegularTileLayout extends SliverGridLayout {
225225

226226
@override
227227
double computeMaxScrollOffset(int childCount) {
228+
if (childCount == 0) {
229+
// There are no children in the grid. The max scroll offset should be
230+
// zero.
231+
return 0.0;
232+
}
228233
final int mainAxisCount = ((childCount - 1) ~/ crossAxisCount) + 1;
229234
final double mainAxisSpacing = mainAxisStride - childMainAxisExtent;
230235
return mainAxisStride * mainAxisCount - mainAxisSpacing;

packages/flutter/test/widgets/slivers_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,55 @@ void main() {
12941294
expect(firstTapped, 0);
12951295
expect(secondTapped, 1);
12961296
});
1297+
1298+
testWidgets('SliverGridRegularTileLayout.computeMaxScrollOffset handles 0 children', (WidgetTester tester) async {
1299+
// Regression test for https://github.com/flutter/flutter/issues/59663
1300+
final ScrollController controller = ScrollController();
1301+
1302+
// SliverGridDelegateWithFixedCrossAxisCount
1303+
await tester.pumpWidget(MaterialApp(
1304+
home: Scaffold(
1305+
body: CustomScrollView(
1306+
controller: controller,
1307+
slivers: <Widget>[
1308+
SliverGrid.builder(
1309+
itemCount: 0,
1310+
itemBuilder: (_, __) => Container(),
1311+
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
1312+
crossAxisCount: 1,
1313+
mainAxisSpacing: 10,
1314+
childAspectRatio: 2.1,
1315+
),
1316+
),
1317+
],
1318+
),
1319+
),
1320+
));
1321+
1322+
// Verify correct scroll extent
1323+
expect(controller.position.maxScrollExtent, 0.0);
1324+
1325+
// SliverGridDelegateWithMaxCrossAxisExtent
1326+
await tester.pumpWidget(MaterialApp(
1327+
home: Scaffold(
1328+
body: CustomScrollView(
1329+
controller: controller,
1330+
slivers: <Widget>[
1331+
SliverGrid.builder(
1332+
itemCount: 0,
1333+
itemBuilder: (_, __) => Container(),
1334+
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
1335+
maxCrossAxisExtent: 30,
1336+
),
1337+
),
1338+
],
1339+
),
1340+
),
1341+
));
1342+
1343+
// Verify correct scroll extent
1344+
expect(controller.position.maxScrollExtent, 0.0);
1345+
});
12971346
}
12981347

12991348
bool isRight(Offset a, Offset b) => b.dx > a.dx;

0 commit comments

Comments
 (0)