From c5866c5e20f019b30f10455a0787a41aa5bd62e0 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Thu, 29 Jul 2021 16:49:06 -0700 Subject: [PATCH] Explicitly set affinity when moving left/right by line (#87133) --- packages/flutter/lib/src/rendering/editable.dart | 2 ++ packages/flutter/test/rendering/editable_test.dart | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 694412dbb6..3ae8315baa 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -1965,6 +1965,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, final TextSelection selectedLine = _getLineAtOffset(TextPosition(offset: startPoint)); final TextSelection nextSelection = TextSelection.collapsed( offset: selectedLine.baseOffset, + affinity: TextAffinity.downstream, ); _setSelection(nextSelection, cause); @@ -2051,6 +2052,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, final TextSelection selectedLine = _getLineAtOffset(TextPosition(offset: startPoint)); final TextSelection nextSelection = TextSelection.collapsed( offset: selectedLine.extentOffset, + affinity: TextAffinity.upstream, ); _setSelection(nextSelection, cause); diff --git a/packages/flutter/test/rendering/editable_test.dart b/packages/flutter/test/rendering/editable_test.dart index d68bcae49c..84bbc7a327 100644 --- a/packages/flutter/test/rendering/editable_test.dart +++ b/packages/flutter/test/rendering/editable_test.dart @@ -925,6 +925,7 @@ void main() { editable.moveSelectionRightByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 13); + expect(currentSelection.affinity, TextAffinity.upstream); // RenderEditable relies on its parent that passes onSelectionChanged to set // the selection. @@ -933,21 +934,25 @@ void main() { editable.moveSelectionRightByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 13); + expect(currentSelection.affinity, TextAffinity.upstream); // Move back to the start of the line. editable.moveSelectionLeftByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 0); + expect(currentSelection.affinity, TextAffinity.downstream); // Trying moveSelectionLeftByLine does nothing at the leftmost of the field. editable.moveSelectionLeftByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 0); + expect(currentSelection.affinity, TextAffinity.downstream); // Move the selection to the empty line. editable.moveSelectionRightByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 13); + expect(currentSelection.affinity, TextAffinity.upstream); editable.moveSelectionRight(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 14); @@ -957,9 +962,11 @@ void main() { editable.moveSelectionLeftByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 14); + expect(currentSelection.affinity, TextAffinity.downstream); editable.moveSelectionRightByLine(SelectionChangedCause.keyboard); expect(currentSelection.isCollapsed, true); expect(currentSelection.baseOffset, 14); + expect(currentSelection.affinity, TextAffinity.downstream); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/61021 test('arrow keys and delete handle simple text correctly', () async {