Fix SliverMainAxisGroup scrollOffsetCorrection (#174369)
Fixes : [#174368](https://github.com/flutter/flutter/issues/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
diff --git a/packages/flutter/lib/src/rendering/sliver_group.dart b/packages/flutter/lib/src/rendering/sliver_group.dart
index 75774aa..ae3b28c 100644
--- a/packages/flutter/lib/src/rendering/sliver_group.dart
+++ b/packages/flutter/lib/src/rendering/sliver_group.dart
@@ -326,7 +326,17 @@
),
parentUsesSize: true,
);
+
final SliverGeometry childLayoutGeometry = child.geometry!;
+
+ final double? scrollOffsetCorrection = childLayoutGeometry.scrollOffsetCorrection;
+ if (scrollOffsetCorrection != null) {
+ geometry = SliverGeometry(scrollOffsetCorrection: scrollOffsetCorrection);
+ return;
+ }
+
+ assert(childLayoutGeometry.debugAssertIsValid());
+
final double childPaintOffset = layoutOffset + childLayoutGeometry.paintOrigin;
final SliverPhysicalParentData childParentData =
child.parentData! as SliverPhysicalParentData;
diff --git a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
index 3367d0c..41c6cb0 100644
--- a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
+++ b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
@@ -1331,6 +1331,104 @@
expectedLocalPosition: const Offset(15, 5),
);
});
+ testWidgets(
+ 'With SliverList can handle inaccurate scroll offset due to changes in children list',
+ (WidgetTester tester) async {
+ bool skip = true;
+ Widget buildItem(BuildContext context, int index) {
+ return !skip || index.isEven
+ ? Card(
+ child: ListTile(title: Text('item$index', style: const TextStyle(fontSize: 80))),
+ )
+ : Container();
+ }
+
+ await tester.pumpWidget(
+ MaterialApp(
+ theme: ThemeData(useMaterial3: false),
+ home: Scaffold(
+ body: CustomScrollView(
+ slivers: <Widget>[
+ SliverMainAxisGroup(
+ slivers: <Widget>[
+ SliverList(delegate: SliverChildBuilderDelegate(buildItem, childCount: 30)),
+ ],
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ // Only even items 0~12 are on the screen.
+ for (int index = 0; index <= 12; index++) {
+ expect(find.text('item$index'), index.isEven ? findsOneWidget : findsNothing);
+ }
+ expect(find.text('item12'), findsOneWidget);
+ expect(find.text('item14'), findsNothing);
+
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, -750.0));
+ await tester.pump();
+ // Only even items 16~28 are on the screen.
+ expect(find.text('item15'), findsNothing);
+ expect(find.text('item16'), findsOneWidget);
+ expect(find.text('item28'), findsOneWidget);
+
+ skip = false;
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: CustomScrollView(
+ slivers: <Widget>[
+ SliverMainAxisGroup(
+ slivers: <Widget>[
+ SliverList(delegate: SliverChildBuilderDelegate(buildItem, childCount: 30)),
+ ],
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+
+ // Only items 12~19 are on the screen.
+ expect(find.text('item11'), findsNothing);
+ expect(find.text('item12'), findsOneWidget);
+ expect(find.text('item19'), findsOneWidget);
+ expect(find.text('item20'), findsNothing);
+
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
+ await tester.pump();
+
+ // Only items 10~16 are on the screen.
+ expect(find.text('item9'), findsNothing);
+ expect(find.text('item10'), findsOneWidget);
+ expect(find.text('item16'), findsOneWidget);
+ expect(find.text('item17'), findsNothing);
+
+ // The inaccurate scroll offset should reach zero at this point
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
+ await tester.pump();
+
+ // Only items 7~13 are on the screen.
+ expect(find.text('item6'), findsNothing);
+ expect(find.text('item7'), findsOneWidget);
+ expect(find.text('item13'), findsOneWidget);
+ expect(find.text('item14'), findsNothing);
+
+ // It will be corrected as we scroll, so we have to drag multiple times.
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
+ await tester.pump();
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
+ await tester.pump();
+ await tester.drag(find.byType(CustomScrollView), const Offset(0.0, 250.0));
+ await tester.pump();
+
+ // Only items 0~6 are on the screen.
+ expect(find.text('item0'), findsOneWidget);
+ expect(find.text('item6'), findsOneWidget);
+ expect(find.text('item7'), findsNothing);
+ },
+ );
}
Widget _buildSliverList({