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
Refactor comments for scrollable snapping behavior
Updated comments to use Dart documentation style.
  • Loading branch information
mbcorona committed Jul 23, 2026
commit b2041c7134976f8249b257f13430a733115dd6f1
23 changes: 0 additions & 23 deletions packages/flutter/lib/src/rendering/selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
/// @docImport 'package:flutter/material.dart';
library;

import 'dart:ui' show PointerDeviceKind;

import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math_64.dart';

Expand Down Expand Up @@ -522,14 +520,9 @@ class SelectionEdgeUpdateEvent extends SelectionEvent {
///
/// The [granularity] contains the granularity which the selection edge should move by.
/// This value defaults to [TextGranularity.character].
///
/// The [deviceKind] is the pointer device that drove this update, if known,
/// so edge auto-scrolling can respect the enclosing scrollable's drag
/// devices. It is null when the driving device isn't known.
const SelectionEdgeUpdateEvent.forStart({
required this.globalPosition,
TextGranularity? granularity,
this.deviceKind,
}) : granularity = granularity ?? TextGranularity.character,
super._(SelectionEventType.startEdgeUpdate);

Expand All @@ -539,14 +532,9 @@ class SelectionEdgeUpdateEvent extends SelectionEvent {
///
/// The [granularity] contains the granularity which the selection edge should move by.
/// This value defaults to [TextGranularity.character].
///
/// The [deviceKind] is the pointer device that drove this update, if known,
/// so edge auto-scrolling can respect the enclosing scrollable's drag
/// devices. It is null when the driving device isn't known.
const SelectionEdgeUpdateEvent.forEnd({
required this.globalPosition,
TextGranularity? granularity,
this.deviceKind,
}) : granularity = granularity ?? TextGranularity.character,
super._(SelectionEventType.endEdgeUpdate);

Expand All @@ -559,17 +547,6 @@ class SelectionEdgeUpdateEvent extends SelectionEvent {
///
/// Defaults to [TextGranularity.character].
final TextGranularity granularity;

/// The pointer device that drove this selection edge update.
///
/// Edge auto-scrolling uses this to decide whether the enclosing scrollable
/// should scroll while selecting: a paged scrollable ignores devices that
/// aren't in its [ScrollBehavior.dragDevices].
///
/// This is null when the device isn't known, for example an event synthesized
/// to keep a newly added selectable in sync. A null value leaves
/// auto-scrolling untouched.
final PointerDeviceKind? deviceKind;
}

/// Extends the start or end of the selection by a given [TextGranularity].
Expand Down
3 changes: 3 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,9 @@ class PageScrollPhysics extends ScrollPhysics {
return PageScrollPhysics(parent: buildParent(ancestor));
}

@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;

double _getPage(ScrollMetrics position) {
if (position is _PagePosition) {
return position.page!;
Expand Down
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/scroll_physics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +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.
///
/// 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;

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.

Thinking about this more, maybe this could be named allowSelectionEdgeScrolling in the same vein as allowUserScrolling and allowImplicitScrolling. This makes it clear for a consumer that unless they are some selection mechanism they do not have to worry about consuming this. If we do make this change, the default would need to flip to true. Do you have any thoughts on this?


@override
String toString() {
if (parent == null) {
Expand Down
35 changes: 3 additions & 32 deletions packages/flutter/lib/src/widgets/scrollable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import 'framework.dart';
import 'gesture_detector.dart';
import 'media_query.dart';
import 'notification_listener.dart';
import 'page_view.dart';
import 'restoration.dart';
import 'restoration_properties.dart';
import 'scroll_activity.dart';
Expand Down Expand Up @@ -1278,7 +1277,6 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
event = SelectionEdgeUpdateEvent.forEnd(
globalPosition: endOffset,
granularity: event.granularity,
deviceKind: event.deviceKind,
);
} else {
_currentDragStartRelatedToOrigin = _inferPositionRelatedToOrigin(event.globalPosition);
Expand All @@ -1289,7 +1287,6 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
event = SelectionEdgeUpdateEvent.forStart(
globalPosition: startOffset,
granularity: event.granularity,
deviceKind: event.deviceKind,
);
}
final SelectionResult result = super.handleSelectionEdgeUpdate(event);
Expand All @@ -1302,20 +1299,9 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
return result;
}
if (_selectionStartsInScrollable) {
// Don't let a selection drag flip a PageView or TabBarView to the next
// page when it wouldn't accept a plain drag from this device (a mouse, on
// desktop and web). Otherwise the page animates over and snaps back mid
// selection. Other scrollables like ListView still auto scroll so mouse
// "select to scroll" keeps working. A null device kind means we don't
// know, so leave the behavior as it was.
//
// Note this is a different case from the shouldAcceptUserOffset check in
// EdgeDraggingAutoScroller; page physics accept user offset, so that
// check never catches this.
final PointerDeviceKind? deviceKind = event.deviceKind;
if (deviceKind != null &&
_scrollableSnapsToPages &&
!state._configuration.dragDevices.contains(deviceKind)) {
// 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) {
_autoScroller.stopAutoScroll();
return result;
}
Expand All @@ -1327,21 +1313,6 @@ class _ScrollableSelectionContainerDelegate extends MultiSelectableSelectionCont
return result;
}

// Whether this scrollable snaps to pages, like a PageView or TabBarView.
// Page physics can be wrapped by other physics, so check the whole chain.
bool get _scrollableSnapsToPages {
for (
ScrollPhysics? physics = state.resolvedPhysics;
physics != null;
physics = physics.parent
) {
if (physics is PageScrollPhysics) {
return true;
}
}
return false;
}

Offset _inferPositionRelatedToOrigin(Offset globalPosition) {
final box = state.context.findRenderObject()! as RenderBox;
final Offset localPosition = box.globalToLocal(globalPosition);
Expand Down
2 changes: 0 additions & 2 deletions packages/flutter/lib/src/widgets/selectable_region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,6 @@ class SelectableRegionState extends State<SelectableRegion>
SelectionEdgeUpdateEvent.forEnd(
globalPosition: _selectionEndPosition!,
granularity: textGranularity,
deviceKind: _lastPointerDeviceKind,
),
) ==
SelectionResult.pending) {
Expand Down Expand Up @@ -1177,7 +1176,6 @@ class SelectableRegionState extends State<SelectableRegion>
SelectionEdgeUpdateEvent.forStart(
globalPosition: _selectionStartPosition!,
granularity: textGranularity,
deviceKind: _lastPointerDeviceKind,
),
) ==
SelectionResult.pending) {
Expand Down
7 changes: 7 additions & 0 deletions packages/flutter/test/widgets/scroll_physics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ 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 applyTo()', () {
const a = TestScrollPhysics(name: 'a');
const b = TestScrollPhysics(name: 'b');
Expand Down
11 changes: 5 additions & 6 deletions packages/flutter/test/widgets/scrollable_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,8 @@ void main() {
WidgetTester tester,
) async {
// Regression test for https://github.com/flutter/flutter/issues/149426.
// A mouse isn't one of a PageView's default drag devices, so dragging a
// selection past its edge shouldn't flip to the next page and snap back.
// A paged scrollable snaps to pages, so dragging a selection past its edge

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 can remove this comment since it is redundant with the test title.

// must not flip to the next page and snap back.
final controller = PageController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Expand Down Expand Up @@ -1553,10 +1553,9 @@ void main() {
testWidgets('mouse selection drag still edge auto-scrolls a non page-snapping scrollable', (
WidgetTester tester,
) async {
// The counterpart to the PageView test above: the fix only touches paged
// scrollables. A plain ListView still scrolls while you drag-select with a
// mouse ("select to scroll"), even though a mouse isn't one of its drag
// devices. See https://github.com/flutter/flutter/issues/149426.
// 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.
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Expand Down
Loading