Skip to content

Commit 2dc8bb1

Browse files
authored
Make the cursor no longer blinking when move, as same as the effect of iOS platform. (#107221)
1 parent 2327bb7 commit 2dc8bb1

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

packages/flutter/lib/src/widgets/editable_text.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
21842184
_floatingCursorResetController!.stop();
21852185
_onFloatingCursorResetTick();
21862186
}
2187+
// Stop cursor blinking and making it visible.
2188+
_stopCursorBlink(resetCharTicks: false);
2189+
_cursorBlinkOpacityController.value = 1.0;
21872190
// We want to send in points that are centered around a (0,0) origin, so
21882191
// we cache the position.
21892192
_pointOffsetOrigin = point.offset;
@@ -2204,6 +2207,8 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
22042207
renderEditable.setFloatingCursor(point.state, _lastBoundedOffset!, _lastTextPosition!);
22052208
break;
22062209
case FloatingCursorDragState.End:
2210+
// Resume cursor blinking.
2211+
_startCursorBlink();
22072212
// We skip animation if no update has happened.
22082213
if (_lastTextPosition != null && _lastBoundedOffset != null) {
22092214
_floatingCursorResetController!.value = 0.0;

packages/flutter/test/widgets/editable_text_cursor_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,73 @@ void main() {
701701
expect(tester.takeException(), null);
702702
});
703703

704+
testWidgets("Drag the floating cursor, it won't blink.", (WidgetTester tester) async {
705+
const String text = 'hello world this is fun and cool and awesome!';
706+
controller.text = text;
707+
final FocusNode focusNode = FocusNode();
708+
709+
await tester.pumpWidget(
710+
MediaQuery(
711+
data: const MediaQueryData(),
712+
child: Directionality(
713+
textDirection: TextDirection.ltr,
714+
child: FocusScope(
715+
node: focusScopeNode,
716+
autofocus: true,
717+
child: EditableText(
718+
backgroundCursorColor: Colors.grey,
719+
controller: controller,
720+
focusNode: focusNode,
721+
style: textStyle,
722+
cursorColor: cursorColor,
723+
),
724+
),
725+
),
726+
),
727+
);
728+
729+
final EditableTextState editableText = tester.state(find.byType(EditableText));
730+
731+
// Check that the cursor visibility toggles after each blink interval.
732+
// Or if it's not blinking at all, it stays on.
733+
Future<void> checkCursorBlinking({ bool isBlinking = true }) async {
734+
bool initialShowCursor = true;
735+
if (isBlinking) {
736+
initialShowCursor = editableText.cursorCurrentlyVisible;
737+
}
738+
await tester.pump(editableText.cursorBlinkInterval);
739+
expect(editableText.cursorCurrentlyVisible, equals(isBlinking ? !initialShowCursor : initialShowCursor));
740+
await tester.pump(editableText.cursorBlinkInterval);
741+
expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
742+
await tester.pump(editableText.cursorBlinkInterval ~/ 10);
743+
expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
744+
await tester.pump(editableText.cursorBlinkInterval);
745+
expect(editableText.cursorCurrentlyVisible, equals(isBlinking ? !initialShowCursor : initialShowCursor));
746+
await tester.pump(editableText.cursorBlinkInterval);
747+
expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
748+
}
749+
750+
final Offset textfieldStart = tester.getTopLeft(find.byType(EditableText));
751+
752+
await tester.tapAt(textfieldStart + const Offset(50.0, 9.0));
753+
await tester.pumpAndSettle();
754+
755+
// Before dragging, the cursor should blink.
756+
await checkCursorBlinking();
757+
758+
final EditableTextState editableTextState = tester.firstState(find.byType(EditableText));
759+
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start));
760+
761+
// When drag cursor, the cursor shouldn't blink.
762+
await checkCursorBlinking(isBlinking: false);
763+
764+
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End));
765+
await tester.pumpAndSettle();
766+
767+
// After dragging, the cursor should blink.
768+
await checkCursorBlinking();
769+
});
770+
704771
// Regression test for https://github.com/flutter/flutter/pull/30475.
705772
testWidgets('Trying to select with the floating cursor does not crash', (WidgetTester tester) async {
706773
const String text = 'hello world this is fun and cool and awesome!';

0 commit comments

Comments
 (0)