From 3c3c9a1bd98f464fc2c25751dac345dd61545ebd Mon Sep 17 00:00:00 2001 From: Eilidh Southren Date: Mon, 6 Feb 2023 14:39:14 +0000 Subject: [PATCH] [M3] Add ListTile's iconColor property support for icon buttons (#120075) * add icon button property override * list tile changes * add imports * add newlines * whitespace --- .../flutter/lib/src/material/list_tile.dart | 38 +++++++++------- .../flutter/test/material/list_tile_test.dart | 44 +++++++++++++++++++ 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 1f50648400f..dba2ecceb42 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -12,6 +12,8 @@ import 'colors.dart'; import 'constants.dart'; import 'debug.dart'; import 'divider.dart'; +import 'icon_button.dart'; +import 'icon_button_theme.dart'; import 'ink_decoration.dart'; import 'ink_well.dart'; import 'list_tile_theme.dart'; @@ -692,6 +694,9 @@ class ListTile extends StatelessWidget { ?? resolveColor(theme.listTileTheme.textColor, theme.listTileTheme.selectedColor, theme.listTileTheme.textColor) ?? resolveColor(defaults.textColor, defaults.selectedColor, defaults.textColor, theme.disabledColor); final IconThemeData iconThemeData = IconThemeData(color: effectiveIconColor); + final IconButtonThemeData iconButtonThemeData = IconButtonThemeData( + style: IconButton.styleFrom(foregroundColor: effectiveIconColor), + ); TextStyle? leadingAndTrailingStyle; if (leading != null || trailing != null) { @@ -791,21 +796,24 @@ class ListTile extends StatelessWidget { minimum: resolvedContentPadding, child: IconTheme.merge( data: iconThemeData, - child: _ListTile( - leading: leadingIcon, - title: titleText, - subtitle: subtitleText, - trailing: trailingIcon, - isDense: _isDenseLayout(theme, tileTheme), - visualDensity: visualDensity ?? tileTheme.visualDensity ?? theme.visualDensity, - isThreeLine: isThreeLine, - textDirection: textDirection, - titleBaselineType: titleStyle.textBaseline ?? defaults.titleTextStyle!.textBaseline!, - subtitleBaselineType: subtitleStyle?.textBaseline ?? defaults.subtitleTextStyle!.textBaseline!, - horizontalTitleGap: horizontalTitleGap ?? tileTheme.horizontalTitleGap ?? 16, - minVerticalPadding: minVerticalPadding ?? tileTheme.minVerticalPadding ?? defaults.minVerticalPadding!, - minLeadingWidth: minLeadingWidth ?? tileTheme.minLeadingWidth ?? defaults.minLeadingWidth!, - material3: theme.useMaterial3, + child: IconButtonTheme( + data: iconButtonThemeData, + child: _ListTile( + leading: leadingIcon, + title: titleText, + subtitle: subtitleText, + trailing: trailingIcon, + isDense: _isDenseLayout(theme, tileTheme), + visualDensity: visualDensity ?? tileTheme.visualDensity ?? theme.visualDensity, + isThreeLine: isThreeLine, + textDirection: textDirection, + titleBaselineType: titleStyle.textBaseline ?? defaults.titleTextStyle!.textBaseline!, + subtitleBaselineType: subtitleStyle?.textBaseline ?? defaults.subtitleTextStyle!.textBaseline!, + horizontalTitleGap: horizontalTitleGap ?? tileTheme.horizontalTitleGap ?? 16, + minVerticalPadding: minVerticalPadding ?? tileTheme.minVerticalPadding ?? defaults.minVerticalPadding!, + minLeadingWidth: minLeadingWidth ?? tileTheme.minLeadingWidth ?? defaults.minLeadingWidth!, + material3: theme.useMaterial3, + ), ), ), ), diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index acc2f4fbb5f..350068e8a3c 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -2150,6 +2150,50 @@ void main() { expect(iconColor(leadingKey), selectedColor); }); + testWidgets('ListTile.iconColor respects iconColor property with icon buttons Material 3 in presence of IconButtonTheme override', (WidgetTester tester) async { + const Color iconButtonThemeColor = Colors.blue; + const Color listTileIconColor = Colors.green; + const Icon leadingIcon = Icon(Icons.favorite); + const Icon trailingIcon = Icon(Icons.close); + + Widget buildFrame() { + return MaterialApp( + theme: ThemeData( + useMaterial3: true, + iconButtonTheme: IconButtonThemeData( + style: IconButton.styleFrom( + foregroundColor: iconButtonThemeColor, + ), + ), + ), + home: Material( + child: Center( + child: Builder( + builder: (BuildContext context) { + return ListTile( + iconColor: listTileIconColor, + 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, listTileIconColor); + expect(getIconStyle(tester, trailingIcon.icon!)?.color, listTileIconColor); + }); + testWidgets('ListTile.dense does not throw assertion', (WidgetTester tester) async { // This is a regression test for https://github.com/flutter/flutter/pull/116908