Skip to content

Commit 242043f

Browse files
authored
Fix SliverMainAxisGroup scrollOffsetCorrection (flutter#174369)
Fixes : [flutter#174368](flutter#174368) ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent e7fab7e commit 242043f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,17 @@ class RenderSliverMainAxisGroup extends RenderSliver
326326
),
327327
parentUsesSize: true,
328328
);
329+
329330
final SliverGeometry childLayoutGeometry = child.geometry!;
331+
332+
final double? scrollOffsetCorrection = childLayoutGeometry.scrollOffsetCorrection;
333+
if (scrollOffsetCorrection != null) {
334+
geometry = SliverGeometry(scrollOffsetCorrection: scrollOffsetCorrection);
335+
return;
336+
}
337+
338+
assert(childLayoutGeometry.debugAssertIsValid());
339+
330340
final double childPaintOffset = layoutOffset + childLayoutGeometry.paintOrigin;
331341
final SliverPhysicalParentData childParentData =
332342
child.parentData! as SliverPhysicalParentData;

packages/flutter/test/widgets/sliver_main_axis_group_test.dart

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,104 @@ void main() {
13311331
expectedLocalPosition: const Offset(15, 5),
13321332
);
13331333
});
1334+
testWidgets(
1335+
'With SliverList can handle inaccurate scroll offset due to changes in children list',
1336+
(WidgetTester tester) async {
1337+
bool skip = true;
1338+
Widget buildItem(BuildContext context, int index) {
1339+
return !skip || index.isEven
1340+
? Card(
1341+
child: ListTile(title: Text('item$index', style: const TextStyle(fontSize: 80))),
1342+
)
1343+
: Container();
1344+
}
1345+
1346+
await tester.pumpWidget(
1347+
MaterialApp(
1348+
theme: ThemeData(useMaterial3: false),
1349+
home: Scaffold(
1350+
body: CustomScrollView(
1351+
slivers: <Widget>[
1352+
SliverMainAxisGroup(
1353+
slivers: <Widget>[
1354+
SliverList(delegate: SliverChildBuilderDelegate(buildItem, childCount: 30)),
1355+
],
1356+
),
1357+
],
1358+
),
1359+
),
1360+
),
1361+
);
1362+
// Only even items 0~12 are on the screen.
1363+
for (int index = 0; index <= 12; index++) {
1364+
expect(find.text('item$index'), index.isEven ? findsOneWidget : findsNothing);
1365+
}
1366+
expect(find.text('item12'), findsOneWidget);
1367+
expect(find.text('item14'), findsNothing);
1368+
1369+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, -750.0));
1370+
await tester.pump();
1371+
// Only even items 16~28 are on the screen.
1372+
expect(find.text('item15'), findsNothing);
1373+
expect(find.text('item16'), findsOneWidget);
1374+
expect(find.text('item28'), findsOneWidget);
1375+
1376+
skip = false;
1377+
await tester.pumpWidget(
1378+
MaterialApp(
1379+
home: Scaffold(
1380+
body: CustomScrollView(
1381+
slivers: <Widget>[
1382+
SliverMainAxisGroup(
1383+
slivers: <Widget>[
1384+
SliverList(delegate: SliverChildBuilderDelegate(buildItem, childCount: 30)),
1385+
],
1386+
),
1387+
],
1388+
),
1389+
),
1390+
),
1391+
);
1392+
1393+
// Only items 12~19 are on the screen.
1394+
expect(find.text('item11'), findsNothing);
1395+
expect(find.text('item12'), findsOneWidget);
1396+
expect(find.text('item19'), findsOneWidget);
1397+
expect(find.text('item20'), findsNothing);
1398+
1399+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
1400+
await tester.pump();
1401+
1402+
// Only items 10~16 are on the screen.
1403+
expect(find.text('item9'), findsNothing);
1404+
expect(find.text('item10'), findsOneWidget);
1405+
expect(find.text('item16'), findsOneWidget);
1406+
expect(find.text('item17'), findsNothing);
1407+
1408+
// The inaccurate scroll offset should reach zero at this point
1409+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
1410+
await tester.pump();
1411+
1412+
// Only items 7~13 are on the screen.
1413+
expect(find.text('item6'), findsNothing);
1414+
expect(find.text('item7'), findsOneWidget);
1415+
expect(find.text('item13'), findsOneWidget);
1416+
expect(find.text('item14'), findsNothing);
1417+
1418+
// It will be corrected as we scroll, so we have to drag multiple times.
1419+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
1420+
await tester.pump();
1421+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
1422+
await tester.pump();
1423+
await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
1424+
await tester.pump();
1425+
1426+
// Only items 0~6 are on the screen.
1427+
expect(find.text('item0'), findsOneWidget);
1428+
expect(find.text('item6'), findsOneWidget);
1429+
expect(find.text('item7'), findsNothing);
1430+
},
1431+
);
13341432
}
13351433

13361434
Widget _buildSliverList({

0 commit comments

Comments
 (0)