diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index cdbb92c834..6e913e709d 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -712,33 +712,57 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { // case where the user moves the cursor to the end or beginning of the text // and then back up or down. if (downArrow || upArrow) { - // The caret offset gives a location in the upper left hand corner of - // the caret so the middle of the line above is a half line above that - // point and the line below is 1.5 lines below that point. - final double preferredLineHeight = _textPainter.preferredLineHeight; - final double verticalOffset = upArrow ? -0.5 * preferredLineHeight : 1.5 * preferredLineHeight; - - final Offset caretOffset = _textPainter.getOffsetForCaret(TextPosition(offset: newSelection.extentOffset), _caretPrototype); - final Offset caretOffsetTranslated = caretOffset.translate(0.0, verticalOffset); - final TextPosition position = _textPainter.getPositionForOffset(caretOffsetTranslated); - - // To account for the possibility where the user vertically highlights - // all the way to the top or bottom of the text, we hold the previous - // cursor location. This allows us to restore to this position in the - // case that the user wants to unhighlight some text. - if (position.offset == newSelection.extentOffset) { - if (downArrow) { - newSelection = newSelection.copyWith(extentOffset: _plainText.length); - } else if (upArrow) { - newSelection = newSelection.copyWith(extentOffset: 0); + if (lineModifier) { + if (upArrow) { + // Extend the selection to the beginning of the field. + final int upperOffset = math.max(0, math.max( + newSelection.baseOffset, + newSelection.extentOffset, + )); + newSelection = TextSelection( + baseOffset: shift ? upperOffset : 0, + extentOffset: 0, + ); + } else { + // Extend the selection to the end of the field. + final int lowerOffset = math.max(0, math.min( + newSelection.baseOffset, + newSelection.extentOffset, + )); + newSelection = TextSelection( + baseOffset: shift ? lowerOffset : _plainText.length, + extentOffset: _plainText.length, + ); } - _wasSelectingVerticallyWithKeyboard = shift; - } else if (_wasSelectingVerticallyWithKeyboard && shift) { - newSelection = newSelection.copyWith(extentOffset: _cursorResetLocation); - _wasSelectingVerticallyWithKeyboard = false; } else { - newSelection = newSelection.copyWith(extentOffset: position.offset); - _cursorResetLocation = newSelection.extentOffset; + // The caret offset gives a location in the upper left hand corner of + // the caret so the middle of the line above is a half line above that + // point and the line below is 1.5 lines below that point. + final double preferredLineHeight = _textPainter.preferredLineHeight; + final double verticalOffset = upArrow ? -0.5 * preferredLineHeight : 1.5 * preferredLineHeight; + + final Offset caretOffset = _textPainter.getOffsetForCaret(TextPosition(offset: newSelection.extentOffset), _caretPrototype); + final Offset caretOffsetTranslated = caretOffset.translate(0.0, verticalOffset); + final TextPosition position = _textPainter.getPositionForOffset(caretOffsetTranslated); + + // To account for the possibility where the user vertically highlights + // all the way to the top or bottom of the text, we hold the previous + // cursor location. This allows us to restore to this position in the + // case that the user wants to unhighlight some text. + if (position.offset == newSelection.extentOffset) { + if (downArrow) { + newSelection = newSelection.copyWith(extentOffset: _plainText.length); + } else if (upArrow) { + newSelection = newSelection.copyWith(extentOffset: 0); + } + _wasSelectingVerticallyWithKeyboard = shift; + } else if (_wasSelectingVerticallyWithKeyboard && shift) { + newSelection = newSelection.copyWith(extentOffset: _cursorResetLocation); + _wasSelectingVerticallyWithKeyboard = false; + } else { + newSelection = newSelection.copyWith(extentOffset: position.offset); + _cursorResetLocation = newSelection.extentOffset; + } } } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 61e89d2ac4..eb9ab17985 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -4167,6 +4167,144 @@ void main() { reason: 'on $platform', ); + // Jump to end. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowDown, + ], + shift: false, + lineModifier: true, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection.collapsed( + offset: testText.length, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + + // Jump to start. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowUp, + ], + shift: false, + lineModifier: true, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection.collapsed( + offset: 0, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + + // Move forward a few letters + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowRight, + LogicalKeyboardKey.arrowRight, + LogicalKeyboardKey.arrowRight, + ], + shift: false, + lineModifier: false, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection.collapsed( + offset: 3, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + + // Select to end. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowDown, + ], + shift: true, + lineModifier: true, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection( + baseOffset: 3, + extentOffset: testText.length, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + + // Select to start, which extends the selection. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowUp, + ], + shift: true, + lineModifier: true, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection( + baseOffset: testText.length, + extentOffset: 0, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + + // Move to start again. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowUp, + ], + shift: false, + lineModifier: false, + platform: platform, + ); + + expect( + selection, + equals( + const TextSelection.collapsed( + offset: 0, + affinity: TextAffinity.downstream, + ), + ), + reason: 'on $platform', + ); + // Jump forward three words. await sendKeys( tester,