Fix remaining iconbuttontheme overrides in listtile (#169029)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

Fix remaining outstanding IconButtonTheme overrides: overlayColor and
mouseCursor.

Issue: #167727


## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Juergen Werner 2025-06-11 20:50:54 +02:00 committed by GitHub
parent 064f6d1509
commit a4a2ef6d1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 21 deletions

View File

@ -692,10 +692,13 @@ class IconButton extends StatelessWidget {
iconSize: ButtonStyleButton.allOrNull<double>(iconSize),
side: ButtonStyleButton.allOrNull<BorderSide>(side),
shape: ButtonStyleButton.allOrNull<OutlinedBorder>(shape),
mouseCursor: WidgetStateProperty<MouseCursor?>.fromMap(<WidgetStatesConstraint, MouseCursor?>{
WidgetState.disabled: disabledMouseCursor,
WidgetState.any: enabledMouseCursor,
}),
mouseCursor:
disabledMouseCursor == null && enabledMouseCursor == null
? null
: WidgetStateProperty<MouseCursor?>.fromMap(<WidgetStatesConstraint, MouseCursor?>{
WidgetState.disabled: disabledMouseCursor,
WidgetState.any: enabledMouseCursor,
}),
visualDensity: visualDensity,
tapTargetSize: tapTargetSize,
animationDuration: animationDuration,

View File

@ -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<Color?>(effectiveIconButtonColor),
) ??
IconButton.styleFrom(foregroundColor: effectiveIconButtonColor),
);
TextStyle? leadingAndTrailingStyle;

View File

@ -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<RichText>(
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<OutlinedBorder> customShape = WidgetStatePropertyAll<OutlinedBorder>(
RoundedRectangleBorder(side: BorderSide()),
);
// Specially treated in ButtonStyle.merge, thus check these, too.
const WidgetStateProperty<Color> overlayColor = WidgetStatePropertyAll<Color>(Colors.purple);
const WidgetStateProperty<MouseCursor> mouseCursor = WidgetStatePropertyAll<MouseCursor>(
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(<MaterialState>{}), 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<RichText>(
find.descendant(of: find.byIcon(iconData), matching: find.byType(RichText)),