diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4e5474a9b9..e452f2e638 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -4823,6 +4823,13 @@ class EditableTextState extends State with AutomaticKeepAliveClien return oldValue.text != newValue.text || oldValue.composing != newValue.composing; }, + undoStackModifier: (TextEditingValue value) { + // On Android we should discard the composing region when pushing + // a new entry to the undo stack. This prevents the TextInputPlugin + // from restarting the input on every undo/redo when the composing + // region is changed by the framework. + return defaultTargetPlatform == TargetPlatform.android ? value.copyWith(composing: TextRange.empty) : value; + }, focusNode: widget.focusNode, controller: widget.undoController, child: Focus( diff --git a/packages/flutter/lib/src/widgets/undo_history.dart b/packages/flutter/lib/src/widgets/undo_history.dart index 48b07d9eb6..ab37737cfc 100644 --- a/packages/flutter/lib/src/widgets/undo_history.dart +++ b/packages/flutter/lib/src/widgets/undo_history.dart @@ -32,6 +32,7 @@ class UndoHistory extends StatefulWidget { required this.value, required this.onTriggered, required this.focusNode, + this.undoStackModifier, this.controller, required this.child, }); @@ -43,6 +44,14 @@ class UndoHistory extends StatefulWidget { /// the undo stack. final bool Function(T? oldValue, T newValue)? shouldChangeUndoStack; + /// Called right before a new entry is pushed to the undo stack. + /// + /// The value returned from this method will be pushed to the stack instead + /// of the original value. + /// + /// If null then the original value will always be pushed to the stack. + final T Function(T value)? undoStackModifier; + /// Called when an undo or redo causes a state change. /// /// If the state would still be the same before and after the undo/redo, this @@ -178,9 +187,14 @@ class UndoHistoryState extends State> with UndoManagerClient { return; } - _lastValue = widget.value.value; + final T nextValue = widget.undoStackModifier?.call(widget.value.value) ?? widget.value.value; + if (nextValue == _lastValue) { + return; + } - _throttleTimer = _throttledPush(widget.value.value); + _lastValue = nextValue; + + _throttleTimer = _throttledPush(nextValue); } void _handleFocus() { diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index f601265f6b..4b7e209c82 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -13127,6 +13127,13 @@ void main() { Future sendUndo(WidgetTester tester) => sendUndoRedo(tester); Future sendRedo(WidgetTester tester) => sendUndoRedo(tester, true); + TextEditingValue emptyComposingOnAndroid(TextEditingValue value) { + if (defaultTargetPlatform == TargetPlatform.android) { + return value.copyWith(composing: TextRange.empty); + } + return value; + } + Widget boilerplate() { return MaterialApp( home: EditableText( @@ -13330,14 +13337,14 @@ void main() { // Undo first insertion. await sendUndo(tester); - expect(controller.value, composingStep2); + expect(controller.value, emptyComposingOnAndroid(composingStep2)); // Waiting for the throttling between undos should have no effect. await tester.pump(const Duration(milliseconds: 500)); // Undo second insertion. await sendUndo(tester); - expect(controller.value, composingStep1); + expect(controller.value, emptyComposingOnAndroid(composingStep1)); // On web, these keyboard shortcuts are handled by the browser. }, variant: TargetPlatformVariant.only(TargetPlatform.android), skip: kIsWeb); // [intended] @@ -13899,7 +13906,6 @@ void main() { controller.value, const TextEditingValue( text: '1 nihao', - composing: TextRange(start: 2, end: 7), selection: TextSelection.collapsed(offset: 7), ), ); @@ -13909,7 +13915,6 @@ void main() { controller.value, const TextEditingValue( text: '1 ni', - composing: TextRange(start: 2, end: 4), selection: TextSelection.collapsed(offset: 4), ), ); @@ -13927,7 +13932,6 @@ void main() { controller.value, const TextEditingValue( text: '1 ni', - composing: TextRange(start: 2, end: 4), selection: TextSelection.collapsed(offset: 4), ), ); @@ -13936,7 +13940,6 @@ void main() { controller.value, const TextEditingValue( text: '1 nihao', - composing: TextRange(start: 2, end: 7), selection: TextSelection.collapsed(offset: 7), ), ); @@ -13962,7 +13965,6 @@ void main() { controller.value, const TextEditingValue( text: '1 nihao', - composing: TextRange(start: 2, end: 7), selection: TextSelection.collapsed(offset: 7), ), ); @@ -13971,7 +13973,6 @@ void main() { controller.value, const TextEditingValue( text: '1 ni', - composing: TextRange(start: 2, end: 4), selection: TextSelection.collapsed(offset: 4), ), ); @@ -14011,7 +14012,6 @@ void main() { controller.value, const TextEditingValue( text: '1 ni', - composing: TextRange(start: 2, end: 4), selection: TextSelection.collapsed(offset: 4), ), ); @@ -14020,7 +14020,6 @@ void main() { controller.value, const TextEditingValue( text: '1 nihao', - composing: TextRange(start: 2, end: 7), selection: TextSelection.collapsed(offset: 7), ), ); @@ -14138,10 +14137,12 @@ void main() { case TargetPlatform.android: expect( controller.value, - const TextEditingValue( - text: '1 2 ni', - composing: TextRange(start: 4, end: 6), - selection: TextSelection.collapsed(offset: 6), + emptyComposingOnAndroid( + const TextEditingValue( + text: '1 2 ni', + composing: TextRange(start: 4, end: 6), + selection: TextSelection.collapsed(offset: 6), + ), ), ); // Composing changes are ignored on all other platforms. @@ -14195,10 +14196,12 @@ void main() { case TargetPlatform.android: expect( controller.value, - const TextEditingValue( - text: '1 2 ni', - composing: TextRange(start: 4, end: 6), - selection: TextSelection.collapsed(offset: 6), + emptyComposingOnAndroid( + const TextEditingValue( + text: '1 2 ni', + composing: TextRange(start: 4, end: 6), + selection: TextSelection.collapsed(offset: 6), + ), ), ); // Composing changes are ignored on all other platforms.