From 49ff0d99d46e0b0ea7c173dd4073aef53ca42593 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Mon, 8 Mar 2021 14:39:02 -0800 Subject: [PATCH] Removed ListTile accentColor dependency (#77004) --- dev/benchmarks/test_apps/stocks/lib/main.dart | 2 +- .../stocks/test/icon_color_test.dart | 2 +- .../flutter/lib/src/material/list_tile.dart | 22 +++------ .../flutter/test/material/list_tile_test.dart | 49 +++++++++++++++++++ 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/dev/benchmarks/test_apps/stocks/lib/main.dart b/dev/benchmarks/test_apps/stocks/lib/main.dart index a1adc49d004..c18ee33e819 100644 --- a/dev/benchmarks/test_apps/stocks/lib/main.dart +++ b/dev/benchmarks/test_apps/stocks/lib/main.dart @@ -64,7 +64,7 @@ class StocksAppState extends State { case StockMode.pessimistic: return ThemeData( brightness: Brightness.dark, - accentColor: Colors.redAccent, + primarySwatch: Colors.purple, ); } assert(_configuration.stockMode != null); diff --git a/dev/benchmarks/test_apps/stocks/test/icon_color_test.dart b/dev/benchmarks/test_apps/stocks/test/icon_color_test.dart index 89234d338a5..d9280da338f 100644 --- a/dev/benchmarks/test_apps/stocks/test/icon_color_test.dart +++ b/dev/benchmarks/test_apps/stocks/test/icon_color_test.dart @@ -83,7 +83,7 @@ void main() { await tester.pump(const Duration(seconds: 5)); // end the transition // check the color of the icon - dark mode - checkIconColor(tester, 'Stock List', Colors.redAccent); // theme accent color + checkIconColor(tester, 'Stock List', Colors.purple); // theme primary color checkIconColor(tester, 'Account Balance', Colors.white38); // disabled checkIconColor(tester, 'About', Colors.white); // enabled }); diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 52dc65b6a52..af5110fb922 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -788,11 +788,6 @@ class ListTile extends StatelessWidget { /// /// When [enabled] is false, the text color is set to [ThemeData.disabledColor]. /// - /// When [selected] is true, the text color is set to [ListTileTheme.selectedColor] - /// if it's not null. If [ListTileTheme.selectedColor] is null, the text color - /// is set to [ThemeData.primaryColor] when [ThemeData.brightness] is - /// [Brightness.light] and to [ThemeData.accentColor] when it is [Brightness.dark]. - /// /// When [selected] is false, the text color is set to [ListTileTheme.textColor] /// if it's not null and to [TextTheme.caption]'s color if [ListTileTheme.textColor] /// is null. @@ -1019,9 +1014,11 @@ class ListTile extends StatelessWidget { switch (theme.brightness) { case Brightness.light: - return selected ? theme.primaryColor : Colors.black45; + // For the sake of backwards compatibility, the default for unselected + // tiles is Colors.black45 rather than colorScheme.onSurface.withAlpha(0x73). + return selected ? theme.colorScheme.primary : Colors.black45; case Brightness.dark: - return selected ? theme.accentColor : null; // null - use current icon theme color + return selected ? theme.colorScheme.primary : null; // null - use current icon theme color } } @@ -1035,14 +1032,9 @@ class ListTile extends StatelessWidget { if (!selected && tileTheme?.textColor != null) return tileTheme!.textColor; - if (selected) { - switch (theme.brightness) { - case Brightness.light: - return theme.primaryColor; - case Brightness.dark: - return theme.accentColor; - } - } + if (selected) + return theme.colorScheme.primary; + return defaultColor; } diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index 04727e926d5..98292a7914f 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -2284,4 +2284,53 @@ void main() { expect(textColor(leadingKey), theme.disabledColor); expect(textColor(trailingKey), theme.disabledColor); }); + + testWidgets('selected, enabled ListTile default icon color, light and dark themes', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/pull/77004 + + const ColorScheme lightColorScheme = ColorScheme.light(); + const ColorScheme darkColorScheme = ColorScheme.dark(); + final Key leadingKey = UniqueKey(); + final Key trailingKey = UniqueKey(); + + Widget buildFrame({ required Brightness brightness, required bool selected }) { + final ThemeData theme = brightness == Brightness.light + ? ThemeData.from(colorScheme: const ColorScheme.light()) + : ThemeData.from(colorScheme: const ColorScheme.dark()); + return MaterialApp( + theme: theme, + home: Material( + child: Center( + child: ListTile( + enabled: true, + selected: selected, + leading: TestIcon(key: leadingKey), + trailing: TestIcon(key: trailingKey), + ), + ), + ), + ); + } + + Color iconColor(Key key) => tester.state(find.byKey(key)).iconTheme.color!; + + await tester.pumpWidget(buildFrame(brightness: Brightness.light, selected: true)); + expect(iconColor(leadingKey), lightColorScheme.primary); + expect(iconColor(trailingKey), lightColorScheme.primary); + + await tester.pumpWidget(buildFrame(brightness: Brightness.light, selected: false)); + expect(iconColor(leadingKey), Colors.black45); + expect(iconColor(trailingKey), Colors.black45); + + await tester.pumpWidget(buildFrame(brightness: Brightness.dark, selected: true)); + await tester.pumpAndSettle(); // Animated theme change + expect(iconColor(leadingKey), darkColorScheme.primary); + expect(iconColor(trailingKey), darkColorScheme.primary); + + // For this configuration, ListTile defers to the default IconTheme. + // The default dark theme's IconTheme has color:white + await tester.pumpWidget(buildFrame(brightness: Brightness.dark, selected: false)); + expect(iconColor(leadingKey), Colors.white); + expect(iconColor(trailingKey), Colors.white); + }); }