diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index 5512aae6c5f..fda192593f6 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -692,10 +692,13 @@ class IconButton extends StatelessWidget { iconSize: ButtonStyleButton.allOrNull(iconSize), side: ButtonStyleButton.allOrNull(side), shape: ButtonStyleButton.allOrNull(shape), - mouseCursor: WidgetStateProperty.fromMap({ - WidgetState.disabled: disabledMouseCursor, - WidgetState.any: enabledMouseCursor, - }), + mouseCursor: + disabledMouseCursor == null && enabledMouseCursor == null + ? null + : WidgetStateProperty.fromMap({ + WidgetState.disabled: disabledMouseCursor, + WidgetState.any: enabledMouseCursor, + }), visualDensity: visualDensity, tapTargetSize: tapTargetSize, animationDuration: animationDuration, diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 982048e583f..eb7f7ea52db 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -815,6 +815,7 @@ class ListTile extends StatelessWidget { Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); final ThemeData theme = Theme.of(context); + final IconButtonThemeData iconButtonTheme = IconButtonTheme.of(context); final ListTileThemeData tileTheme = ListTileTheme.of(context); final ListTileStyle listTileStyle = style ?? tileTheme.style ?? theme.listTileTheme.style ?? ListTileStyle.list; @@ -841,20 +842,29 @@ class ListTile extends StatelessWidget { ).resolve(states); } - final Color? effectiveIconColor = + Color? effectiveIconColor = resolveColor(iconColor, selectedColor, iconColor) ?? resolveColor(tileTheme.iconColor, tileTheme.selectedColor, tileTheme.iconColor) ?? resolveColor( theme.listTileTheme.iconColor, theme.listTileTheme.selectedColor, theme.listTileTheme.iconColor, - ) ?? - resolveColor( - defaults.iconColor, - defaults.selectedColor, - defaults.iconColor, - theme.disabledColor, ); + + final Color? defaultEffectiveIconColor = resolveColor( + defaults.iconColor, + defaults.selectedColor, + defaults.iconColor, + theme.disabledColor, + ); + + final Color? effectiveIconButtonColor = + effectiveIconColor ?? + iconButtonTheme.style?.foregroundColor?.resolve(states) ?? + defaultEffectiveIconColor; + + effectiveIconColor ??= defaultEffectiveIconColor; + final Color? effectiveColor = resolveColor(textColor, selectedColor, textColor) ?? resolveColor(tileTheme.textColor, tileTheme.selectedColor, tileTheme.textColor) ?? @@ -871,9 +881,11 @@ class ListTile extends StatelessWidget { ); final IconThemeData iconThemeData = IconThemeData(color: effectiveIconColor); final IconButtonThemeData iconButtonThemeData = IconButtonThemeData( - style: IconButton.styleFrom( - foregroundColor: effectiveIconColor, - ).merge(IconButtonTheme.of(context).style), + style: + IconButtonTheme.of(context).style?.copyWith( + foregroundColor: WidgetStatePropertyAll(effectiveIconButtonColor), + ) ?? + IconButton.styleFrom(foregroundColor: effectiveIconButtonColor), ); TextStyle? leadingAndTrailingStyle; diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index 6f5e51a96ab..197a87a408e 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -2301,18 +2301,74 @@ void main() { }, ); + testWidgets( + 'IconButtonTheme.style.foregroundColor is preserved in ListTile in non-overriding scenario', + (WidgetTester tester) async { + const Color iconButtonThemeColor = Colors.blue; + const Icon leadingIcon = Icon(Icons.favorite); + const Icon trailingIcon = Icon(Icons.close); + + Widget buildFrame() { + return MaterialApp( + theme: ThemeData( + iconButtonTheme: IconButtonThemeData( + style: IconButton.styleFrom(foregroundColor: iconButtonThemeColor), + ), + ), + home: Material( + child: Center( + child: Builder( + builder: (BuildContext context) { + return ListTile( + leading: IconButton(icon: leadingIcon, onPressed: () {}), + trailing: IconButton(icon: trailingIcon, onPressed: () {}), + ); + }, + ), + ), + ), + ); + } + + TextStyle? getIconStyle(WidgetTester tester, IconData icon) => + tester + .widget( + find.descendant(of: find.byIcon(icon), matching: find.byType(RichText)), + ) + .text + .style; + + await tester.pumpWidget(buildFrame()); + expect(getIconStyle(tester, leadingIcon.icon!)?.color, iconButtonThemeColor); + expect(getIconStyle(tester, trailingIcon.icon!)?.color, iconButtonThemeColor); + }, + ); + testWidgets('ListTile respects and combines parent IconButtonThemeData style', ( WidgetTester tester, ) async { const Color customIconColor = Colors.green; const Icon leadingIcon = Icon(Icons.favorite); const Icon trailingIcon = Icon(Icons.close); - const OutlinedBorder customShape = RoundedRectangleBorder(side: BorderSide()); + const WidgetStateProperty customShape = WidgetStatePropertyAll( + RoundedRectangleBorder(side: BorderSide()), + ); + // Specially treated in ButtonStyle.merge, thus check these, too. + const WidgetStateProperty overlayColor = WidgetStatePropertyAll(Colors.purple); + const WidgetStateProperty mouseCursor = WidgetStatePropertyAll( + SystemMouseCursors.resizeUpRightDownLeft, + ); Widget buildFrame() { return MaterialApp( theme: ThemeData( - iconButtonTheme: IconButtonThemeData(style: IconButton.styleFrom(shape: customShape)), + iconButtonTheme: const IconButtonThemeData( + style: ButtonStyle( + overlayColor: overlayColor, + mouseCursor: mouseCursor, + shape: customShape, + ), + ), ), home: Scaffold( body: Center( @@ -2328,12 +2384,15 @@ void main() { await tester.pumpWidget(buildFrame()); - // Verify that the merged theme retains the parent shape - final BuildContext tileContext = tester.element(find.byType(ListTile)); - final IconButtonThemeData mergedTheme = IconButtonTheme.of(tileContext); - expect(mergedTheme.style?.shape?.resolve({}), customShape); + // Verify that the merged theme retains the parent shape and other critical fields. + for (final BuildContext iconButtonContext in find.byType(IconButton).evaluate()) { + final IconButtonThemeData mergedTheme = IconButtonTheme.of(iconButtonContext); + expect(mergedTheme.style?.shape, customShape); + expect(mergedTheme.style?.overlayColor, overlayColor); + expect(mergedTheme.style?.mouseCursor, mouseCursor); + } - // Verify that ListTile's iconColor overrides the inherited color + // Verify that ListTile's iconColor overrides the inherited color. Color? getIconColor(IconData iconData) { final RichText richText = tester.widget( find.descendant(of: find.byIcon(iconData), matching: find.byType(RichText)),