Skip to content

Commit

Permalink
Remove TextEditingController private member access (#149042)
Browse files Browse the repository at this point in the history
Fixes flutter/flutter#148692

I intend to CP this.
  • Loading branch information
LongCatIsLooong authored May 28, 2024
1 parent 6543a21 commit b1221a9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
51 changes: 51 additions & 0 deletions packages/flutter/test/widgets/editable_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b1221a9

Please sign in to comment.