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
Refine allowSelectionEdgeScrolling docs and add nested ListView scrol…
…l tests
  • Loading branch information
mbcorona committed Aug 21, 2026
commit a39cf436bec986a3060c7478576ff81eae715fab
2 changes: 2 additions & 0 deletions packages/flutter/lib/src/widgets/page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ class PageScrollPhysics extends ScrollPhysics {
return PageScrollPhysics(parent: buildParent(ancestor));
}

// Selection gestures such as dragging to select text must not scroll a
// PageView to an adjacent page, so edge scrolling is disallowed here.
@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 allowSelectionEdgeScrolling => false;

Expand Down
12 changes: 7 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,14 @@ class ScrollPhysics {
/// Whether a viewport is allowed to change the scroll position as the result of user input.
bool get allowUserScrolling => true;

/// Whether a viewport may scroll to its edge as a side effect of extending a
/// selection past the visible area.
/// Whether a viewport may scroll to reveal more content when a selection
/// gesture extends past the visible area, i.e. the viewport's edge.
///
/// Defaults to true. Paged scrollables such as [PageView] override this to
/// false, since edge scrolling there flips to an adjacent page instead of
/// revealing more content.
/// 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
Contributor

Choose a reason for hiding this comment

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

This defaults line should also mention the interaction with the parent.

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. The docs now note it resolves through the parent physics.

bool get allowSelectionEdgeScrolling => parent?.allowSelectionEdgeScrolling ?? true;

@override
Expand Down
72 changes: 72 additions & 0 deletions packages/flutter/test/widgets/scrollable_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,66 @@ Offset globalize(Offset point, RenderBox box) {
return box.localToGlobal(point);
}

// Drag-selects past the edge of a ListView nested in a PageView along [axis],
// and checks the ListView edge scrolls while the PageView stays on its page.
Future<void> selectPastNestedListEdge(WidgetTester tester, Axis axis) async {
final pageController = PageController();
addTearDown(pageController.dispose);
final listController = ScrollController();
addTearDown(listController.dispose);
await tester.pumpWidget(
TestWidgetsApp(
home: SelectableRegion(
selectionControls: testTextSelectionHandleControls,
child: PageView(
scrollDirection: axis,
controller: pageController,
children: <Widget>[
ListView.builder(
scrollDirection: axis,
controller: listController,
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return Center(child: Text('Item $index'));
},
),
const Center(child: Text('Second page')),
],
),
),
),
);
await tester.pumpAndSettle();
expect(pageController.page, 0.0);
expect(listController.offset, 0.0);

final RenderParagraph item0 = tester.renderObject<RenderParagraph>(
find.descendant(of: find.text('Item 0'), matching: find.byType(RichText)),
);
final Rect box = tester.getRect(find.byType(PageView));
final target = axis == Axis.vertical
? Offset(box.center.dx, box.bottom + 40.0)
: Offset(box.right + 40.0, box.center.dy);

final TestGesture gesture = await tester.startGesture(
textOffsetToPosition(item0, 2),
kind: PointerDeviceKind.mouse,
);
addTearDown(gesture.removePointer);
await tester.pump();
await gesture.moveTo(target);
await tester.pump();
await tester.pump(const Duration(seconds: 1));

// The inner ListView edge scrolled, but the PageView did not flip pages.
expect(listController.offset, greaterThan(0.0));
expect(pageController.page, 0.0);
expect(tester.takeException(), isNull);

await gesture.up();
await tester.pumpAndSettle();
}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final mockClipboard = MockClipboard();
Expand Down Expand Up @@ -1600,6 +1660,18 @@ void main() {
expect(tester.takeException(), isNull);
});

testWidgets('vertical ListView inside a PageView still edge scrolls during selection', (
WidgetTester tester,
) async {
await selectPastNestedListEdge(tester, Axis.vertical);
});

testWidgets('horizontal ListView inside a PageView still edge scrolls during selection', (
WidgetTester tester,
) async {
await selectPastNestedListEdge(tester, Axis.horizontal);
});

group('Complex cases', () {
testWidgets('selection starts outside of the scrollable', (WidgetTester tester) async {
final controller = ScrollController();
Expand Down