From 1cb6b8bb0deaf11c50e3d98a60c2d91f51de60c1 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 22 Jul 2019 13:20:21 -0700 Subject: [PATCH] fixes iphone force press keybaord select crashes (#36698) --- .../lib/src/widgets/editable_text.dart | 7 ++- .../widgets/editable_text_cursor_test.dart | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index c821bb2a6e..27c1b91170 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1145,8 +1145,11 @@ class EditableTextState extends State with AutomaticKeepAliveClien } break; case FloatingCursorDragState.End: - _floatingCursorResetController.value = 0.0; - _floatingCursorResetController.animateTo(1.0, duration: _floatingCursorResetTime, curve: Curves.decelerate); + // We skip animation if no update has happened. + if (_lastTextPosition != null && _lastBoundedOffset != null) { + _floatingCursorResetController.value = 0.0; + _floatingCursorResetController.animateTo(1.0, duration: _floatingCursorResetTime, curve: Curves.decelerate); + } break; } } diff --git a/packages/flutter/test/widgets/editable_text_cursor_test.dart b/packages/flutter/test/widgets/editable_text_cursor_test.dart index 08c2d0fa45..23292da0a1 100644 --- a/packages/flutter/test/widgets/editable_text_cursor_test.dart +++ b/packages/flutter/test/widgets/editable_text_cursor_test.dart @@ -509,6 +509,50 @@ void main() { expect(controller.selection.baseOffset, 10); }, skip: isBrowser); + testWidgets('Updating the floating cursor can end without update', (WidgetTester tester) async { + const String text = 'hello world this is fun and cool and awesome!'; + controller.text = text; + final FocusNode focusNode = FocusNode(); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + ); + + await tester.tap(find.byType(EditableText)); + final RenderEditable renderEditable = findRenderEditable(tester); + renderEditable.selection = const TextSelection(baseOffset: 29, extentOffset: 29); + + expect(controller.selection.baseOffset, 29); + + final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); + editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start)); + + expect(controller.selection.baseOffset, 29); + + editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End)); + + await tester.pumpAndSettle(); + // The cursor did not change. + expect(controller.selection.baseOffset, 29); + expect(tester.takeException(), null); + }, skip: isBrowser); + // Regression test for https://github.com/flutter/flutter/pull/30475. testWidgets('Trying to select with the floating cursor does not crash', (WidgetTester tester) async { const String text = 'hello world this is fun and cool and awesome!';