Skip to content

Commit

Permalink
Make the cursor no longer blinking when move, as same as the effect o…
Browse files Browse the repository at this point in the history
…f iOS platform. (#107221)
  • Loading branch information
talisk authored Jul 19, 2022
1 parent 2327bb7 commit 2dc8bb1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,9 @@ class EditableTextState extends State<EditableText> 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;
Expand All @@ -2204,6 +2207,8 @@ class EditableTextState extends State<EditableText> 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;
Expand Down
67 changes: 67 additions & 0 deletions packages/flutter/test/widgets/editable_text_cursor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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!';
Expand Down

0 comments on commit 2dc8bb1

Please sign in to comment.