diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4136c2e7a5..f5287bf5bb 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2219,7 +2219,10 @@ class EditableTextState extends State with AutomaticKeepAliveClien @override bool get wantKeepAlive => widget.focusNode.hasFocus; - Color get _cursorColor => widget.cursorColor.withOpacity(_cursorBlinkOpacityController.value); + Color get _cursorColor { + final double effectiveOpacity = math.min(widget.cursorColor.alpha / 255.0, _cursorBlinkOpacityController.value); + return widget.cursorColor.withOpacity(effectiveOpacity); + } @override bool get cutEnabled { @@ -3873,7 +3876,8 @@ class EditableTextState extends State with AutomaticKeepAliveClien } void _onCursorColorTick() { - renderEditable.cursorColor = widget.cursorColor.withOpacity(_cursorBlinkOpacityController.value); + final double effectiveOpacity = math.min(widget.cursorColor.alpha / 255.0, _cursorBlinkOpacityController.value); + renderEditable.cursorColor = widget.cursorColor.withOpacity(effectiveOpacity); _cursorVisibilityNotifier.value = widget.showCursor && _cursorBlinkOpacityController.value > 0; } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 776793e993..4efaf4992c 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -16657,6 +16657,30 @@ testWidgets('Floating cursor ending with selection', (WidgetTester tester) async expect(calls, equals(kIsWeb ? 0 : 2)); }); }); + + testWidgets('Cursor color with an opacity is respected', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + const double opacity = 0.55; + await tester.pumpWidget( + MaterialApp( + home: EditableText( + key: key, + cursorColor: cursorColor.withOpacity(opacity), + backgroundCursorColor: Colors.grey, + controller: TextEditingController(text: 'blah blah'), + focusNode: focusNode, + style: textStyle, + ), + ), + ); + + // Tap to show the cursor. + await tester.tap(find.byKey(key)); + await tester.pumpAndSettle(); + + final EditableTextState state = tester.state(find.byType(EditableText)); + expect(state.renderEditable.cursorColor, cursorColor.withOpacity(opacity)); + }); } class UnsettableController extends TextEditingController {