Unverified Commit 7d9eaab0 authored by Eilidh Southren's avatar Eilidh Southren Committed by GitHub

Appbar iconTheme override fix (#118681)

* theme override fix

* add conditional centering
parent 11d21e06
...@@ -1023,39 +1023,35 @@ class _AppBarState extends State<AppBar> { ...@@ -1023,39 +1023,35 @@ class _AppBarState extends State<AppBar> {
} }
if (leading != null) { if (leading != null) {
if (theme.useMaterial3) { if (theme.useMaterial3) {
if (leading is IconButton) { final IconButtonThemeData effectiveIconButtonTheme;
final IconButtonThemeData effectiveIconButtonTheme;
// This comparison is to check if there is a custom [overallIconTheme]. If true, it means that no
// This comparison is to check if there is a custom [overallIconTheme]. If true, it means that no // custom [overallIconTheme] is provided, so [iconButtonTheme] is applied. Otherwise, we generate
// custom [overallIconTheme] is provided, so [iconButtonTheme] is applied. Otherwise, we generate // a new [IconButtonThemeData] based on the values from [overallIconTheme]. If [iconButtonTheme] only
// a new [IconButtonThemeData] based on the values from [overallIconTheme]. If [iconButtonTheme] only // has null values, the default [overallIconTheme] will be applied below by [IconTheme.merge]
// has null values, the default [overallIconTheme] will be applied below by [IconTheme.merge] if (overallIconTheme == defaults.iconTheme) {
if (overallIconTheme == defaults.iconTheme) { effectiveIconButtonTheme = iconButtonTheme;
effectiveIconButtonTheme = iconButtonTheme; } else {
} else { // The [IconButton.styleFrom] method is used to generate a correct [overlayColor] based on the [foregroundColor].
// The [IconButton.styleFrom] method is used to generate a correct [overlayColor] based on the [foregroundColor]. final ButtonStyle leadingIconButtonStyle = IconButton.styleFrom(
final ButtonStyle leadingIconButtonStyle = IconButton.styleFrom( foregroundColor: overallIconTheme.color,
foregroundColor: overallIconTheme.color, iconSize: overallIconTheme.size,
iconSize: overallIconTheme.size, );
);
effectiveIconButtonTheme = IconButtonThemeData(
effectiveIconButtonTheme = IconButtonThemeData( style: iconButtonTheme.style?.copyWith(
style: iconButtonTheme.style?.copyWith( foregroundColor: leadingIconButtonStyle.foregroundColor,
foregroundColor: leadingIconButtonStyle.foregroundColor, overlayColor: leadingIconButtonStyle.overlayColor,
overlayColor: leadingIconButtonStyle.overlayColor, iconSize: leadingIconButtonStyle.iconSize,
iconSize: leadingIconButtonStyle.iconSize,
)
);
}
leading = Center(
child: IconButtonTheme(
data: effectiveIconButtonTheme,
child: leading
) )
); );
} }
leading = IconButtonTheme(
data: effectiveIconButtonTheme,
child: leading is IconButton ? Center(child: leading) : leading,
);
// Based on the Material Design 3 specs, the leading IconButton should have // Based on the Material Design 3 specs, the leading IconButton should have
// a size of 48x48, and a highlight size of 40x40. Users can also put other // a size of 48x48, and a highlight size of 40x40. Users can also put other
// type of widgets on leading with the original config. // type of widgets on leading with the original config.
......
...@@ -3235,6 +3235,39 @@ void main() { ...@@ -3235,6 +3235,39 @@ void main() {
expect(actionIconButtonSize(), 30.0); expect(actionIconButtonSize(), 30.0);
}); });
testWidgets('AppBar.iconTheme should override any IconButtonTheme present in the theme for widgets containing an iconButton - M3', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(
iconButtonTheme: IconButtonThemeData(
style: IconButton.styleFrom(
foregroundColor: Colors.red,
iconSize: 32.0,
),
),
useMaterial3: true,
);
const IconThemeData overallIconTheme = IconThemeData(color: Colors.yellow, size: 30.0);
await tester.pumpWidget(
MaterialApp(
theme: themeData,
home: Scaffold(
appBar: AppBar(
iconTheme: overallIconTheme,
leading: BackButton(onPressed: () {}),
title: const Text('title'),
),
),
),
);
Color? leadingIconButtonColor() => iconStyle(tester, Icons.arrow_back)?.color;
double? leadingIconButtonSize() => iconStyle(tester, Icons.arrow_back)?.fontSize;
expect(leadingIconButtonColor(), Colors.yellow);
expect(leadingIconButtonSize(), 30.0);
});
testWidgets('AppBar.actionsIconTheme should override any IconButtonTheme present in the theme - M3', (WidgetTester tester) async { testWidgets('AppBar.actionsIconTheme should override any IconButtonTheme present in the theme - M3', (WidgetTester tester) async {
final ThemeData themeData = ThemeData( final ThemeData themeData = ThemeData(
iconButtonTheme: IconButtonThemeData( iconButtonTheme: IconButtonThemeData(
...@@ -3275,6 +3308,40 @@ void main() { ...@@ -3275,6 +3308,40 @@ void main() {
expect(actionIconButtonSize(), 30.0); expect(actionIconButtonSize(), 30.0);
}); });
testWidgets('AppBar.actionsIconTheme should override any IconButtonTheme present in the theme for widgets containing an iconButton - M3', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(
iconButtonTheme: IconButtonThemeData(
style: IconButton.styleFrom(
foregroundColor: Colors.red,
iconSize: 32.0,
),
),
useMaterial3: true,
);
const IconThemeData actionsIconTheme = IconThemeData(color: Colors.yellow, size: 30.0);
await tester.pumpWidget(
MaterialApp(
theme: themeData,
home: Scaffold(
appBar: AppBar(
actionsIconTheme: actionsIconTheme,
title: const Text('title'),
actions: <Widget>[
BackButton(onPressed: () {}),
],
),
),
),
);
Color? actionIconButtonColor() => iconStyle(tester, Icons.arrow_back)?.color;
double? actionIconButtonSize() => iconStyle(tester, Icons.arrow_back)?.fontSize;
expect(actionIconButtonColor(), Colors.yellow);
expect(actionIconButtonSize(), 30.0);
});
testWidgets('The foregroundColor property of the AppBar overrides any IconButtonTheme present in the theme - M3', (WidgetTester tester) async { testWidgets('The foregroundColor property of the AppBar overrides any IconButtonTheme present in the theme - M3', (WidgetTester tester) async {
final ThemeData themeData = ThemeData( final ThemeData themeData = ThemeData(
iconButtonTheme: IconButtonThemeData( iconButtonTheme: IconButtonThemeData(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment