Skip to content

Make EdgeDraggingAutoScroller respect ScrollPhysics#186541

Open
mbcorona wants to merge 3 commits into
flutter:masterfrom
mbcorona:fix-140654-edge-autoscroller-physics
Open

Make EdgeDraggingAutoScroller respect ScrollPhysics#186541
mbcorona wants to merge 3 commits into
flutter:masterfrom
mbcorona:fix-140654-edge-autoscroller-physics

Conversation

@mbcorona
Copy link
Copy Markdown
Member

Fixes #140654

Problem

When a Scrollable with NeverScrollableScrollPhysics lives under a SelectionArea, drag-selecting past the edge of the viewport still auto-scrolls it, and can additionally trip the "Drag target size is larger than scrollable size, which may cause bouncing" assertion in scrollable_helpers.dart. The original report uses a PageView(physics: NeverScrollableScrollPhysics()) containing a DataTable; reproduces on web, macOS, and Android, back to 3.16.

Root cause

EdgeDraggingAutoScroller._scroll() advances the scroll position by calling ScrollPosition.animateTo(...) directly. That bypasses ScrollPhysics.applyUserOffset / applyBoundaryConditions, so a Scrollable whose physics returns false from shouldAcceptUserOffset was still being driven during a drag-select. The two other user-driven scroll entry points already had this guard — ScrollAction.invoke in scrollable_helpers.dart and Scrollable._receivedPointerSignal in scrollable.dart — the auto-scroller was the missing one. The assertion lives inside _scroll(), so it fires only because _scroll() runs when it shouldn't; gating physics resolves both symptoms in one place.

(Root cause originally diagnosed by @Renzo-Olivares on the issue thread)

Change

EdgeDraggingAutoScroller.startAutoScrollIfNecessary now consults scrollable.resolvedPhysics.shouldAcceptUserOffset(scrollable.position). If physics refuses user offset, it stopAutoScroll()s and returns early — mirroring the pattern at the existing guard sites. No new public API.

The auto-scroller is shared infrastructure: SliverReorderableList uses the same startAutoScrollIfNecessary for drag-reorder auto-scroll. Under default physics the new guard is a pass-through, so reorder behavior is unchanged; the existing SliverReorderableList auto scrolls speed is configurable test serves as the regression guard and still passes.

Test

Added a testWidgets in scrollable_selection_test.dart that mouse-drag-selects past the bottom of a ListView wrapped in a SelectionArea with NeverScrollableScrollPhysics, then asserts the scroll offset stays at 0.0 and no exception is thrown. The new test fails on master with Expected: <0.0> Actual: <20.0> and passes with this change.

Manual verification

Tested with the original repro (a SelectionArea > PageView(NeverScrollableScrollPhysics) with a DataTable first page and a Text second page). Before: drag-selecting near the right edge of the DataTable slid the PageView to page 2 and threw the assertion. After: PageView stays on page 1 and selection continues normally. Video attached.

Screen.Recording.2026-05-14.at.11.07.19.a.m.mov

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

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.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 14, 2026
@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. labels May 14, 2026
@mbcorona mbcorona requested a review from Renzo-Olivares May 14, 2026 18:48
Copy link
Copy Markdown
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 modifies the EdgeDraggingAutoScroller to respect scroll physics that disallow user offsets, such as NeverScrollableScrollPhysics, by preventing or stopping auto-scroll. A regression test was added to ensure this behavior and prevent future regressions. Feedback was provided to simplify the implementation by removing a redundant null check on the non-nullable resolvedPhysics property, aligning with the repository's readability guidelines.

Comment thread packages/flutter/lib/src/widgets/scrollable_helpers.dart
Copy link
Copy Markdown
Contributor

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

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

Just some remarks on the test

Comment thread packages/flutter/test/widgets/scrollable_selection_test.dart Outdated
await tester.pumpWidget(
MaterialApp(
home: SelectionArea(
selectionControls: materialTextSelectionControls,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use testTextSelectionHandleControls, per the same remark about using Material here ?

);
await tester.pumpAndSettle();

final RenderParagraph paragraph0 = tester.renderObject<RenderParagraph>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason this requires finding the paragraph, as opposed to just using a text finder, and using for example the center of the text for the gesture start position?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense to me. We only use RenderParagraph if we need to precisely select and/or verify the TextSelection or other RenderParagraph members.

Co-authored-by: Navaron Bracke <brackenavaron@gmail.com>
@github-actions github-actions Bot removed the CICD Run CI/CD label May 14, 2026
addTearDown(controller.dispose);
await tester.pumpWidget(
TestWidgetsApp(
home: SelectionArea(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should use SelectableRegion here instead of SelectionArea to avoid using Material in the widgets library tests.


// Drag past the bottom of the scrollable; this would normally trigger
// edge auto-scroll.
await gesture.moveTo(tester.getBottomRight(find.byType(ListView)) + const Offset(0, 40));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: per the style guide use double literals for double constants https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md#use-double-literals-for-double-constants

Offset(0, 40) -> Offset(0.0, 40.0).

});

testWidgets(
'drag-select edge auto-scroll respects NeverScrollableScrollPhysics',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this can just be automatic edge scrolling respects NeverScrollableScrollPhysics.

Comment on lines +1452 to +1455
// A Scrollable whose physics refuses user offset (e.g.
// NeverScrollableScrollPhysics) must not be advanced by the selection
// edge auto-scroller, and the auto-scroller must not trip its drag-target
// size assertion in the process.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider the wording below to avoid specific implementation details that may not be true in the future.

// When a scrollable view with non-scrollable physics (e.g.,
// NeverScrollableScrollPhysics) is wrapped in a SelectableRegion, dragging
// a selection past the viewport boundary must not advance the scroll offset
// or throw exceptions.

@Renzo-Olivares
Copy link
Copy Markdown
Contributor

Hi @mbcorona, thank you for working on this! The code change LGTM just some small documentation comments.

await tester.pump();
await tester.pump(const Duration(seconds: 1));

// The scroll position must not have advanced, and no exception (e.g. the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think leaving as "no exception must have been thrown" is good enough. The specific assertion text may change so this specific comment may not be true later on.


await gesture.up();
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

consider checking controller.offset again after the pumpAndSettle.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 15, 2026
@mbcorona
Copy link
Copy Markdown
Member Author

@Renzo-Olivares @navaronbracke — pushed addressing all your comments. Ready for re-review when you have a minute.

@navaronbracke
Copy link
Copy Markdown
Contributor

Looks like Linux analyze is not happy with some of the formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD 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.

PageView with SelectionArea scrolls even with NeverScrollablePhysics and throws an Exception

3 participants