diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index af7f1fb4a0..688b1095f4 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -314,7 +314,7 @@ class TextEditingController extends ValueNotifier { /// If the new selection is outside the composing range, the composing range is /// cleared. set selection(TextSelection newSelection) { - if (!_isSelectionWithinTextBounds(newSelection)) { + if (text.length < newSelection.end || text.length < newSelection.start) { throw FlutterError('invalid text selection: $newSelection'); } final TextRange newComposing = _isSelectionWithinComposingRange(newSelection) ? value.composing : TextRange.empty; @@ -348,11 +348,6 @@ class TextEditingController extends ValueNotifier { 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; - } - /// Check that the [selection] is inside of the composing range. bool _isSelectionWithinComposingRange(TextSelection selection) { return selection.start >= value.composing.start && selection.end <= value.composing.end; @@ -3953,7 +3948,8 @@ class EditableTextState extends State with AutomaticKeepAliveClien // 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)) { + final String text = widget.controller.value.text; + if (text.length < selection.end || text.length < selection.start) { return; } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 2639d3d936..9afa7100a4 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -17654,6 +17654,27 @@ void main() { expect(tester.takeException(), isNull); }); + + testWidgets('Can implement TextEditingController', (WidgetTester tester) async { + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: EditableText( + autofocus: true, + backgroundCursorColor: Colors.grey, + controller: _TextEditingControllerImpl(), + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ); + + expect(tester.takeException(), isNull); + }); } class UnsettableController extends TextEditingController { @@ -18016,6 +18037,36 @@ class _AccentColorTextEditingController extends TextEditingController { } } +class _TextEditingControllerImpl extends ChangeNotifier implements TextEditingController { + final TextEditingController _innerController = TextEditingController(); + + @override + void clear() => _innerController.clear(); + + @override + void clearComposing() => _innerController.clearComposing(); + + @override + TextSelection get selection => _innerController.selection; + @override + set selection(TextSelection newSelection) => _innerController.selection = newSelection; + + @override + String get text => _innerController.text; + @override + set text(String newText) => _innerController.text = newText; + + @override + TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) { + return _innerController.buildTextSpan(context: context, style: style, withComposing: withComposing); + } + + @override + TextEditingValue get value => _innerController.value; + @override + set value(TextEditingValue newValue) => _innerController.value = newValue; +} + class _TestScrollController extends ScrollController { bool get attached => hasListeners; }