Skip to content

Conversation

manu-sncf
Copy link
Contributor

@manu-sncf manu-sncf commented Aug 25, 2025

Fixes : #174368

Pre-launch Checklist

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

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. labels Aug 25, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where SliverMainAxisGroup did not correctly handle scrollOffsetCorrection from its children. The change adds logic to RenderSliverMainAxisGroup to check for and propagate this correction, which is the correct approach. A comprehensive regression test has been added to verify the fix in a scenario with a dynamically changing list. My review focuses on improving the new test's maintainability by addressing code duplication and inconsistency.

Comment on lines 1201 to 1431
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.
expect(find.text('item0'), findsOneWidget);
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);
},
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The MaterialApp widget is constructed twice in this test, and the two instances are inconsistent as the second one (line 1243) is missing the theme property. This could lead to subtle differences in widget sizes, making the test less reliable.1

To improve readability, reduce duplication, and ensure consistency, consider extracting the widget creation into a local helper function.

For example:

      Widget buildFrame() {
        return MaterialApp(
          theme: ThemeData(useMaterial3: false),
          home: Scaffold(
            body: CustomScrollView(
              slivers: <Widget>[
                SliverMainAxisGroup(
                  slivers: <Widget>[
                    SliverList(delegate: SliverChildBuilderDelegate(buildItem, childCount: 30)),
                  ],
                ),
              ],
            ),
          ),
        );
      }

      await tester.pumpWidget(buildFrame());
      // ...
      skip = false;
      await tester.pumpWidget(buildFrame());

Style Guide References

Footnotes

  1. Code should be readable and written correctly. Duplicated code with inconsistencies makes it harder to read, maintain, and can introduce bugs. (link)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a bad point. Are the two pumpWidgets sections of the test self contained too? Maybe you could split it into two separate tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both calls of tester.pumpWidget in same test are necessary to update the widgets tree with skip=true/false that trigger a SliverGeometry.scrollOffsetCorrection and thus reproduce the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinmc, , can I resolve this ? As mentioned before, both calls to tester.pumpWidget are necessary in this test. The second call uses the skip variable to modify the SliverList state, which is precisely what triggers the SliverGeometry.scrollOffsetCorrection we need to verify.

@justinmc justinmc requested a review from Piinks August 26, 2025 22:08
@justinmc
Copy link
Contributor

@Piinks Tagged you for review since this is about Slivers. I also plan to take a look.

Comment on lines 1229 to 1367
// Only even items 0~12 are on the screen.
expect(find.text('item0'), findsOneWidget);
expect(find.text('item12'), findsOneWidget);
expect(find.text('item14'), findsNothing);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could loop through 0-12 if you think it would be valuable to check everything.

Copy link
Contributor Author

@manu-sncf manu-sncf Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @justinmc, thanks for your review. FYI, this test is a duplicate of "slivers_test.dart" -> "SliverList can handle inaccurate scroll offset due to changes in children list".

I modified the test with a loop, as you suggested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok for you @justinmc ?

Comment on lines 1201 to 1431
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.
expect(find.text('item0'), findsOneWidget);
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);
},
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a bad point. Are the two pumpWidgets sections of the test self contained too? Maybe you could split it into two separate tests.

@@ -1197,6 +1197,103 @@ void main() {
expect(tester.getBottomRight(find.byKey(key)), offset - const Offset(0.0, 20.0));
},
);

testWidgets(
'With SliverList can handle inaccurate scroll offset due to changes in children list',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this test fails on master.

if (childLayoutGeometry case SliverGeometry(
:final double scrollOffsetCorrection,
) when scrollOffsetCorrection != 0) {
geometry = SliverGeometry(scrollOffsetCorrection: scrollOffsetCorrection);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to @Piinks on whether this is the right way to solve this.

Comment on lines 331 to 333
if (childLayoutGeometry case SliverGeometry(
:final double scrollOffsetCorrection,
) when scrollOffsetCorrection != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write this differently? We have not adopted this pattern in the framework because it can be hard for readability in some cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiiim may also be interested in this, they have made a lot of changes here recently.

Copy link
Member

@yiiim yiiim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for the Sliver changes. For tests and formatting issues, I will follow the guidance of @justinmc and @Piinks

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, thank you for the contribution!

@Piinks
Copy link
Contributor

Piinks commented Sep 5, 2025

There are some merge conflicts that need to be resolved here, and then this should be good to go!

@manu-sncf manu-sncf force-pushed the scroll_offset_correction branch from 0dec903 to 482c7ea Compare September 5, 2025 20:38
@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 5, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Sep 5, 2025
Merged via the queue into flutter:master with commit 242043f Sep 5, 2025
78 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SliverMainAxisGroup does not handle scrollOffsetCorrection
4 participants