Remove TextEditingController private member access (#149042)
Fixes https://github.com/flutter/flutter/issues/148692 I intend to CP this.
This commit is contained in:
@@ -314,7 +314,7 @@ class TextEditingController extends ValueNotifier<TextEditingValue> {
|
||||
/// 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<TextEditingValue> {
|
||||
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<EditableText> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user