From ea343ccc3ff1baaae2affb8c40c33d25a9ed1587 Mon Sep 17 00:00:00 2001 From: Nizarius Date: Thu, 28 Mar 2019 03:25:29 +0300 Subject: [PATCH] CupertinoTextField: added ability to change placeholder color (#28001) --- .../flutter/lib/src/cupertino/text_field.dart | 29 +++++++++++----- .../test/cupertino/text_field_test.dart | 34 +++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 2badd43762..ab51638b01 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -152,6 +152,10 @@ class CupertinoTextField extends StatefulWidget { this.decoration = _kDefaultRoundedBorderDecoration, this.padding = const EdgeInsets.all(6.0), this.placeholder, + this.placeholderStyle = const TextStyle( + fontWeight: FontWeight.w300, + color: _kInactiveTextColor + ), this.prefix, this.prefixMode = OverlayVisibilityMode.always, this.suffix, @@ -238,6 +242,17 @@ class CupertinoTextField extends StatefulWidget { /// main text entry except a lighter font weight and a grey font color. final String placeholder; + /// The style to use for the placeholder text. + /// + /// The [placeholderStyle] is merged with the [style] [TextStyle] when applied + /// to the [placeholder] text. To avoid merging with [style], specify + /// [TextStyle.inherit] as false. + /// + /// Defaults to the [style] property with w300 font weight and grey color. + /// + /// If specifically set to null, placeholder's style will be the same as [style]. + final TextStyle placeholderStyle; + /// An optional [Widget] to display before the text. final Widget prefix; @@ -422,6 +437,7 @@ class CupertinoTextField extends StatefulWidget { properties.add(DiagnosticsProperty('decoration', decoration)); properties.add(DiagnosticsProperty('padding', padding)); properties.add(StringProperty('placeholder', placeholder)); + properties.add(DiagnosticsProperty('placeholderStyle', placeholderStyle)); properties.add(DiagnosticsProperty('prefix', prefix == null ? null : prefixMode)); properties.add(DiagnosticsProperty('suffix', suffix == null ? null : suffixMode)); properties.add(DiagnosticsProperty('clearButtonMode', clearButtonMode)); @@ -603,9 +619,10 @@ class _CupertinoTextFieldState extends State with AutomaticK ); } - Widget _addTextDependentAttachments(Widget editableText, TextStyle textStyle) { + Widget _addTextDependentAttachments(Widget editableText, TextStyle textStyle, TextStyle placeholderStyle) { assert(editableText != null); assert(textStyle != null); + assert(placeholderStyle != null); // If there are no surrounding widgets, just return the core editable text // part. if (widget.placeholder == null && @@ -640,12 +657,7 @@ class _CupertinoTextFieldState extends State with AutomaticK widget.placeholder, maxLines: 1, overflow: TextOverflow.ellipsis, - style: textStyle.merge( - const TextStyle( - color: _kInactiveTextColor, - fontWeight: FontWeight.w300, - ), - ), + style: placeholderStyle ), ), ); @@ -698,6 +710,7 @@ class _CupertinoTextFieldState extends State with AutomaticK } final CupertinoThemeData themeData = CupertinoTheme.of(context); final TextStyle textStyle = themeData.textTheme.textStyle.merge(widget.style); + final TextStyle placeholderStyle = textStyle.merge(widget.placeholderStyle); final Brightness keyboardAppearance = widget.keyboardAppearance ?? themeData.brightness; final Color cursorColor = widget.cursorColor ?? themeData.primaryColor; @@ -773,7 +786,7 @@ class _CupertinoTextFieldState extends State with AutomaticK onDragSelectionUpdate: _handleMouseDragSelectionUpdate, onDragSelectionEnd: _handleMouseDragSelectionEnd, behavior: HitTestBehavior.translucent, - child: _addTextDependentAttachments(paddedEditable, textStyle), + child: _addTextDependentAttachments(paddedEditable, textStyle, placeholderStyle), ), ), ), diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index fc20441a30..5f427db91b 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -399,6 +399,40 @@ void main() { }, ); + testWidgets( + "placeholderStyle modifies placeholder's style and doesn't affect text's style", + (WidgetTester tester) async { + await tester.pumpWidget( + const CupertinoApp( + home: Center( + child: CupertinoTextField( + placeholder: 'placeholder', + style: TextStyle( + color: Color(0X00FFFFFF), + fontWeight: FontWeight.w300, + ), + placeholderStyle: TextStyle( + color: Color(0XAAFFFFFF), + fontWeight: FontWeight.w600 + ), + ), + ), + ), + ); + + final Text placeholder = tester.widget(find.text('placeholder')); + expect(placeholder.style.color, const Color(0XAAFFFFFF)); + expect(placeholder.style.fontWeight, FontWeight.w600); + + await tester.enterText(find.byType(CupertinoTextField), 'input'); + await tester.pump(); + + final EditableText inputText = tester.widget(find.text('input')); + expect(inputText.style.color, const Color(0X00FFFFFF)); + expect(inputText.style.fontWeight, FontWeight.w300); + }, + ); + testWidgets( 'prefix widget is in front of the text', (WidgetTester tester) async {