From 88852731a5bb15730e298a9abcbbf54c1516a721 Mon Sep 17 00:00:00 2001 From: Per Classon Date: Fri, 6 Mar 2020 10:26:01 +0100 Subject: [PATCH] Validate text selection before updating it inside of EditableText (#51410) --- .../lib/src/widgets/editable_text.dart | 13 +++++++++- .../test/material/text_field_test.dart | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 1b7a48406c..e02cea6a99 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -189,7 +189,7 @@ class TextEditingController extends ValueNotifier { /// in a separate statement. To change both the [text] and the [selection] /// change the controller's [value]. set selection(TextSelection newSelection) { - if (newSelection.start > text.length || newSelection.end > text.length) + if (!isSelectionWithinTextBounds(newSelection)) throw FlutterError('invalid text selection: $newSelection'); value = value.copyWith(selection: newSelection, composing: TextRange.empty); } @@ -220,6 +220,11 @@ class TextEditingController extends ValueNotifier { void clearComposing() { value = value.copyWith(composing: TextRange.empty); } + + /// Check that the [selection] is inside of the bounds of [text]. + bool isSelectionWithinTextBounds(TextSelection selection) { + return selection.start <= text.length && selection.end <= text.length; + } } /// Toolbar configuration for [EditableText]. @@ -1530,6 +1535,12 @@ class EditableTextState extends State with AutomaticKeepAliveClien } void _handleSelectionChanged(TextSelection selection, RenderEditable renderObject, SelectionChangedCause cause) { + // We return early if the selection is not valid. This can happen when the + // text of [EditableText] is updated at the same time as the selection is + // changed by a gesture event. + if (!widget.controller.isSelectionWithinTextBounds(selection)) + return; + widget.controller.selection = selection; // This will show the keyboard for all selection changes on the diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 08ef5eb7f8..dfdd77ac98 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -3378,6 +3378,30 @@ void main() { } }); + // Regression test for https://github.com/flutter/flutter/issues/35848 + testWidgets('Clearing text field with suffixIcon does not cause text selection exception', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'Prefilled text.', + ); + + await tester.pumpWidget( + boilerplate( + child: TextField( + controller: controller, + decoration: InputDecoration( + suffixIcon: IconButton( + icon: const Icon(Icons.close), + onPressed: controller.clear, + ), + ), + ), + ), + ); + + await tester.tap(find.byType(IconButton)); + expect(controller.text, ''); + }); + testWidgets('maxLength limits input.', (WidgetTester tester) async { final TextEditingController textController = TextEditingController();