From f6e77a9ee59521fc58a1b321d67b155446f1339e Mon Sep 17 00:00:00 2001 From: Nurhan Turgut <50856934+nturgut@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:25:39 -0700 Subject: [PATCH] Fixing a text editing bug happening when text field changes. (#41108) * Carrying last size and transform information to TextInputConnection. Doing this we are making sure that the size/transform information from the previous connection will be removed, when connection changes. * remove unused lastsize and lasttransform values * Adding unit tests. Adressing comments. --- .../flutter/lib/src/services/text_input.dart | 27 +++--- .../lib/src/widgets/editable_text.dart | 9 +- .../test/widgets/editable_text_test.dart | 82 +++++++++++++++++++ 3 files changed, 100 insertions(+), 18 deletions(-) diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 2d79171e78..d2158a1ab6 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -642,8 +642,11 @@ abstract class TextInputClient { /// * [TextInput.attach] class TextInputConnection { TextInputConnection._(this._client) - : assert(_client != null), - _id = _nextId++; + : assert(_client != null), + _id = _nextId++; + + Size _cachedSize; + Matrix4 _cachedTransform; static int _nextId = 1; final int _id; @@ -678,14 +681,18 @@ class TextInputConnection { /// 2. [transform]: a matrix that maps the local paint coordinate system /// to the [PipelineOwner.rootNode]. void setEditableSizeAndTransform(Size editableBoxSize, Matrix4 transform) { - SystemChannels.textInput.invokeMethod( - 'TextInput.setEditableSizeAndTransform', - { - 'width': editableBoxSize.width, - 'height': editableBoxSize.height, - 'transform': transform.storage, - }, - ); + if (editableBoxSize != _cachedSize || transform != _cachedTransform) { + _cachedSize = editableBoxSize; + _cachedTransform = transform; + SystemChannels.textInput.invokeMethod( + 'TextInput.setEditableSizeAndTransform', + { + 'width': editableBoxSize.width, + 'height': editableBoxSize.height, + 'transform': transform.storage, + }, + ); + } } /// Send text styling information. diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 7181991509..fc37441b09 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1703,18 +1703,11 @@ class EditableTextState extends State with AutomaticKeepAliveClien updateKeepAlive(); } - Size _lastSize; - Matrix4 _lastTransform; - void _updateSizeAndTransform() { if (_hasInputConnection) { final Size size = renderEditable.size; final Matrix4 transform = renderEditable.getTransformTo(null); - if (size != _lastSize || transform != _lastTransform) { - _lastSize = size; - _lastTransform = transform; - _textInputConnection.setEditableSizeAndTransform(size, transform); - } + _textInputConnection.setEditableSizeAndTransform(size, transform); SchedulerBinding.instance .addPostFrameCallback((Duration _) => _updateSizeAndTransform()); } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 4a5e9fa6d2..63260ecda7 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -2215,6 +2215,88 @@ void main() { ); }); + testWidgets('transform and size is reset when text connection opens', (WidgetTester tester) async { + final List log = []; + SystemChannels.textInput.setMockMethodCallHandler((MethodCall methodCall) async { + log.add(methodCall); + }); + + final TextEditingController controller1 = TextEditingController(); + final TextEditingController controller2 = TextEditingController(); + controller1.text = 'Text1'; + controller2.text = 'Text2'; + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + devicePixelRatio: 1.0 + ), + child: Directionality( + textDirection: TextDirection.ltr, + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + EditableText( + key: ValueKey(controller1.text), + controller: controller1, + focusNode: FocusNode(), + style: Typography(platform: TargetPlatform.android).black.subhead, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + ), + const SizedBox(height: 200.0), + EditableText( + key: ValueKey(controller2.text), + controller: controller2, + focusNode: FocusNode(), + style: Typography(platform: TargetPlatform.android).black.subhead, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + ), + const SizedBox(height: 100.0), + ], + ), + ), + ), + ); + + await tester.showKeyboard(find.byKey(ValueKey(controller1.text))); + final MethodCall methodCall = log.firstWhere((MethodCall m) => m.method == 'TextInput.setEditableSizeAndTransform'); + expect( + methodCall, + isMethodCall('TextInput.setEditableSizeAndTransform', arguments: { + 'width': 800, + 'height': 14, + 'transform': Matrix4.identity().storage.toList(), + }), + ); + + // Move to the next editable text. + await tester.showKeyboard(find.byKey(ValueKey(controller2.text))); + final MethodCall methodCall2 = log.firstWhere((MethodCall m) => m.method == 'TextInput.setEditableSizeAndTransform'); + expect( + methodCall2, + isMethodCall('TextInput.setEditableSizeAndTransform', arguments: { + 'width': 800, + 'height': 14, + 'transform': Matrix4.identity().storage.toList(), + }), + ); + + // Move back to the first editable text. + await tester.showKeyboard(find.byKey(ValueKey(controller1.text))); + final MethodCall methodCall3 = log.firstWhere((MethodCall m) => m.method == 'TextInput.setEditableSizeAndTransform'); + expect( + methodCall3, + isMethodCall('TextInput.setEditableSizeAndTransform', arguments: { + 'width': 800, + 'height': 14, + 'transform': Matrix4.identity().storage.toList(), + }), + ); + }); + testWidgets('size and transform are sent when they change', (WidgetTester tester) async { final List log = []; SystemChannels.textInput.setMockMethodCallHandler((MethodCall methodCall) async {