From 1c77696615558bf8f9fa7a6e2a473f438f1c03ca Mon Sep 17 00:00:00 2001 From: Victor Sanni Date: Fri, 14 Jun 2024 11:57:23 -0700 Subject: [PATCH] Make `CupertinoTextField` respect decoration color when disabled (#149774) #83531 contains complaints that this default disabled color can no longer be overridden by setting a color of the `decoration` property. This regression was introduced by #78058, which was itself made to fix an issue where the color of the decoration was always used even when disabled. Currently, when the text field is disabled, its color is set to a default disabled color. This PR makes it so that if a decoration is set when the text field is disabled, its color is used instead of the default disabled color. Fixes https://github.com/flutter/flutter/issues/83531 --- .../flutter/lib/src/cupertino/text_field.dart | 14 +++++++--- .../test/cupertino/text_field_test.dart | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 65661e158b..ec4d1694ce 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -1338,10 +1338,16 @@ class _CupertinoTextFieldState extends State with Restoratio ); } - final BoxDecoration? effectiveDecoration = widget.decoration?.copyWith( - border: resolvedBorder, - color: enabled ? decorationColor : disabledColor, - ); + // Use the default disabled color only if the box decoration was not set. + final BoxDecoration? effectiveDecoration = + widget.decoration?.copyWith( + border: resolvedBorder, + color: enabled ? decorationColor + : (widget.decoration == _kDefaultRoundedBorderDecoration + ? disabledColor + : widget.decoration?.color + ), + ); final Color selectionColor = CupertinoDynamicColor.maybeResolve( DefaultSelectionStyle.of(context).selectionColor, diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index 7407eccea1..9a1f1e9089 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -8397,6 +8397,34 @@ void main() { ); }); + testWidgets('Disabled widget does not override background color', (WidgetTester tester) async { + const Color backgroundColor = Color(0x0000000A); + await tester.pumpWidget( + const CupertinoApp( + home: Center( + child: CupertinoTextField( + enabled: false, + decoration: BoxDecoration(color: backgroundColor), + ), + ), + ), + ); + + final BoxDecoration decoration = tester + .widget( + find.descendant( + of: find.byType(CupertinoTextField), + matching: find.byType(DecoratedBox), + ), + ) + .decoration as BoxDecoration; + + expect( + decoration.color!.value, + backgroundColor.value, + ); + }); + // Regression test for https://github.com/flutter/flutter/issues/78097. testWidgets( 'still gets disabled background color when decoration is null',