From 2dc8bb18e87a20a21e68a3b3094b744da1fb63bb Mon Sep 17 00:00:00 2001 From: Swain Date: Wed, 20 Jul 2022 00:27:04 +0800 Subject: [PATCH] Make the cursor no longer blinking when move, as same as the effect of iOS platform. (#107221) --- .../lib/src/widgets/editable_text.dart | 5 ++ .../widgets/editable_text_cursor_test.dart | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 7976567c25..37801f2765 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2184,6 +2184,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien _floatingCursorResetController!.stop(); _onFloatingCursorResetTick(); } + // Stop cursor blinking and making it visible. + _stopCursorBlink(resetCharTicks: false); + _cursorBlinkOpacityController.value = 1.0; // We want to send in points that are centered around a (0,0) origin, so // we cache the position. _pointOffsetOrigin = point.offset; @@ -2204,6 +2207,8 @@ class EditableTextState extends State with AutomaticKeepAliveClien renderEditable.setFloatingCursor(point.state, _lastBoundedOffset!, _lastTextPosition!); break; case FloatingCursorDragState.End: + // Resume cursor blinking. + _startCursorBlink(); // We skip animation if no update has happened. if (_lastTextPosition != null && _lastBoundedOffset != null) { _floatingCursorResetController!.value = 0.0; diff --git a/packages/flutter/test/widgets/editable_text_cursor_test.dart b/packages/flutter/test/widgets/editable_text_cursor_test.dart index 39388358b0..1708f82a86 100644 --- a/packages/flutter/test/widgets/editable_text_cursor_test.dart +++ b/packages/flutter/test/widgets/editable_text_cursor_test.dart @@ -701,6 +701,73 @@ void main() { expect(tester.takeException(), null); }); + testWidgets("Drag the floating cursor, it won't blink.", (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(), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + ); + + final EditableTextState editableText = tester.state(find.byType(EditableText)); + + // Check that the cursor visibility toggles after each blink interval. + // Or if it's not blinking at all, it stays on. + Future checkCursorBlinking({ bool isBlinking = true }) async { + bool initialShowCursor = true; + if (isBlinking) { + initialShowCursor = editableText.cursorCurrentlyVisible; + } + await tester.pump(editableText.cursorBlinkInterval); + expect(editableText.cursorCurrentlyVisible, equals(isBlinking ? !initialShowCursor : initialShowCursor)); + await tester.pump(editableText.cursorBlinkInterval); + expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor)); + await tester.pump(editableText.cursorBlinkInterval ~/ 10); + expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor)); + await tester.pump(editableText.cursorBlinkInterval); + expect(editableText.cursorCurrentlyVisible, equals(isBlinking ? !initialShowCursor : initialShowCursor)); + await tester.pump(editableText.cursorBlinkInterval); + expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor)); + } + + final Offset textfieldStart = tester.getTopLeft(find.byType(EditableText)); + + await tester.tapAt(textfieldStart + const Offset(50.0, 9.0)); + await tester.pumpAndSettle(); + + // Before dragging, the cursor should blink. + await checkCursorBlinking(); + + final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); + editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start)); + + // When drag cursor, the cursor shouldn't blink. + await checkCursorBlinking(isBlinking: false); + + editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End)); + await tester.pumpAndSettle(); + + // After dragging, the cursor should blink. + await checkCursorBlinking(); + }); + // 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!';