From c58e31a74f949b5767c77a950931d90700abbd2a Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Mon, 11 Feb 2019 12:54:21 -0800 Subject: [PATCH] Cupertino TextField Cursor Fix (#27697) * Fix for #26261. Changes CupertinoTextField's cursorColor to read from CupertinoTheme instead of prior default of activeBlue. CursorColor will still default to activeBlue for light theme and activeOrange for dark theme if a primary color has not been specified for the CupertinoTheme. * Reverted unnecessary changes in XCode file. * Updated text_field.dart per suggestions from @gspencergoog * Updated comments for cursorColor to reflect appropriate hyperlinks per @Hixie * Simplified cursorColor assignment per @xster * Added test in cupertino/text_field_test.dart to check for correct cursorColor based on CupertinoTheme per @Hixie & @xster. --- .../flutter/lib/src/cupertino/text_field.dart | 9 ++-- .../test/cupertino/text_field_test.dart | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index d7237ef307..4e1f5d9a75 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -172,7 +172,7 @@ class CupertinoTextField extends StatefulWidget { this.enabled, this.cursorWidth = 2.0, this.cursorRadius = const Radius.circular(2.0), - this.cursorColor = CupertinoColors.activeBlue, + this.cursorColor, this.keyboardAppearance, this.scrollPadding = const EdgeInsets.all(20.0), }) : assert(textAlign != null), @@ -365,7 +365,9 @@ class CupertinoTextField extends StatefulWidget { /// The color to use when painting the cursor. /// - /// Defaults to the standard iOS blue color. Cannot be null. + /// Defaults to the [CupertinoThemeData.primaryColor] of the ambient theme, + /// which itself defaults to [CupertinoColors.activeBlue] in the light theme + /// and [CupertinoColors.activeOrange] in the dark theme. final Color cursorColor; /// The appearance of the keyboard. @@ -401,6 +403,7 @@ class CupertinoTextField extends StatefulWidget { properties.add(IntProperty('maxLines', maxLines, defaultValue: 1)); properties.add(IntProperty('maxLength', maxLength, defaultValue: null)); properties.add(FlagProperty('maxLengthEnforced', value: maxLengthEnforced, ifTrue: 'max length enforced')); + properties.add(DiagnosticsProperty('cursorColor', cursorColor, defaultValue: null)); } } @@ -644,7 +647,7 @@ class _CupertinoTextFieldState extends State with AutomaticK rendererIgnoresPointer: true, cursorWidth: widget.cursorWidth, cursorRadius: widget.cursorRadius, - cursorColor: widget.cursorColor, + cursorColor: themeData.primaryColor, cursorOpacityAnimates: true, cursorOffset: cursorOffset, paintCursorAboveText: true, diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index e9c866dd5d..db12b4cfb8 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -1345,4 +1345,46 @@ void main() { expect(setClient.method, 'TextInput.setClient'); expect(setClient.arguments.last['keyboardAppearance'], 'Brightness.light'); }); + + testWidgets('cursorColor respects theme', (WidgetTester tester) async { + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoTextField(), + ), + ); + + final Finder textFinder = find.byType(CupertinoTextField); + await tester.tap(textFinder); + await tester.pump(); + + final EditableTextState editableTextState = + tester.firstState(find.byType(EditableText)); + final RenderEditable renderEditable = editableTextState.renderEditable; + + expect(renderEditable.cursorColor, CupertinoColors.activeBlue); + + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoTextField(), + theme: CupertinoThemeData( + brightness: Brightness.dark, + ), + ), + ); + + await tester.pump(); + expect(renderEditable.cursorColor, CupertinoColors.activeOrange); + + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoTextField(), + theme: CupertinoThemeData( + primaryColor: Color(0xFFF44336), + ), + ), + ); + + await tester.pump(); + expect(renderEditable.cursorColor, const Color(0xFFF44336)); + }); }