From c61e55ad6b567b6cd04646043909490c0859de95 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 17 Jan 2020 10:58:01 -0800 Subject: [PATCH] Selection menu not showing when selection is 0,0 (#49000) --- .../flutter/lib/src/rendering/editable.dart | 10 +++++++-- .../test/material/text_field_test.dart | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 1b89c0e783..171bdc776e 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -409,8 +409,14 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { ) { // Changes made by the keyboard can sometimes be "out of band" for listening // components, so always send those events, even if we didn't think it - // changed. - if (nextSelection == selection && cause != SelectionChangedCause.keyboard) { + // changed. Also, focusing an empty field is sent as a selection change even + // if the selection offset didn't change. + final bool focusingEmpty = nextSelection.baseOffset == 0 + && nextSelection.extentOffset == 0 + && !hasFocus; + if (nextSelection == selection + && cause != SelectionChangedCause.keyboard + && !focusingEmpty) { return; } if (onSelectionChanged != null) { diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 5c677f67da..84a0068d61 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -857,6 +857,27 @@ void main() { expect(handle.opacity.value, equals(1.0)); }); + + testWidgets('Long pressing a field with selection 0,0 shows the selection menu', (WidgetTester tester) async { + await tester.pumpWidget(overlay( + child: TextField( + controller: TextEditingController.fromValue( + const TextEditingValue( + selection: TextSelection(baseOffset: 0, extentOffset: 0), + ), + ), + ), + )); + + expect(find.text('PASTE'), findsNothing); + + final Offset emptyPos = textOffsetToPosition(tester, 0); + await tester.longPressAt(emptyPos, pointer: 7); + await tester.pumpAndSettle(); + + expect(find.text('PASTE'), findsOneWidget); + }); + testWidgets('Entering text hides selection handle caret', (WidgetTester tester) async { final TextEditingController controller = TextEditingController();