diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 759a002c58..f50d6a6b96 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1693,12 +1693,12 @@ class EditableTextState extends State with AutomaticKeepAliveClien switch (defaultTargetPlatform) { case TargetPlatform.iOS: - break; case TargetPlatform.macOS: - case TargetPlatform.android: - case TargetPlatform.fuchsia: case TargetPlatform.linux: case TargetPlatform.windows: + break; + case TargetPlatform.android: + case TargetPlatform.fuchsia: // Collapse the selection and hide the toolbar and handles. userUpdateTextEditingValue( TextEditingValue( diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index 0b5756e58f..a2605aadde 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -248,7 +248,7 @@ void main() { await tester.tap(find.text('Copy')); await tester.pumpAndSettle(); expect(controller.text, 'blah1 blah2'); - expect(controller.selection, const TextSelection(baseOffset: 5, extentOffset: 5)); + expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 5)); expect(find.byType(CupertinoButton), findsNothing); // Paste it at the end. diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index d1fd2e57bc..2cf0cb56a1 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -250,7 +250,7 @@ void main() { await tester.tap(find.text('Copy')); await tester.pumpAndSettle(); expect(controller.text, 'blah1 blah2'); - expect(controller.selection, const TextSelection(baseOffset: 5, extentOffset: 5)); + expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 5)); expect(find.byType(CupertinoButton), findsNothing); // Paste it at the end. @@ -359,7 +359,7 @@ void main() { await tester.tap(find.text('Copy')); await tester.pumpAndSettle(); expect(controller.text, 'blah1 blah2'); - expect(controller.selection, const TextSelection(baseOffset: 5, extentOffset: 5)); + expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 5)); expect(find.byType(CupertinoButton), findsNothing); // Paste it at the end. diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index f18ef95095..3436b7301c 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -65,7 +65,7 @@ void main() { await tester.tap(find.text('Copy')); await tester.pumpAndSettle(); expect(controller.text, 'blah1 blah2'); - expect(controller.selection, const TextSelection(baseOffset: 5, extentOffset: 5)); + expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 5)); expect(find.byType(CupertinoButton), findsNothing); // Paste it at the end. @@ -176,7 +176,7 @@ void main() { await tester.tap(find.text('Copy')); await tester.pumpAndSettle(); expect(controller.text, 'blah1 blah2'); - expect(controller.selection, const TextSelection(baseOffset: 5, extentOffset: 5)); + expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 5)); expect(find.byType(CupertinoButton), findsNothing); // Paste it at the end. diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 7f222f8dfd..813606533d 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -1553,6 +1553,82 @@ void main() { expect(find.text('Paste'), findsNothing); }); + testWidgets('Copy selection does not collapse selection on desktop and iOS', (WidgetTester tester) async { + final TextEditingController localController = TextEditingController(text: 'Hello world'); + await tester.pumpWidget( + MaterialApp( + home: EditableText( + backgroundCursorColor: Colors.grey, + controller: localController, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + selectionControls: materialTextSelectionControls, + ), + ), + ); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + + // Show the toolbar. + state.renderEditable.selectWordsInRange( + from: Offset.zero, + cause: SelectionChangedCause.tap, + ); + await tester.pump(); + + final TextSelection copySelectionRange = localController.selection; + + state.showToolbar(); + await tester.pumpAndSettle(); + + expect(find.text('Copy'), findsOneWidget); + + await tester.tap(find.text('Copy')); + await tester.pumpAndSettle(); + expect(copySelectionRange, localController.selection); + expect(find.text('Copy'), findsNothing); + }, skip: kIsWeb, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows })); // [intended] + + testWidgets('Copy selection collapses selection and hides the toolbar on Android and Fuchsia', (WidgetTester tester) async { + final TextEditingController localController = TextEditingController(text: 'Hello world'); + await tester.pumpWidget( + MaterialApp( + home: EditableText( + backgroundCursorColor: Colors.grey, + controller: localController, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + selectionControls: materialTextSelectionControls, + ), + ), + ); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + + // Show the toolbar. + state.renderEditable.selectWordsInRange( + from: Offset.zero, + cause: SelectionChangedCause.tap, + ); + await tester.pump(); + + final TextSelection copySelectionRange = localController.selection; + + state.showToolbar(); + await tester.pumpAndSettle(); + + expect(find.text('Copy'), findsOneWidget); + + await tester.tap(find.text('Copy')); + await tester.pumpAndSettle(); + expect(localController.selection, TextSelection.collapsed(offset: copySelectionRange.extentOffset)); + expect(find.text('Copy'), findsNothing); + }, skip: kIsWeb, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.fuchsia })); // [intended] + testWidgets('can show the toolbar after clearing all text', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/35998. await tester.pumpWidget(