Visitar URL original
Fix TabBarView/PageView edge auto-scroll during mouse text selection by mbcorona · Pull Request #189544 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename to allowSelectionEdgeScrolling and assert selection in test
  • Loading branch information
mbcorona committed Aug 9, 2026
commit 88c7799764cd149d0bb06a5f66d3f6cecebcaade
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/widgets/page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class PageScrollPhysics extends ScrollPhysics {
}

@override

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: consider adding a comment explaining why this is set to false for pageview physics. Something simple like Defaults to false to prevent selection gestures such as dragging to select text from scrolling to an adjacent page.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

bool get snapsToPages => true;
bool get allowSelectionEdgeScrolling => false;

double _getPage(ScrollMetrics position) {
if (position is _PagePosition) {
Expand Down
11 changes: 6 additions & 5 deletions packages/flutter/lib/src/widgets/scroll_physics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,13 @@ class ScrollPhysics {
/// Whether a viewport is allowed to change the scroll position as the result of user input.
bool get allowUserScrolling => true;

/// Whether this physics snaps to page boundaries, as [PageScrollPhysics] does.
/// Whether a viewport may scroll to its edge as a side effect of extending a

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:

Whether a viewport may scroll to reveal more content when a selection gesture extends past the visible area, i.e. the viewport's edge.

/// selection past the visible area.
///
/// Selection edge auto-scrolling checks this to avoid scrolling a paged
/// scrollable (such as a [PageView] or [TabBarView]) while extending a
/// selection, which would flip pages instead of revealing more content.
bool get snapsToPages => parent?.snapsToPages ?? false;
/// Defaults to true. Paged scrollables such as [PageView] override this to

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: consider the following edit.

Paged scrollables that want to prevent selection
gestures from revealing an adjacent page should
override this to false in their [ScrollPhysics]
similar to [PageScrollPhysics] used by [PageView].

Defaults to true.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

applied the suggested edit

/// false, since edge scrolling there flips to an adjacent page instead of
/// revealing more content.
bool get allowSelectionEdgeScrolling => parent?.allowSelectionEdgeScrolling ?? true;

@override
String toString() {
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter/lib/src/widgets/scrollable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1299,9 +1299,9 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
return result;
}
if (_selectionStartsInScrollable) {
// A paged scrollable (PageView or TabBarView) would flip pages instead of
// revealing more content, so never edge scroll it while selecting.
if (state.resolvedPhysics?.snapsToPages ?? false) {
// Paged scrollables (like PageView) disallow this; edge scrolling there
// flips to an adjacent page instead of revealing more content.
if (!(state.resolvedPhysics?.allowSelectionEdgeScrolling ?? true)) {
_autoScroller.stopAutoScroll();
return result;
}
Expand Down
13 changes: 8 additions & 5 deletions packages/flutter/test/widgets/scroll_physics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class TestScrollPhysics extends ScrollPhysics {
void main() {
const kBlueColor = Color(0xFF0000FF);

test('ScrollPhysics.snapsToPages', () {
expect(const BouncingScrollPhysics().snapsToPages, isFalse);
expect(const PageScrollPhysics().snapsToPages, isTrue);
// Propagates through the parent chain so wrapped page physics still count.
expect(const ClampingScrollPhysics(parent: PageScrollPhysics()).snapsToPages, isTrue);
test('ScrollPhysics.allowSelectionEdgeScrolling', () {
expect(const BouncingScrollPhysics().allowSelectionEdgeScrolling, isTrue);
expect(const PageScrollPhysics().allowSelectionEdgeScrolling, isFalse);
// Resolves through the parent chain so wrapped page physics still count.
expect(
const ClampingScrollPhysics(parent: PageScrollPhysics()).allowSelectionEdgeScrolling,
isFalse,
);
});

test('ScrollPhysics applyTo()', () {
Expand Down
94 changes: 48 additions & 46 deletions packages/flutter/test/widgets/scrollable_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1499,62 +1499,64 @@ void main() {
expect(tester.takeException(), isNull);
});

testWidgets('mouse selection drag does not edge auto-scroll a page-snapping scrollable', (
WidgetTester tester,
) async {
// Regression test for https://github.com/flutter/flutter/issues/149426.
// A paged scrollable snaps to pages, so dragging a selection past its edge
// must not flip to the next page and snap back.
final controller = PageController();
addTearDown(controller.dispose);
await tester.pumpWidget(
TestWidgetsApp(
home: SelectableRegion(
selectionControls: testTextSelectionHandleControls,
child: PageView.builder(
controller: controller,
itemCount: 5,
itemBuilder: (BuildContext context, int index) {
return Center(child: Text('Page $index'));
},
testWidgets(

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 add an additional test that verifies a ListView inside a PageView can still be scrolled (while not flipping to an adjancent page), in both vertical and horizontal axes.

'paged scrollable does not flip pages during selection, but text still gets selected',
(WidgetTester tester) async {
// Dragging a selection past a PageView's edge must not flip to the next

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 we can remove this comment, it simply restates the test title.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed

// page and snap back. The selection itself must still work.
final controller = PageController();
addTearDown(controller.dispose);
await tester.pumpWidget(
TestWidgetsApp(
home: SelectableRegion(
selectionControls: testTextSelectionHandleControls,
child: PageView.builder(
controller: controller,
itemCount: 5,
itemBuilder: (BuildContext context, int index) {
return Center(child: Text('Page $index'));
},
),
),
),
),
);
await tester.pumpAndSettle();
expect(controller.page, 0.0);
);
await tester.pumpAndSettle();
expect(controller.page, 0.0);

final TestGesture gesture = await tester.startGesture(
tester.getCenter(find.text('Page 0')),
kind: ui.PointerDeviceKind.mouse,
);
addTearDown(gesture.removePointer);
await tester.pump();
expect(controller.page, 0.0);
final RenderParagraph page0 = tester.renderObject<RenderParagraph>(
find.descendant(of: find.text('Page 0'), matching: find.byType(RichText)),
);
final TestGesture gesture = await tester.startGesture(
tester.getCenter(find.text('Page 0')),
kind: ui.PointerDeviceKind.mouse,
);
addTearDown(gesture.removePointer);
await tester.pump();
expect(controller.page, 0.0);

// Drag past the right edge. Without the fix this scrolls toward page 1.
await gesture.moveTo(tester.getBottomRight(find.byType(PageView)) + const Offset(40.0, 0.0));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(controller.page, 0.0);
expect(tester.takeException(), isNull);
// Hold the drag past the right edge; without the fix this keeps flipping
// pages toward page 1.
await gesture.moveTo(tester.getBottomRight(find.byType(PageView)) + const Offset(40.0, 0.0));
await tester.pump();
await tester.pump(const Duration(seconds: 1));

await tester.pump(const Duration(seconds: 1));
expect(controller.page, 0.0);
expect(tester.takeException(), isNull);
// The page did not flip, but the page's text was still selected.
expect(controller.page, 0.0);
expect(page0.selections, isNotEmpty);
expect(tester.takeException(), isNull);

await gesture.up();
await tester.pumpAndSettle();
expect(controller.page, 0.0);
expect(tester.takeException(), isNull);
});
await gesture.up();
await tester.pumpAndSettle();
expect(controller.page, 0.0);
expect(tester.takeException(), isNull);
},
);

testWidgets('mouse selection drag still edge auto-scrolls a non page-snapping scrollable', (
WidgetTester tester,
) async {
// Counterpart to the PageView test above: the guard only touches paged
// scrollables. A plain ListView still edge scrolls while drag-selecting
// ("select to scroll"). See https://github.com/flutter/flutter/issues/149426.
// Counterpart to the PageView test above: only paged scrollables are
// affected. A plain ListView still edge scrolls while drag-selecting.
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Expand Down