diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 09fea17285..07853bb918 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -1269,7 +1269,9 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, // [assembleSemanticsNode] invocations. LinkedHashMap? _cachedChildNodes; - /// Returns a list of rects that bound the given selection. + /// Returns a list of rects that bound the given selection, and the text + /// direction. The text direction is used by the engine to calculate + /// the closest position to a given point. /// /// See [TextPainter.getBoxesForSelection] for more details. List getBoxesForSelection(TextSelection selection) { diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 7187bfb58a..607200d9c7 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2792,6 +2792,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (_textInputConnection?.scribbleInProgress ?? false) { cause = SelectionChangedCause.scribble; } else if (_pointOffsetOrigin != null) { + // For floating cursor selection when force pressing the space bar. cause = SelectionChangedCause.forcePress; } else { cause = SelectionChangedCause.keyboard; @@ -2941,8 +2942,22 @@ class EditableTextState extends State with AutomaticKeepAliveClien final Offset finalPosition = renderEditable.getLocalRectForCaret(_lastTextPosition!).centerLeft - _floatingCursorOffset; if (_floatingCursorResetController!.isCompleted) { renderEditable.setFloatingCursor(FloatingCursorDragState.End, finalPosition, _lastTextPosition!); - // Only change if the current selection range is collapsed, to prevent - // overwriting the result of the iOS keyboard selection gesture. + // During a floating cursor's move gesture (1 finger), a cursor is + // animated only visually, without actually updating the selection. + // Only after move gesture is complete, this function will be called + // to actually update the selection to the new cursor location with + // zero selection length. + + // However, During a floating cursor's selection gesture (2 fingers), the + // selection is constantly updated by the engine throughout the gesture. + // Thus when the gesture is complete, we should not update the selection + // to the cursor location with zero selection length, because that would + // overwrite the selection made by floating cursor selection. + + // Here we use `isCollapsed` to distinguish between floating cursor's + // move gesture (1 finger) vs selection gesture (2 fingers), as + // the engine does not provide information other than notifying a + // new selection during with selection gesture (2 fingers). if (renderEditable.selection!.isCollapsed) { // The cause is technically the force cursor, but the cause is listed as tap as the desired functionality is the same. _handleSelectionChanged(TextSelection.fromPosition(_lastTextPosition!), SelectionChangedCause.forcePress);