diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 50851e85e6..4e5474a9b9 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -4115,11 +4115,13 @@ class EditableTextState extends State with AutomaticKeepAliveClien _textInputConnection!.setSelectionRects(rects); } - // Sends the current composing rect to the iOS text input plugin via the text - // input channel. We need to keep sending the information even if no text is - // currently marked, as the information usually lags behind. The text input - // plugin needs to estimate the composing rect based on the latest caret rect, - // when the composing rect info didn't arrive in time. + // Sends the current composing rect to the embedder's text input plugin. + // + // In cases where the composing rect hasn't been updated in the embedder due + // to the lag of asynchronous messages over the channel, the position of the + // current caret rect is used instead. + // + // See: [_updateCaretRectIfNeeded] void _updateComposingRectIfNeeded() { final TextRange composingRange = _value.composing; assert(mounted); @@ -4133,12 +4135,24 @@ class EditableTextState extends State with AutomaticKeepAliveClien _textInputConnection!.setComposingRect(composingRect); } + // Sends the current caret rect to the embedder's text input plugin. + // + // The position of the caret rect is updated periodically such that if the + // user initiates composing input, the current cursor rect can be used for + // the first character until the composing rect can be sent. + // + // On selection changes, the start of the selection is used. This ensures + // that regardless of the direction the selection was created, the cursor is + // set to the position where next text input occurs. This position is used to + // position the IME's candidate selection menu. + // + // See: [_updateComposingRectIfNeeded] void _updateCaretRectIfNeeded() { final TextSelection? selection = renderEditable.selection; - if (selection == null || !selection.isValid || !selection.isCollapsed) { + if (selection == null || !selection.isValid) { return; } - final TextPosition currentTextPosition = TextPosition(offset: selection.baseOffset); + final TextPosition currentTextPosition = TextPosition(offset: selection.start); final Rect caretRect = renderEditable.getLocalRectForCaret(currentTextPosition); _textInputConnection!.setCaretRect(caretRect); } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index e4acbf0ac6..f601265f6b 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -5875,16 +5875,48 @@ void main() { ); testWidgetsWithLeakTracking( - 'not sent with selection', + 'set to selection start on forward selection', (WidgetTester tester) async { controller.value = TextEditingValue( text: 'a' * 100, - selection: const TextSelection(baseOffset: 0, extentOffset: 10), + selection: const TextSelection(baseOffset: 10, extentOffset: 30), ); await tester.pumpWidget(builder()); await tester.showKeyboard(find.byType(EditableText)); - expect(tester.testTextInput.log, isNot(contains(matchesMethodCall('TextInput.setCaretRect')))); + expect(tester.testTextInput.log, contains( + matchesMethodCall( + 'TextInput.setCaretRect', + // Now the composing range is not empty. + args: allOf( + containsPair('x', equals(140)), + containsPair('y', equals(0)), + ), + ), + )); + }, + ); + + testWidgetsWithLeakTracking( + 'set to selection start on reversed selection', + (WidgetTester tester) async { + controller.value = TextEditingValue( + text: 'a' * 100, + selection: const TextSelection(baseOffset: 30, extentOffset: 10), + ); + await tester.pumpWidget(builder()); + await tester.showKeyboard(find.byType(EditableText)); + + expect(tester.testTextInput.log, contains( + matchesMethodCall( + 'TextInput.setCaretRect', + // Now the composing range is not empty. + args: allOf( + containsPair('x', equals(140)), + containsPair('y', equals(0)), + ), + ), + )); }, ); });