Unverified Commit b5139515 authored by chunhtai's avatar chunhtai Committed by GitHub

Revert "fixes TextInputFormatter gets wrong old value of a selection (#75541)" (#76627)

This reverts commit a46139a2.
parent c0b5db6c
...@@ -102,7 +102,6 @@ class _CupertinoTextFieldSelectionGestureDetectorBuilder extends TextSelectionGe ...@@ -102,7 +102,6 @@ class _CupertinoTextFieldSelectionGestureDetectorBuilder extends TextSelectionGe
@override @override
void onSingleTapUp(TapUpDetails details) { void onSingleTapUp(TapUpDetails details) {
editableText.hideToolbar();
// Because TextSelectionGestureDetector listens to taps that happen on // Because TextSelectionGestureDetector listens to taps that happen on
// widgets in front of it, tapping the clear button will also trigger // widgets in front of it, tapping the clear button will also trigger
// this handler. If the clear button widget recognizes the up event, // this handler. If the clear button widget recognizes the up event,
......
...@@ -494,8 +494,6 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive ...@@ -494,8 +494,6 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
}); });
} }
TextSelection? _lastSeenTextSelection;
void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) { void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) {
final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause); final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause);
if (willShowSelectionHandles != _showSelectionHandles) { if (willShowSelectionHandles != _showSelectionHandles) {
...@@ -503,12 +501,10 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive ...@@ -503,12 +501,10 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
_showSelectionHandles = willShowSelectionHandles; _showSelectionHandles = willShowSelectionHandles;
}); });
} }
// TODO(chunhtai): The selection may be the same. We should remove this
// check once this is fixed https://github.com/flutter/flutter/issues/76349. if (widget.onSelectionChanged != null) {
if (widget.onSelectionChanged != null && _lastSeenTextSelection != selection) {
widget.onSelectionChanged!(selection, cause); widget.onSelectionChanged!(selection, cause);
} }
_lastSeenTextSelection = selection;
switch (Theme.of(context).platform) { switch (Theme.of(context).platform) {
case TargetPlatform.iOS: case TargetPlatform.iOS:
......
...@@ -754,60 +754,12 @@ class TextEditingValue { ...@@ -754,60 +754,12 @@ class TextEditingValue {
); );
} }
/// Indicates what triggered the change in selected text (including changes to
/// the cursor location).
enum SelectionChangedCause {
/// The user tapped on the text and that caused the selection (or the location
/// of the cursor) to change.
tap,
/// The user tapped twice in quick succession on the text and that caused
/// the selection (or the location of the cursor) to change.
doubleTap,
/// The user long-pressed the text and that caused the selection (or the
/// location of the cursor) to change.
longPress,
/// The user force-pressed the text and that caused the selection (or the
/// location of the cursor) to change.
forcePress,
/// The user used the keyboard to change the selection or the location of the
/// cursor.
///
/// Keyboard-triggered selection changes may be caused by the IME as well as
/// by accessibility tools (e.g. TalkBack on Android).
keyboard,
/// The user used the selection toolbar to change the selection or the
/// location of the cursor.
///
/// An example is when the user taps on select all in the tool bar.
toolBar,
/// The user used the mouse to change the selection by dragging over a piece
/// of text.
drag,
}
/// An interface for manipulating the selection, to be used by the implementor /// An interface for manipulating the selection, to be used by the implementor
/// of the toolbar widget. /// of the toolbar widget.
abstract class TextSelectionDelegate { abstract class TextSelectionDelegate {
/// Gets the current text input. /// Gets the current text input.
TextEditingValue get textEditingValue; TextEditingValue get textEditingValue;
/// Indicates that the user has requested the delegate to replace its current
/// text editing state with [value].
///
/// The new [value] is treated as user input and thus may subject to input
/// formatting.
@Deprecated(
'Use the userUpdateTextEditingValue instead. '
'This feature was deprecated after v1.26.0-17.2.pre.'
)
set textEditingValue(TextEditingValue value) {}
/// Indicates that the user has requested the delegate to replace its current /// Indicates that the user has requested the delegate to replace its current
/// text editing state with [value]. /// text editing state with [value].
/// ///
...@@ -816,10 +768,10 @@ abstract class TextSelectionDelegate { ...@@ -816,10 +768,10 @@ abstract class TextSelectionDelegate {
/// ///
/// See also: /// See also:
/// ///
/// * [EditableTextState.userUpdateTextEditingValue]: an implementation that /// * [EditableTextState.textEditingValue]: an implementation that applies
/// applies additional pre-processing to the specified [value], before /// additional pre-processing to the specified [value], before updating the
/// updating the text editing state. /// text editing state.
void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause cause); set textEditingValue(TextEditingValue value);
/// Hides the text selection toolbar. /// Hides the text selection toolbar.
void hideToolbar(); void hideToolbar();
......
...@@ -32,7 +32,8 @@ import 'text.dart'; ...@@ -32,7 +32,8 @@ import 'text.dart';
import 'text_selection.dart'; import 'text_selection.dart';
import 'ticker_provider.dart'; import 'ticker_provider.dart';
export 'package:flutter/services.dart' show SelectionChangedCause, TextEditingValue, TextSelection, TextInputType, SmartQuotesType, SmartDashesType; export 'package:flutter/rendering.dart' show SelectionChangedCause;
export 'package:flutter/services.dart' show TextEditingValue, TextSelection, TextInputType, SmartQuotesType, SmartDashesType;
/// Signature for the callback that reports when the user changes the selection /// Signature for the callback that reports when the user changes the selection
/// (including the cursor location). /// (including the cursor location).
...@@ -1479,7 +1480,7 @@ class EditableText extends StatefulWidget { ...@@ -1479,7 +1480,7 @@ class EditableText extends StatefulWidget {
} }
/// State for a [EditableText]. /// State for a [EditableText].
class EditableTextState extends State<EditableText> with AutomaticKeepAliveClientMixin<EditableText>, WidgetsBindingObserver, TickerProviderStateMixin<EditableText>, TextSelectionDelegate implements TextInputClient, AutofillClient { class EditableTextState extends State<EditableText> with AutomaticKeepAliveClientMixin<EditableText>, WidgetsBindingObserver, TickerProviderStateMixin<EditableText> implements TextSelectionDelegate, TextInputClient, AutofillClient {
Timer? _cursorTimer; Timer? _cursorTimer;
bool _targetCursorVisibility = false; bool _targetCursorVisibility = false;
final ValueNotifier<bool> _cursorVisibilityNotifier = ValueNotifier<bool>(true); final ValueNotifier<bool> _cursorVisibilityNotifier = ValueNotifier<bool>(true);
...@@ -1714,7 +1715,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -1714,7 +1715,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
if (value.text == _value.text && value.composing == _value.composing) { if (value.text == _value.text && value.composing == _value.composing) {
// `selection` is the only change. // `selection` is the only change.
_handleSelectionChanged(value.selection, SelectionChangedCause.keyboard); _handleSelectionChanged(value.selection, renderEditable, SelectionChangedCause.keyboard);
} else { } else {
hideToolbar(); hideToolbar();
_currentPromptRectRange = null; _currentPromptRectRange = null;
...@@ -1727,7 +1728,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -1727,7 +1728,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
} }
} }
_formatAndSetValue(value, SelectionChangedCause.keyboard); _formatAndSetValue(value);
} }
if (_hasInputConnection) { if (_hasInputConnection) {
...@@ -1835,7 +1836,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -1835,7 +1836,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
renderEditable.setFloatingCursor(FloatingCursorDragState.End, finalPosition, _lastTextPosition!); renderEditable.setFloatingCursor(FloatingCursorDragState.End, finalPosition, _lastTextPosition!);
if (_lastTextPosition!.offset != renderEditable.selection!.baseOffset) if (_lastTextPosition!.offset != renderEditable.selection!.baseOffset)
// The cause is technically the force cursor, but the cause is listed as tap as the desired functionality is the same. // The cause is technically the force cursor, but the cause is listed as tap as the desired functionality is the same.
_handleSelectionChanged(TextSelection.collapsed(offset: _lastTextPosition!.offset), SelectionChangedCause.forcePress); _handleSelectionChanged(TextSelection.collapsed(offset: _lastTextPosition!.offset), renderEditable, SelectionChangedCause.forcePress);
_startCaretRect = null; _startCaretRect = null;
_lastTextPosition = null; _lastTextPosition = null;
_pointOffsetOrigin = null; _pointOffsetOrigin = null;
...@@ -2101,7 +2102,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2101,7 +2102,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
} }
} }
void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) { void _handleSelectionChanged(TextSelection selection, RenderEditable renderObject, SelectionChangedCause? cause) {
// We return early if the selection is not valid. This can happen when the // 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 // text of [EditableText] is updated at the same time as the selection is
// changed by a gesture event. // changed by a gesture event.
...@@ -2113,11 +2114,11 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2113,11 +2114,11 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// This will show the keyboard for all selection changes on the // This will show the keyboard for all selection changes on the
// EditableWidget, not just changes triggered by user gestures. // EditableWidget, not just changes triggered by user gestures.
requestKeyboard(); requestKeyboard();
if (widget.selectionControls == null) {
_selectionOverlay?.hide(); _selectionOverlay?.hide();
_selectionOverlay = null; _selectionOverlay = null;
} else {
if (_selectionOverlay == null) { if (widget.selectionControls != null) {
_selectionOverlay = TextSelectionOverlay( _selectionOverlay = TextSelectionOverlay(
clipboardStatus: _clipboardStatus, clipboardStatus: _clipboardStatus,
context: context, context: context,
...@@ -2126,21 +2127,14 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2126,21 +2127,14 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
toolbarLayerLink: _toolbarLayerLink, toolbarLayerLink: _toolbarLayerLink,
startHandleLayerLink: _startHandleLayerLink, startHandleLayerLink: _startHandleLayerLink,
endHandleLayerLink: _endHandleLayerLink, endHandleLayerLink: _endHandleLayerLink,
renderObject: renderEditable, renderObject: renderObject,
selectionControls: widget.selectionControls, selectionControls: widget.selectionControls,
selectionDelegate: this, selectionDelegate: this,
dragStartBehavior: widget.dragStartBehavior, dragStartBehavior: widget.dragStartBehavior,
onSelectionHandleTapped: widget.onSelectionHandleTapped, onSelectionHandleTapped: widget.onSelectionHandleTapped,
); );
} else {
_selectionOverlay!.update(_value);
}
_selectionOverlay!.handlesVisible = widget.showSelectionHandles; _selectionOverlay!.handlesVisible = widget.showSelectionHandles;
_selectionOverlay!.showHandles(); _selectionOverlay!.showHandles();
}
// TODO(chunhtai): we should make sure selection actually changed before
// we call the onSelectionChanged.
// https://github.com/flutter/flutter/issues/76349.
try { try {
widget.onSelectionChanged?.call(selection, cause); widget.onSelectionChanged?.call(selection, cause);
} catch (exception, stack) { } catch (exception, stack) {
...@@ -2151,6 +2145,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2151,6 +2145,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
context: ErrorDescription('while calling onSelectionChanged for $cause'), context: ErrorDescription('while calling onSelectionChanged for $cause'),
)); ));
} }
}
// To keep the cursor from blinking while it moves, restart the timer here. // To keep the cursor from blinking while it moves, restart the timer here.
if (_cursorTimer != null) { if (_cursorTimer != null) {
...@@ -2244,7 +2239,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2244,7 +2239,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
late final _WhitespaceDirectionalityFormatter _whitespaceFormatter = _WhitespaceDirectionalityFormatter(textDirection: _textDirection); late final _WhitespaceDirectionalityFormatter _whitespaceFormatter = _WhitespaceDirectionalityFormatter(textDirection: _textDirection);
void _formatAndSetValue(TextEditingValue value, SelectionChangedCause? cause, {bool userInteraction = false}) { void _formatAndSetValue(TextEditingValue value) {
// Only apply input formatters if the text has changed (including uncommited // Only apply input formatters if the text has changed (including uncommited
// text in the composing region), or when the user committed the composing // text in the composing region), or when the user committed the composing
// text. // text.
...@@ -2276,16 +2271,6 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2276,16 +2271,6 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// sending multiple `TextInput.updateEditingValue` messages. // sending multiple `TextInput.updateEditingValue` messages.
beginBatchEdit(); beginBatchEdit();
_value = value; _value = value;
// Changes made by the keyboard can sometimes be "out of band" for listening
// components, so always send those events, even if we didn't think it
// changed. Also, the user long pressing should always send a selection change
// as well.
if (selectionChanged ||
(userInteraction &&
(cause == SelectionChangedCause.longPress ||
cause == SelectionChangedCause.keyboard))) {
_handleSelectionChanged(value.selection, cause);
}
if (textChanged) { if (textChanged) {
try { try {
widget.onChanged?.call(value.text); widget.onChanged?.call(value.text);
...@@ -2299,6 +2284,19 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2299,6 +2284,19 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
} }
} }
if (selectionChanged) {
try {
widget.onSelectionChanged?.call(value.selection, null);
} catch (exception, stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'widgets',
context: ErrorDescription('while calling onSelectionChanged'),
));
}
}
endBatchEdit(); endBatchEdit();
} }
...@@ -2409,7 +2407,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2409,7 +2407,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
_showCaretOnScreen(); _showCaretOnScreen();
if (!_value.selection.isValid) { if (!_value.selection.isValid) {
// Place cursor at the end if the selection is invalid when we receive focus. // Place cursor at the end if the selection is invalid when we receive focus.
_handleSelectionChanged(TextSelection.collapsed(offset: _value.text.length), null); _handleSelectionChanged(TextSelection.collapsed(offset: _value.text.length), renderEditable, null);
} }
} else { } else {
WidgetsBinding.instance!.removeObserver(this); WidgetsBinding.instance!.removeObserver(this);
...@@ -2471,8 +2469,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2471,8 +2469,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
double get _devicePixelRatio => MediaQuery.of(context).devicePixelRatio; double get _devicePixelRatio => MediaQuery.of(context).devicePixelRatio;
@override @override
void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause? cause) { set textEditingValue(TextEditingValue value) {
_formatAndSetValue(value, cause, userInteraction: true); _selectionOverlay?.update(value);
_formatAndSetValue(value);
} }
@override @override
...@@ -2635,6 +2634,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2635,6 +2634,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
smartQuotesType: widget.smartQuotesType, smartQuotesType: widget.smartQuotesType,
enableSuggestions: widget.enableSuggestions, enableSuggestions: widget.enableSuggestions,
offset: offset, offset: offset,
onSelectionChanged: _handleSelectionChanged,
onCaretChanged: _handleCaretChanged, onCaretChanged: _handleCaretChanged,
rendererIgnoresPointer: widget.rendererIgnoresPointer, rendererIgnoresPointer: widget.rendererIgnoresPointer,
cursorWidth: widget.cursorWidth, cursorWidth: widget.cursorWidth,
...@@ -2718,6 +2718,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2718,6 +2718,7 @@ class _Editable extends LeafRenderObjectWidget {
required this.smartQuotesType, required this.smartQuotesType,
required this.enableSuggestions, required this.enableSuggestions,
required this.offset, required this.offset,
this.onSelectionChanged,
this.onCaretChanged, this.onCaretChanged,
this.rendererIgnoresPointer = false, this.rendererIgnoresPointer = false,
required this.cursorWidth, required this.cursorWidth,
...@@ -2765,6 +2766,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2765,6 +2766,7 @@ class _Editable extends LeafRenderObjectWidget {
final SmartQuotesType smartQuotesType; final SmartQuotesType smartQuotesType;
final bool enableSuggestions; final bool enableSuggestions;
final ViewportOffset offset; final ViewportOffset offset;
final SelectionChangedHandler? onSelectionChanged;
final CaretChangedHandler? onCaretChanged; final CaretChangedHandler? onCaretChanged;
final bool rendererIgnoresPointer; final bool rendererIgnoresPointer;
final double cursorWidth; final double cursorWidth;
...@@ -2804,6 +2806,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2804,6 +2806,7 @@ class _Editable extends LeafRenderObjectWidget {
locale: locale ?? Localizations.maybeLocaleOf(context), locale: locale ?? Localizations.maybeLocaleOf(context),
selection: value.selection, selection: value.selection,
offset: offset, offset: offset,
onSelectionChanged: onSelectionChanged,
onCaretChanged: onCaretChanged, onCaretChanged: onCaretChanged,
ignorePointer: rendererIgnoresPointer, ignorePointer: rendererIgnoresPointer,
obscuringCharacter: obscuringCharacter, obscuringCharacter: obscuringCharacter,
...@@ -2848,6 +2851,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2848,6 +2851,7 @@ class _Editable extends LeafRenderObjectWidget {
..locale = locale ?? Localizations.maybeLocaleOf(context) ..locale = locale ?? Localizations.maybeLocaleOf(context)
..selection = value.selection ..selection = value.selection
..offset = offset ..offset = offset
..onSelectionChanged = onSelectionChanged
..onCaretChanged = onCaretChanged ..onCaretChanged = onCaretChanged
..ignorePointer = rendererIgnoresPointer ..ignorePointer = rendererIgnoresPointer
..textHeightBehavior = textHeightBehavior ..textHeightBehavior = textHeightBehavior
......
...@@ -205,15 +205,12 @@ abstract class TextSelectionControls { ...@@ -205,15 +205,12 @@ abstract class TextSelectionControls {
Clipboard.setData(ClipboardData( Clipboard.setData(ClipboardData(
text: value.selection.textInside(value.text), text: value.selection.textInside(value.text),
)); ));
delegate.userUpdateTextEditingValue( delegate.textEditingValue = TextEditingValue(
TextEditingValue(
text: value.selection.textBefore(value.text) text: value.selection.textBefore(value.text)
+ value.selection.textAfter(value.text), + value.selection.textAfter(value.text),
selection: TextSelection.collapsed( selection: TextSelection.collapsed(
offset: value.selection.start offset: value.selection.start
)
), ),
SelectionChangedCause.toolBar,
); );
delegate.bringIntoView(delegate.textEditingValue.selection.extent); delegate.bringIntoView(delegate.textEditingValue.selection.extent);
delegate.hideToolbar(); delegate.hideToolbar();
...@@ -231,12 +228,9 @@ abstract class TextSelectionControls { ...@@ -231,12 +228,9 @@ abstract class TextSelectionControls {
text: value.selection.textInside(value.text), text: value.selection.textInside(value.text),
)); ));
clipboardStatus?.update(); clipboardStatus?.update();
delegate.userUpdateTextEditingValue( delegate.textEditingValue = TextEditingValue(
TextEditingValue(
text: value.text, text: value.text,
selection: TextSelection.collapsed(offset: value.selection.end), selection: TextSelection.collapsed(offset: value.selection.end),
),
SelectionChangedCause.toolBar,
); );
delegate.bringIntoView(delegate.textEditingValue.selection.extent); delegate.bringIntoView(delegate.textEditingValue.selection.extent);
delegate.hideToolbar(); delegate.hideToolbar();
...@@ -257,16 +251,13 @@ abstract class TextSelectionControls { ...@@ -257,16 +251,13 @@ abstract class TextSelectionControls {
final TextEditingValue value = delegate.textEditingValue; // Snapshot the input before using `await`. final TextEditingValue value = delegate.textEditingValue; // Snapshot the input before using `await`.
final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain); final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
if (data != null) { if (data != null) {
delegate.userUpdateTextEditingValue( delegate.textEditingValue = TextEditingValue(
TextEditingValue(
text: value.selection.textBefore(value.text) text: value.selection.textBefore(value.text)
+ data.text! + data.text!
+ value.selection.textAfter(value.text), + value.selection.textAfter(value.text),
selection: TextSelection.collapsed( selection: TextSelection.collapsed(
offset: value.selection.start + data.text!.length offset: value.selection.start + data.text!.length
), ),
),
SelectionChangedCause.toolBar,
); );
} }
delegate.bringIntoView(delegate.textEditingValue.selection.extent); delegate.bringIntoView(delegate.textEditingValue.selection.extent);
...@@ -281,15 +272,12 @@ abstract class TextSelectionControls { ...@@ -281,15 +272,12 @@ abstract class TextSelectionControls {
/// This is called by subclasses when their select-all affordance is activated /// This is called by subclasses when their select-all affordance is activated
/// by the user. /// by the user.
void handleSelectAll(TextSelectionDelegate delegate) { void handleSelectAll(TextSelectionDelegate delegate) {
delegate.userUpdateTextEditingValue( delegate.textEditingValue = TextEditingValue(
TextEditingValue(
text: delegate.textEditingValue.text, text: delegate.textEditingValue.text,
selection: TextSelection( selection: TextSelection(
baseOffset: 0, baseOffset: 0,
extentOffset: delegate.textEditingValue.text.length, extentOffset: delegate.textEditingValue.text.length,
), ),
),
SelectionChangedCause.toolBar,
); );
delegate.bringIntoView(delegate.textEditingValue.selection.extent); delegate.bringIntoView(delegate.textEditingValue.selection.extent);
} }
...@@ -448,16 +436,13 @@ class TextSelectionOverlay { ...@@ -448,16 +436,13 @@ class TextSelectionOverlay {
/// Builds the handles by inserting them into the [context]'s overlay. /// Builds the handles by inserting them into the [context]'s overlay.
void showHandles() { void showHandles() {
if (_handles != null) assert(_handles == null);
return;
_handles = <OverlayEntry>[ _handles = <OverlayEntry>[
OverlayEntry(builder: (BuildContext context) => _buildHandle(context, _TextSelectionHandlePosition.start)), OverlayEntry(builder: (BuildContext context) => _buildHandle(context, _TextSelectionHandlePosition.start)),
OverlayEntry(builder: (BuildContext context) => _buildHandle(context, _TextSelectionHandlePosition.end)), OverlayEntry(builder: (BuildContext context) => _buildHandle(context, _TextSelectionHandlePosition.end)),
]; ];
Overlay.of(context, rootOverlay: true, debugRequiredFor: debugRequiredFor)! Overlay.of(context, rootOverlay: true, debugRequiredFor: debugRequiredFor)!.insertAll(_handles!);
.insertAll(_handles!);
} }
/// Destroys the handles by removing them from overlay. /// Destroys the handles by removing them from overlay.
...@@ -628,13 +613,10 @@ class TextSelectionOverlay { ...@@ -628,13 +613,10 @@ class TextSelectionOverlay {
textPosition = newSelection.base; textPosition = newSelection.base;
break; break;
case _TextSelectionHandlePosition.end: case _TextSelectionHandlePosition.end:
textPosition = newSelection.extent; textPosition =newSelection.extent;
break; break;
} }
selectionDelegate!.userUpdateTextEditingValue( selectionDelegate!.textEditingValue = _value.copyWith(selection: newSelection, composing: TextRange.empty);
_value.copyWith(selection: newSelection, composing: TextRange.empty),
SelectionChangedCause.drag,
);
selectionDelegate!.bringIntoView(textPosition); selectionDelegate!.bringIntoView(textPosition);
} }
} }
......
...@@ -3475,6 +3475,7 @@ void main() { ...@@ -3475,6 +3475,7 @@ void main() {
from: tester.getTopRight(find.byType(CupertinoApp)), from: tester.getTopRight(find.byType(CupertinoApp)),
cause: SelectionChangedCause.tap, cause: SelectionChangedCause.tap,
); );
expect(state.showToolbar(), true);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// -1 because we want to reach the end of the line, not the start of a new line. // -1 because we want to reach the end of the line, not the start of a new line.
...@@ -3535,6 +3536,7 @@ void main() { ...@@ -3535,6 +3536,7 @@ void main() {
from: tester.getCenter(find.byType(EditableText)), from: tester.getCenter(find.byType(EditableText)),
cause: SelectionChangedCause.tap, cause: SelectionChangedCause.tap,
); );
expect(state.showToolbar(), true);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
bottomLeftSelectionPosition = textOffsetToBottomLeftPosition(tester, state.renderEditable.selection!.baseOffset); bottomLeftSelectionPosition = textOffsetToBottomLeftPosition(tester, state.renderEditable.selection!.baseOffset);
......
...@@ -119,8 +119,6 @@ void main() { ...@@ -119,8 +119,6 @@ void main() {
expect(tester.testTextInput.isVisible, isTrue); expect(tester.testTextInput.isVisible, isTrue);
tester.testTextInput.hide(); tester.testTextInput.hide();
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
state.connectionClosed();
expect(tester.testTextInput.isVisible, isFalse); expect(tester.testTextInput.isVisible, isFalse);
......
...@@ -18,8 +18,6 @@ import '../widgets/editable_text_utils.dart' show findRenderEditable, globalize, ...@@ -18,8 +18,6 @@ import '../widgets/editable_text_utils.dart' show findRenderEditable, globalize,
import '../widgets/semantics_tester.dart'; import '../widgets/semantics_tester.dart';
import 'feedback_tester.dart'; import 'feedback_tester.dart';
typedef FormatEditUpdateCallback = void Function(TextEditingValue, TextEditingValue);
class MockClipboard { class MockClipboard {
Object _clipboardData = <String, dynamic>{ Object _clipboardData = <String, dynamic>{
'text': null, 'text': null,
...@@ -129,16 +127,6 @@ double getOpacity(WidgetTester tester, Finder finder) { ...@@ -129,16 +127,6 @@ double getOpacity(WidgetTester tester, Finder finder) {
).opacity.value; ).opacity.value;
} }
class TestFormatter extends TextInputFormatter {
TestFormatter(this.onFormatEditUpdate);
FormatEditUpdateCallback onFormatEditUpdate;
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
onFormatEditUpdate(oldValue, newValue);
return newValue;
}
}
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final MockClipboard mockClipboard = MockClipboard(); final MockClipboard mockClipboard = MockClipboard();
...@@ -486,47 +474,6 @@ void main() { ...@@ -486,47 +474,6 @@ void main() {
); );
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
testWidgets('TextInputFormatter gets correct selection value', (WidgetTester tester) async {
late TextEditingValue actualOldValue;
late TextEditingValue actualNewValue;
final FormatEditUpdateCallback callBack = (TextEditingValue oldValue, TextEditingValue newValue) {
actualOldValue = oldValue;
actualNewValue = newValue;
};
final FocusNode focusNode = FocusNode();
final TextEditingController controller = TextEditingController(text: '123');
await tester.pumpWidget(
boilerplate(
child: TextField(
controller: controller,
focusNode: focusNode,
inputFormatters: <TextInputFormatter>[TestFormatter(callBack)],
),
),
);
await tester.tap(find.byType(TextField));
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
await tester.pumpAndSettle();
expect(
actualOldValue,
const TextEditingValue(
text: '123',
selection: TextSelection.collapsed(offset: 3, affinity: TextAffinity.upstream),
),
);
expect(
actualNewValue,
const TextEditingValue(
text: '12',
selection: TextSelection.collapsed(offset: 2),
),
);
});
testWidgets('text field selection toolbar renders correctly inside opacity', (WidgetTester tester) async { testWidgets('text field selection toolbar renders correctly inside opacity', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
...@@ -1124,9 +1071,11 @@ void main() { ...@@ -1124,9 +1071,11 @@ void main() {
)); ));
expect(find.text('Paste'), findsNothing); expect(find.text('Paste'), findsNothing);
final Offset emptyPos = textOffsetToPosition(tester, 0); final Offset emptyPos = textOffsetToPosition(tester, 0);
await tester.longPressAt(emptyPos, pointer: 7); await tester.longPressAt(emptyPos, pointer: 7);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.text('Paste'), findsOneWidget); expect(find.text('Paste'), findsOneWidget);
}); });
......
...@@ -18,9 +18,6 @@ class FakeEditableTextState with TextSelectionDelegate { ...@@ -18,9 +18,6 @@ class FakeEditableTextState with TextSelectionDelegate {
@override @override
TextEditingValue textEditingValue = TextEditingValue.empty; TextEditingValue textEditingValue = TextEditingValue.empty;
@override
void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause cause) { }
@override @override
void hideToolbar() { } void hideToolbar() { }
......
...@@ -48,7 +48,6 @@ void main() { ...@@ -48,7 +48,6 @@ void main() {
}); });
testWidgets('cursor layout has correct width', (WidgetTester tester) async { testWidgets('cursor layout has correct width', (WidgetTester tester) async {
EditableText.debugDeterministicCursor = true;
final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>(); final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();
late String changedValue; late String changedValue;
...@@ -88,7 +87,8 @@ void main() { ...@@ -88,7 +87,8 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
await tester.tap(find.text('Paste')); await tester.tap(find.text('Paste'));
await tester.pump(); // Wait for cursor to appear.
await tester.pump(const Duration(milliseconds: 600));
expect(changedValue, clipboardContent); expect(changedValue, clipboardContent);
...@@ -96,7 +96,6 @@ void main() { ...@@ -96,7 +96,6 @@ void main() {
find.byKey(const ValueKey<int>(1)), find.byKey(const ValueKey<int>(1)),
matchesGoldenFile('editable_text_test.0.png'), matchesGoldenFile('editable_text_test.0.png'),
); );
EditableText.debugDeterministicCursor = false;
}); });
testWidgets('cursor layout has correct radius', (WidgetTester tester) async { testWidgets('cursor layout has correct radius', (WidgetTester tester) async {
...@@ -788,7 +787,6 @@ void main() { ...@@ -788,7 +787,6 @@ void main() {
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
testWidgets('cursor layout', (WidgetTester tester) async { testWidgets('cursor layout', (WidgetTester tester) async {
EditableText.debugDeterministicCursor = true;
final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>(); final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();
late String changedValue; late String changedValue;
...@@ -833,7 +831,8 @@ void main() { ...@@ -833,7 +831,8 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
await tester.tap(find.text('Paste')); await tester.tap(find.text('Paste'));
await tester.pump(); // Wait for cursor to appear.
await tester.pump(const Duration(milliseconds: 600));
expect(changedValue, clipboardContent); expect(changedValue, clipboardContent);
...@@ -841,11 +840,9 @@ void main() { ...@@ -841,11 +840,9 @@ void main() {
find.byKey(const ValueKey<int>(1)), find.byKey(const ValueKey<int>(1)),
matchesGoldenFile('editable_text_test.2.png'), matchesGoldenFile('editable_text_test.2.png'),
); );
EditableText.debugDeterministicCursor = false;
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
testWidgets('cursor layout has correct height', (WidgetTester tester) async { testWidgets('cursor layout has correct height', (WidgetTester tester) async {
EditableText.debugDeterministicCursor = true;
final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>(); final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();
late String changedValue; late String changedValue;
...@@ -891,7 +888,8 @@ void main() { ...@@ -891,7 +888,8 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
await tester.tap(find.text('Paste')); await tester.tap(find.text('Paste'));
await tester.pump(); // Wait for cursor to appear.
await tester.pump(const Duration(milliseconds: 600));
expect(changedValue, clipboardContent); expect(changedValue, clipboardContent);
...@@ -899,6 +897,5 @@ void main() { ...@@ -899,6 +897,5 @@ void main() {
find.byKey(const ValueKey<int>(1)), find.byKey(const ValueKey<int>(1)),
matchesGoldenFile('editable_text_test.3.png'), matchesGoldenFile('editable_text_test.3.png'),
); );
EditableText.debugDeterministicCursor = false;
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
} }
...@@ -5199,7 +5199,7 @@ void main() { ...@@ -5199,7 +5199,7 @@ void main() {
tester.testTextInput.log.clear(); tester.testTextInput.log.clear();
final EditableTextState state = tester.state<EditableTextState>(find.byWidget(editableText)); final EditableTextState state = tester.state<EditableTextState>(find.byWidget(editableText));
state.userUpdateTextEditingValue(const TextEditingValue(text: 'remoteremoteremote'), SelectionChangedCause.keyboard); state.textEditingValue = const TextEditingValue(text: 'remoteremoteremote');
// Apply in order: length formatter -> listener -> onChanged -> listener. // Apply in order: length formatter -> listener -> onChanged -> listener.
expect(controller.text, 'remote listener onChanged listener'); expect(controller.text, 'remote listener onChanged listener');
...@@ -5355,7 +5355,6 @@ void main() { ...@@ -5355,7 +5355,6 @@ void main() {
'TextInput.setEditingState', 'TextInput.setEditingState',
'TextInput.setEditingState', 'TextInput.setEditingState',
'TextInput.show', 'TextInput.show',
'TextInput.show',
]; ];
expect(tester.testTextInput.log.length, logOrder.length); expect(tester.testTextInput.log.length, logOrder.length);
int index = 0; int index = 0;
...@@ -5470,18 +5469,16 @@ void main() { ...@@ -5470,18 +5469,16 @@ void main() {
log.clear(); log.clear();
final EditableTextState state = tester.firstState(find.byType(EditableText)); final EditableTextState state = tester.firstState(find.byType(EditableText));
// setEditingState is not called when only the remote changes // setEditingState is not called when only the remote changes
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'a', text: 'a',
selection: controller.selection,
)); ));
expect(log.length, 0); expect(log.length, 0);
// setEditingState is called when remote value modified by the formatter. // setEditingState is called when remote value modified by the formatter.
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'I will be modified by the formatter.', text: 'I will be modified by the formatter.',
selection: controller.selection,
)); ));
expect(log.length, 1); expect(log.length, 1);
MethodCall methodCall = log[0]; MethodCall methodCall = log[0];
...@@ -5595,9 +5592,8 @@ void main() { ...@@ -5595,9 +5592,8 @@ void main() {
final EditableTextState state = tester.firstState(find.byType(EditableText)); final EditableTextState state = tester.firstState(find.byType(EditableText));
// setEditingState is called when remote value modified by the formatter. // setEditingState is called when remote value modified by the formatter.
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'I will be modified by the formatter.', text: 'I will be modified by the formatter.',
selection: controller.selection,
)); ));
expect(log.length, 1); expect(log.length, 1);
expect(log, contains(matchesMethodCall( expect(log, contains(matchesMethodCall(
...@@ -5669,9 +5665,8 @@ void main() { ...@@ -5669,9 +5665,8 @@ void main() {
final EditableTextState state = tester.firstState(find.byType(EditableText)); final EditableTextState state = tester.firstState(find.byType(EditableText));
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'a', text: 'a',
selection: controller.selection,
)); ));
await tester.pump(); await tester.pump();
...@@ -5694,9 +5689,8 @@ void main() { ...@@ -5694,9 +5689,8 @@ void main() {
log.clear(); log.clear();
// Send repeat value from the engine. // Send repeat value from the engine.
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'a', text: 'a',
selection: controller.selection,
)); ));
await tester.pump(); await tester.pump();
...@@ -5790,9 +5784,8 @@ void main() { ...@@ -5790,9 +5784,8 @@ void main() {
final EditableTextState state = tester.firstState(find.byType(EditableText)); final EditableTextState state = tester.firstState(find.byType(EditableText));
state.updateEditingValue(TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'a', text: 'a',
selection: controller.selection,
)); ));
await tester.pump(); await tester.pump();
...@@ -6586,7 +6579,6 @@ void main() { ...@@ -6586,7 +6579,6 @@ void main() {
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
controller.selection = const TextSelection.collapsed(offset: 2); controller.selection = const TextSelection.collapsed(offset: 2);
...@@ -6595,7 +6587,6 @@ void main() { ...@@ -6595,7 +6587,6 @@ void main() {
// Reset the composing range. // Reset the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12)); expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12));
...@@ -6603,14 +6594,13 @@ void main() { ...@@ -6603,14 +6594,13 @@ void main() {
// Positioning cursor after the composing range should clear the composing range. // Positioning cursor after the composing range should clear the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
controller.selection = const TextSelection.collapsed(offset: 14); controller.selection = const TextSelection.collapsed(offset: 14);
expect(state.currentTextEditingValue.composing, TextRange.empty); expect(state.currentTextEditingValue.composing, TextRange.empty);
}); });
testWidgets('Clears composing range if cursor moves outside that range - case two', (WidgetTester tester) async { testWidgets('Clears composing range if cursor moves outside that range', (WidgetTester tester) async {
final Widget widget = MaterialApp( final Widget widget = MaterialApp(
home: EditableText( home: EditableText(
backgroundCursorColor: Colors.grey, backgroundCursorColor: Colors.grey,
...@@ -6627,7 +6617,6 @@ void main() { ...@@ -6627,7 +6617,6 @@ void main() {
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
controller.selection = const TextSelection(baseOffset: 1, extentOffset: 2); controller.selection = const TextSelection(baseOffset: 1, extentOffset: 2);
...@@ -6636,7 +6625,6 @@ void main() { ...@@ -6636,7 +6625,6 @@ void main() {
// Reset the composing range. // Reset the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12)); expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12));
...@@ -6644,7 +6632,6 @@ void main() { ...@@ -6644,7 +6632,6 @@ void main() {
// Setting a selection within the composing range clears the composing range. // Setting a selection within the composing range clears the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
controller.selection = const TextSelection(baseOffset: 5, extentOffset: 7); controller.selection = const TextSelection(baseOffset: 5, extentOffset: 7);
...@@ -6653,7 +6640,6 @@ void main() { ...@@ -6653,7 +6640,6 @@ void main() {
// Reset the composing range. // Reset the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12)); expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12));
...@@ -6661,7 +6647,6 @@ void main() { ...@@ -6661,7 +6647,6 @@ void main() {
// Setting a selection after the composing range clears the composing range. // Setting a selection after the composing range clears the composing range.
state.updateEditingValue(const TextEditingValue( state.updateEditingValue(const TextEditingValue(
text: 'foo composing bar', text: 'foo composing bar',
selection: TextSelection.collapsed(offset: 4),
composing: TextRange(start: 4, end: 12), composing: TextRange(start: 4, end: 12),
)); ));
controller.selection = const TextSelection(baseOffset: 13, extentOffset: 15); controller.selection = const TextSelection(baseOffset: 13, extentOffset: 15);
......
...@@ -797,7 +797,6 @@ class FakeRenderEditable extends RenderEditable { ...@@ -797,7 +797,6 @@ class FakeRenderEditable extends RenderEditable {
), ),
startHandleLayerLink: LayerLink(), startHandleLayerLink: LayerLink(),
endHandleLayerLink: LayerLink(), endHandleLayerLink: LayerLink(),
ignorePointer: true,
textAlign: TextAlign.start, textAlign: TextAlign.start,
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
locale: const Locale('en', 'US'), locale: const Locale('en', 'US'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment