Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix edge scrolling on platforms that select word by word on long press move #113128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 2 additions & 6 deletions packages/flutter/lib/src/cupertino/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1035,16 +1035,12 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
if (cause == SelectionChangedCause.longPress
|| cause == SelectionChangedCause.drag) {
_editableText.bringIntoView(selection.extent);
}
break;
case TargetPlatform.linux:
case TargetPlatform.windows:
case TargetPlatform.fuchsia:
case TargetPlatform.android:
if (cause == SelectionChangedCause.drag) {
if (cause == SelectionChangedCause.longPress
|| cause == SelectionChangedCause.drag) {
_editableText.bringIntoView(selection.extent);
}
break;
Expand Down
69 changes: 4 additions & 65 deletions packages/flutter/lib/src/material/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,45 +62,6 @@ class _TextFieldSelectionGestureDetectorBuilder extends TextSelectionGestureDete
// Not required.
}

@override
void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
if (delegate.selectionEnabled) {
final TargetPlatform targetPlatform = Theme.of(_state.context).platform;

switch (targetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditable.selectWordsInRange(
from: details.globalPosition - details.offsetFromOrigin,
to: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
}

switch (targetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
editableText.showMagnifier(details.globalPosition);
break;
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
break;
}
}
}

@override
void onSingleTapUp(TapUpDetails details) {
super.onSingleTapUp(details);
Expand All @@ -110,37 +71,19 @@ class _TextFieldSelectionGestureDetectorBuilder extends TextSelectionGestureDete

@override
void onSingleLongTapStart(LongPressStartDetails details) {
super.onSingleLongTapStart(details);
if (delegate.selectionEnabled) {
final TargetPlatform targetPlatform = Theme.of(_state.context).platform;

switch (targetPlatform) {
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditable.selectWord(cause: SelectionChangedCause.longPress);
Feedback.forLongPress(_state.context);
break;
}

switch (targetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
editableText.showMagnifier(details.globalPosition);
break;
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
break;
}
}
}
}
Expand Down Expand Up @@ -1150,16 +1093,12 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
switch (Theme.of(context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
if (cause == SelectionChangedCause.longPress
|| cause == SelectionChangedCause.drag) {
_editableText?.bringIntoView(selection.extent);
}
break;
case TargetPlatform.linux:
case TargetPlatform.windows:
case TargetPlatform.fuchsia:
case TargetPlatform.android:
if (cause == SelectionChangedCause.drag) {
if (cause == SelectionChangedCause.longPress
|| cause == SelectionChangedCause.drag) {
_editableText?.bringIntoView(selection.extent);
}
break;
Expand Down
54 changes: 47 additions & 7 deletions packages/flutter/lib/src/widgets/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1996,10 +1996,21 @@ class TextSelectionGestureDetectorBuilder {
@protected
void onSingleLongTapStart(LongPressStartDetails details) {
if (delegate.selectionEnabled) {
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditable.selectWord(cause: SelectionChangedCause.longPress);
break;
}

switch (defaultTargetPlatform) {
case TargetPlatform.android:
Expand All @@ -2012,6 +2023,9 @@ class TextSelectionGestureDetectorBuilder {
case TargetPlatform.windows:
break;
}

_dragStartViewportOffset = renderEditable.offset.pixels;
_dragStartScrollOffset = _scrollPosition;
}
}

Expand All @@ -2027,11 +2041,35 @@ class TextSelectionGestureDetectorBuilder {
@protected
void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
if (delegate.selectionEnabled) {
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
// Adjust the drag start offset for possible viewport offset changes.
final Offset editableOffset = renderEditable.maxLines == 1
? Offset(renderEditable.offset.pixels - _dragStartViewportOffset, 0.0)
: Offset(0.0, renderEditable.offset.pixels - _dragStartViewportOffset);
final Offset scrollableOffset = Offset(
0.0,
_scrollPosition - _dragStartScrollOffset,
);

switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditable.selectWordsInRange(
from: details.globalPosition - details.offsetFromOrigin - editableOffset - scrollableOffset,
to: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
}

switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
Expand Down Expand Up @@ -2070,6 +2108,8 @@ class TextSelectionGestureDetectorBuilder {
if (shouldShowSelectionToolbar) {
editableText.showToolbar();
}
_dragStartViewportOffset = 0.0;
_dragStartScrollOffset = 0.0;
}

/// Handler for [TextSelectionGestureDetector.onSecondaryTap].
Expand Down
Loading