From 625be62a011e468d3153365fa46149895fb15608 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Fri, 9 Apr 2021 06:24:05 +0800 Subject: [PATCH] fix paste crash when section is invalid (#78948) --- .../flutter/lib/src/rendering/editable.dart | 2 +- .../test/material/text_field_test.dart | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index c8822ab258..75a48933fb 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -2152,7 +2152,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { // Snapshot the input before using `await`. // See https://github.com/flutter/flutter/issues/11427 final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain); - if (data != null) { + if (data != null && selection.isValid) { value = TextEditingValue( text: selection.textBefore(text) + data.text! + selection.textAfter(text), selection: TextSelection.collapsed( diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index b0553f9795..5ab52d3942 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -4823,6 +4823,57 @@ void main() { expect(find.text(expected), findsOneWidget, reason: 'Because text contains ${controller.text}'); }, skip: areKeyEventsHandledByPlatform); + // Regressing test for https://github.com/flutter/flutter/issues/78219 + testWidgets('Paste does not crash when the section is inValid', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(); + final TextEditingController controller = TextEditingController(); + final TextField textField = TextField( + controller: controller, + obscureText: true, + ); + + const String clipboardContent = 'I love Flutter!'; + SystemChannels.platform + .setMockMethodCallHandler((MethodCall methodCall) async { + if (methodCall.method == 'Clipboard.getData') + return {'text': clipboardContent}; + return null; + }); + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: RawKeyboardListener( + focusNode: focusNode, + onKey: null, + child: textField, + ), + ), + ), + ); + focusNode.requestFocus(); + await tester.pump(); + + await tester.tap(find.byType(TextField)); + await tester.pumpAndSettle(); + + // This setter will set `selection` invalid. + controller.text = ''; + + // Paste clipboardContent to the text field. + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlRight); + await tester.sendKeyDownEvent(LogicalKeyboardKey.keyV); + await tester.pumpAndSettle(); + await tester.pump(const Duration(milliseconds: 200)); + await tester.sendKeyUpEvent(LogicalKeyboardKey.keyV); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlRight); + await tester.pumpAndSettle(); + + // Do nothing. + expect(find.text(clipboardContent), findsNothing); + expect(controller.selection, const TextSelection.collapsed(offset: -1)); + }); + testWidgets('Cut test', (WidgetTester tester) async { final FocusNode focusNode = FocusNode(); final TextEditingController controller = TextEditingController();