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
This commit is contained in:
Victor Sanni
2024-06-14 11:57:23 -07:00
committed by GitHub
parent 0aadb89764
commit 1c77696615
2 changed files with 38 additions and 4 deletions

View File

@@ -1338,10 +1338,16 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> 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,

View File

@@ -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<DecoratedBox>(
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',