Skip to content

Commit c4b9709

Browse files
authored
Fix TabBarView/PageView edge auto-scroll during mouse text selection (flutter#189544)
Fixes flutter#149426 **Problem:** In a `PageView`/`TabBarView` wrapped by a `SelectionArea`, drag-selecting text with a mouse on desktop/web flips to the next page and bounces back. **Cause:** A selection drag reuses the scrollable's edge auto-scroll. That is wanted for most scrollables (a `ListView`'s drag-past-edge "select to scroll"), but a page-snapping scrollable flips to an adjacent page instead of revealing more content. The existing physics guard doesn't help, since `PageScrollPhysics` accepts a user offset. **Fix:** Add `ScrollPhysics.allowSelectionEdgeScrolling`, in the same vein as `allowUserScrolling`/`allowImplicitScrolling`. It defaults to `true`; `PageScrollPhysics` overrides it to `false`. The `Scrollable` selection handler skips edge auto-scroll when the resolved physics returns `false`. It delegates through `parent`, so it resolves even when the page physics are wrapped. No import cycle, and no `deviceKind` on `SelectionEdgeUpdateEvent`. `CarouselScrollPhysics` should override this too; tracked separately in flutter#190737 since it lives in Material. **Tests:** - A `PageView` does not flip while a selection drag reaches its edge, and the selection still extends into the widget beside it. - A `ListView` still edge scrolls ("select to scroll") during a selection drag. - A `ListView` nested in a `PageView` still edge scrolls without flipping the page, in both axes. - `ScrollPhysics.allowSelectionEdgeScrolling` default, override, and parent delegation. Before: https://github.com/user-attachments/assets/cc5893df-07c7-49bf-86c0-09448de8a221 After: https://github.com/user-attachments/assets/c3e8140d-c308-460a-9275-e76961cd6184 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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]. 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](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 [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 6d0eaf2 commit c4b9709

5 files changed

Lines changed: 202 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/page_view.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ class PageScrollPhysics extends ScrollPhysics {
581581
return PageScrollPhysics(parent: buildParent(ancestor));
582582
}
583583

584+
// Selection gestures such as dragging to select text must not scroll a
585+
// PageView to an adjacent page, so edge scrolling is disallowed here.
586+
@override
587+
bool get allowSelectionEdgeScrolling => false;
588+
584589
double _getPage(ScrollMetrics position) {
585590
if (position is _PagePosition) {
586591
return position.page!;

‎packages/flutter/lib/src/widgets/scroll_physics.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ class ScrollPhysics {
499499
/// Whether a viewport is allowed to change the scroll position as the result of user input.
500500
bool get allowUserScrolling => true;
501501

502+
/// Whether a viewport may scroll to reveal more content when a selection
503+
/// gesture extends past the visible area, i.e. the viewport's edge.
504+
///
505+
/// Paged scrollables that want to prevent selection gestures from revealing an
506+
/// adjacent page should override this to false in their [ScrollPhysics],
507+
/// similar to [PageScrollPhysics] used by [PageView].
508+
///
509+
/// Defaults to true, or to the value of the [parent] physics when there is one.
510+
bool get allowSelectionEdgeScrolling => parent?.allowSelectionEdgeScrolling ?? true;
511+
502512
/// Called whenever a [Scrollable] is rebuilt with a new [ScrollPhysics]
503513
/// of the same [runtimeType].
504514
///

‎packages/flutter/lib/src/widgets/scrollable.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,12 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
13011301
return result;
13021302
}
13031303
if (_selectionStartsInScrollable) {
1304+
// Paged scrollables (like PageView) disallow this; edge scrolling there
1305+
// flips to an adjacent page instead of revealing more content.
1306+
if (!(state.resolvedPhysics?.allowSelectionEdgeScrolling ?? true)) {
1307+
_autoScroller.stopAutoScroll();
1308+
return result;
1309+
}
13041310
_autoScroller.startAutoScrollIfNecessary(_dragTargetFromEvent(event));
13051311
if (_autoScroller.scrolling) {
13061312
return SelectionResult.pending;

‎packages/flutter/test/widgets/scroll_physics_test.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ class TestScrollPhysics extends ScrollPhysics {
3030
void main() {
3131
const kBlueColor = Color(0xFF0000FF);
3232

33+
test('ScrollPhysics.allowSelectionEdgeScrolling', () {
34+
expect(const BouncingScrollPhysics().allowSelectionEdgeScrolling, isTrue);
35+
expect(const PageScrollPhysics().allowSelectionEdgeScrolling, isFalse);
36+
// Resolves through the parent chain so wrapped page physics still count.
37+
expect(
38+
const ClampingScrollPhysics(parent: PageScrollPhysics()).allowSelectionEdgeScrolling,
39+
isFalse,
40+
);
41+
});
42+
3343
test('ScrollPhysics applyTo()', () {
3444
const a = TestScrollPhysics(name: 'a');
3545
const b = TestScrollPhysics(name: 'b');

‎packages/flutter/test/widgets/scrollable_selection_test.dart‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,66 @@ Offset globalize(Offset point, RenderBox box) {
2424
return box.localToGlobal(point);
2525
}
2626

27+
// Drag-selects past the edge of a ListView nested in a PageView along [axis],
28+
// and checks the ListView edge scrolls while the PageView stays on its page.
29+
Future<void> selectPastNestedListEdge(WidgetTester tester, Axis axis) async {
30+
final pageController = PageController();
31+
addTearDown(pageController.dispose);
32+
final listController = ScrollController();
33+
addTearDown(listController.dispose);
34+
await tester.pumpWidget(
35+
TestWidgetsApp(
36+
home: SelectableRegion(
37+
selectionControls: testTextSelectionHandleControls,
38+
child: PageView(
39+
scrollDirection: axis,
40+
controller: pageController,
41+
children: <Widget>[
42+
ListView.builder(
43+
scrollDirection: axis,
44+
controller: listController,
45+
itemCount: 100,
46+
itemBuilder: (BuildContext context, int index) {
47+
return Center(child: Text('Item $index'));
48+
},
49+
),
50+
const Center(child: Text('Second page')),
51+
],
52+
),
53+
),
54+
),
55+
);
56+
await tester.pumpAndSettle();
57+
expect(pageController.page, 0.0);
58+
expect(listController.offset, 0.0);
59+
60+
final RenderParagraph item0 = tester.renderObject<RenderParagraph>(
61+
find.descendant(of: find.text('Item 0'), matching: find.byType(RichText)),
62+
);
63+
final Rect box = tester.getRect(find.byType(PageView));
64+
final target = axis == Axis.vertical
65+
? Offset(box.center.dx, box.bottom + 40.0)
66+
: Offset(box.right + 40.0, box.center.dy);
67+
68+
final TestGesture gesture = await tester.startGesture(
69+
textOffsetToPosition(item0, 2),
70+
kind: PointerDeviceKind.mouse,
71+
);
72+
addTearDown(gesture.removePointer);
73+
await tester.pump();
74+
await gesture.moveTo(target);
75+
await tester.pump();
76+
await tester.pump(const Duration(seconds: 1));
77+
78+
// The inner ListView edge scrolled, but the PageView did not flip pages.
79+
expect(listController.offset, greaterThan(0.0));
80+
expect(pageController.page, 0.0);
81+
expect(tester.takeException(), isNull);
82+
83+
await gesture.up();
84+
await tester.pumpAndSettle();
85+
}
86+
2787
void main() {
2888
TestWidgetsFlutterBinding.ensureInitialized();
2989
final mockClipboard = MockClipboard();
@@ -1499,6 +1559,117 @@ void main() {
14991559
expect(tester.takeException(), isNull);
15001560
});
15011561

1562+
testWidgets(
1563+
'paged scrollable does not flip pages during selection, but text still gets selected',
1564+
(WidgetTester tester) async {
1565+
final controller = PageController();
1566+
addTearDown(controller.dispose);
1567+
await tester.pumpWidget(
1568+
TestWidgetsApp(
1569+
home: SelectableRegion(
1570+
selectionControls: testTextSelectionHandleControls,
1571+
child: PageView.builder(
1572+
controller: controller,
1573+
itemCount: 5,
1574+
itemBuilder: (BuildContext context, int index) {
1575+
return Center(child: Text('Page $index'));
1576+
},
1577+
),
1578+
),
1579+
),
1580+
);
1581+
await tester.pumpAndSettle();
1582+
expect(controller.page, 0.0);
1583+
1584+
final RenderParagraph page0 = tester.renderObject<RenderParagraph>(
1585+
find.descendant(of: find.text('Page 0'), matching: find.byType(RichText)),
1586+
);
1587+
final TestGesture gesture = await tester.startGesture(
1588+
tester.getCenter(find.text('Page 0')),
1589+
kind: ui.PointerDeviceKind.mouse,
1590+
);
1591+
addTearDown(gesture.removePointer);
1592+
await tester.pump();
1593+
expect(controller.page, 0.0);
1594+
1595+
// Hold the drag past the right edge; without the fix this keeps flipping
1596+
// pages toward page 1.
1597+
await gesture.moveTo(tester.getBottomRight(find.byType(PageView)) + const Offset(40.0, 0.0));
1598+
await tester.pump();
1599+
await tester.pump(const Duration(seconds: 1));
1600+
1601+
// The page did not flip, but the page's text was still selected.
1602+
expect(controller.page, 0.0);
1603+
expect(page0.selections, isNotEmpty);
1604+
expect(tester.takeException(), isNull);
1605+
1606+
await gesture.up();
1607+
await tester.pumpAndSettle();
1608+
expect(controller.page, 0.0);
1609+
expect(tester.takeException(), isNull);
1610+
},
1611+
);
1612+
1613+
testWidgets('mouse selection drag still edge auto-scrolls a non page-snapping scrollable', (
1614+
WidgetTester tester,
1615+
) async {
1616+
// Counterpart to the PageView test above: only paged scrollables are
1617+
// affected. A plain ListView still edge scrolls while drag-selecting.
1618+
final controller = ScrollController();
1619+
addTearDown(controller.dispose);
1620+
await tester.pumpWidget(
1621+
TestWidgetsApp(
1622+
home: SelectableRegion(
1623+
selectionControls: testTextSelectionHandleControls,
1624+
child: ListView.builder(
1625+
controller: controller,
1626+
scrollDirection: Axis.horizontal,
1627+
itemCount: 100,
1628+
itemBuilder: (BuildContext context, int index) {
1629+
return Center(child: Text('Item $index'));
1630+
},
1631+
),
1632+
),
1633+
),
1634+
);
1635+
await tester.pumpAndSettle();
1636+
expect(controller.offset, 0.0);
1637+
1638+
final RenderParagraph paragraph0 = tester.renderObject<RenderParagraph>(
1639+
find.descendant(of: find.text('Item 0'), matching: find.byType(RichText)),
1640+
);
1641+
final TestGesture gesture = await tester.startGesture(
1642+
textOffsetToPosition(paragraph0, 2),
1643+
kind: ui.PointerDeviceKind.mouse,
1644+
);
1645+
addTearDown(gesture.removePointer);
1646+
await tester.pump();
1647+
expect(controller.offset, 0.0);
1648+
1649+
// Drag past the right edge to kick off auto-scroll.
1650+
await gesture.moveTo(tester.getBottomRight(find.byType(ListView)) + const Offset(40.0, 0.0));
1651+
await tester.pump();
1652+
await tester.pump(const Duration(seconds: 1));
1653+
expect(controller.offset, greaterThan(0.0));
1654+
expect(tester.takeException(), isNull);
1655+
1656+
await gesture.up();
1657+
await tester.pumpAndSettle();
1658+
expect(tester.takeException(), isNull);
1659+
});
1660+
1661+
testWidgets('vertical ListView inside a PageView still edge scrolls during selection', (
1662+
WidgetTester tester,
1663+
) async {
1664+
await selectPastNestedListEdge(tester, Axis.vertical);
1665+
});
1666+
1667+
testWidgets('horizontal ListView inside a PageView still edge scrolls during selection', (
1668+
WidgetTester tester,
1669+
) async {
1670+
await selectPastNestedListEdge(tester, Axis.horizontal);
1671+
});
1672+
15021673
group('Complex cases', () {
15031674
testWidgets('selection starts outside of the scrollable', (WidgetTester tester) async {
15041675
final controller = ScrollController();

0 commit comments

Comments
 (0)