Fix IconButton.color overrided by IconButtomTheme (#174515)

## Description

This PR fixes IconButton icon color resolution. 

## Before

The IconButton.color property does not take precedence over the ambient
`IconButtonTheme`

## After

The IconButton.color property overrides the ambient `IconButtonTheme`.

## Related Issue

Fixes [SnackBar closeIconColor does not respect widget
override](https://github.com/flutter/flutter/issues/174472)
Fixes [IconButton.color is overidden by
IconButtonTheme](https://github.com/flutter/flutter/issues/174511)

## Tests

Adds 1 test.
This commit is contained in:
Bruno Leroux 2025-09-05 00:17:25 +02:00 committed by GitHub
parent cc0b0471b9
commit c1ce4e530c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -745,7 +745,9 @@ class IconButton extends StatelessWidget {
if (style != null) {
adjustedStyle = style!.merge(adjustedStyle);
}
if (adjustedStyle.iconColor == null) {
adjustedStyle = adjustedStyle.copyWith(iconColor: adjustedStyle.foregroundColor);
}
Widget effectiveIcon = icon;
if ((isSelected ?? false) && selectedIcon != null) {
effectiveIcon = selectedIcon!;

View File

@ -2761,6 +2761,37 @@ void main() {
expect(getOverlayColor(tester), paints..rect(color: overlayColor));
});
// Regression test for https://github.com/flutter/flutter/issues/174511.
testWidgets('IconButton.color takes precedence over ambient IconButtonThemeData.iconColor', (
WidgetTester tester,
) async {
const Color iconButtonColor = Color(0xFFFF1234);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
iconButtonTheme: const IconButtonThemeData(
style: ButtonStyle(
iconColor: WidgetStateColor.fromMap(<WidgetStatesConstraint, Color>{
WidgetState.any: Colors.purple,
}),
),
),
),
home: Material(
child: Center(
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.add, size: 64),
color: iconButtonColor,
),
),
),
),
);
expect(_iconStyle(tester, Icons.add)?.color, iconButtonColor);
});
group('IconTheme tests in Material 3', () {
testWidgets('IconTheme overrides default values in M3', (WidgetTester tester) async {
// Theme's IconTheme