Skip to content

Commit 670b30e

Browse files
authored
[material_ui] Inherit IconButtonThemeData.variant from enclosing themes (#13263)
`IconButton` resolves its Material style variant from the closest `IconButtonTheme` only. An `IconButtonTheme` that doesn't set `variant` therefore resets its subtree to Material 3, even when an ancestor opted into Material 3 Expressive. `ListTile`, `InputDecorator`, and `AppBar` hit this too, because they wrap their children in an internal `IconButtonTheme` without a variant. This PR makes a null `IconButtonThemeData.variant` inherit the variant of the enclosing icon button themes: * Adds `IconButtonTheme.variantOf`, which returns the **first non-null `variant`** of the enclosing `IconButtonTheme`s, and then falls back to `ThemeData.iconButtonTheme.variant`, and then to `StyleVariant.material3`. `IconButton` now uses it instead of `IconButtonTheme.of(context).variant`. * Keeps the current behavior of `style`: the closest theme replaces its ancestors. An explicit `variant: StyleVariant.material3` still opts a subtree out. * Documents that an enclosing `IconButtonTheme` takes precedence over `ThemeData.iconButtonTheme`, and that `IconButtonTheme.of` only returns the variant specified by the closest theme. Fixes flutter/flutter#194414 TODO: * Follow-up PR: - `AppBar`, `ListTile`, and `InputDecorator` set `variant: IconButtonTheme.variantOf(context)` on the themes they create. This fixes the common cases above. - I'll also add some notes to the `variant` docs: dialogs, bottom sheets, and menus only get the closest theme, so set the variant in `ThemeData` or on that theme. * Update the M3E migration checklist (the migration skill): a theme that a widget creates internally should always set `variant`, either to `variantOf(context)` or to `StyleVariant.material3`. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent b7e0312 commit 670b30e

4 files changed

Lines changed: 307 additions & 3 deletions

File tree

‎packages/material_ui/lib/src/icon_button.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ class IconButton extends StatelessWidget {
786786
final ThemeData theme = Theme.of(context);
787787

788788
if (theme.useMaterial3) {
789-
final StyleVariant effectiveVariant =
790-
IconButtonTheme.of(context).variant ?? StyleVariant.material3;
789+
final StyleVariant effectiveVariant = IconButtonTheme.variantOf(context);
791790

792791
final Size? minSize = constraints == null
793792
? null

‎packages/material_ui/lib/src/icon_button_theme.dart‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,23 @@ class IconButtonThemeData with Diagnosticable {
5656
/// Set this to [StyleVariant.material3Expressive] to opt icon buttons into
5757
/// the Material 3 Expressive style.
5858
///
59-
/// If null, [IconButton] uses [StyleVariant.material3].
59+
/// If null, the variant is inherited from the closest enclosing
60+
/// [IconButtonTheme] that specifies one, and then from
61+
/// [ThemeData.iconButtonTheme]. If none of them specify a variant,
62+
/// [IconButton] uses [StyleVariant.material3]. To keep a subtree on
63+
/// Material 3 when an ancestor opts into Material 3 Expressive, set this to
64+
/// [StyleVariant.material3].
65+
///
66+
/// An enclosing [IconButtonTheme] takes precedence over
67+
/// [ThemeData.iconButtonTheme], even when a closer [Theme] specifies a
68+
/// different variant. This matches [IconButtonTheme.of], which only uses
69+
/// [ThemeData.iconButtonTheme] when there is no enclosing [IconButtonTheme].
70+
/// To change the variant of a subtree, wrap it in an [IconButtonTheme].
71+
///
72+
/// See also:
73+
///
74+
/// * [IconButtonTheme.variantOf], which returns the variant that applies
75+
/// at a given location.
6076
final StyleVariant? variant;
6177

6278
/// Linearly interpolate between two icon button themes.
@@ -113,6 +129,10 @@ class IconButtonTheme extends InheritedTheme {
113129
/// If there is no enclosing [IconButtonTheme] widget, then
114130
/// [ThemeData.iconButtonTheme] is used.
115131
///
132+
/// The returned [IconButtonThemeData.variant] is the value specified by
133+
/// that theme, which can be null even when an ancestor specifies a variant.
134+
/// Use [variantOf] to get the variant that applies at the given [context].
135+
///
116136
/// Typical usage is as follows:
117137
///
118138
/// ```dart
@@ -124,6 +144,42 @@ class IconButtonTheme extends InheritedTheme {
124144
return buttonTheme?.data ?? Theme.of(context).iconButtonTheme;
125145
}
126146

147+
/// Returns the [StyleVariant] that [IconButton]s use at the given [context].
148+
///
149+
/// The closest enclosing [IconButtonTheme] whose
150+
/// [IconButtonThemeData.variant] is non-null determines the variant, so an
151+
/// [IconButtonTheme] that doesn't specify a variant inherits it from its
152+
/// ancestors. If none of the enclosing [IconButtonTheme]s specify a variant,
153+
/// the variant of [ThemeData.iconButtonTheme] is used. If that is also null,
154+
/// [StyleVariant.material3] is returned.
155+
///
156+
/// Typical usage is as follows:
157+
///
158+
/// ```dart
159+
/// final StyleVariant variant = IconButtonTheme.variantOf(context);
160+
/// ```
161+
static StyleVariant variantOf(BuildContext context) {
162+
InheritedElement? element = context.getElementForInheritedWidgetOfExactType<IconButtonTheme>();
163+
while (element != null) {
164+
// Depend on every theme that is visited, so that a change to any of them
165+
// rebuilds the dependent.
166+
context.dependOnInheritedElement(element);
167+
final StyleVariant? variant = (element.widget as IconButtonTheme).data.variant;
168+
if (variant != null) {
169+
return variant;
170+
}
171+
// Calling getElementForInheritedWidgetOfExactType on the element itself
172+
// would return the same element, so continue the search from its parent.
173+
Element? parent;
174+
element.visitAncestorElements((Element ancestor) {
175+
parent = ancestor;
176+
return false;
177+
});
178+
element = parent?.getElementForInheritedWidgetOfExactType<IconButtonTheme>();
179+
}
180+
return Theme.of(context).iconButtonTheme.variant ?? StyleVariant.material3;
181+
}
182+
127183
@override
128184
Widget wrap(BuildContext context, Widget child) {
129185
return IconButtonTheme(data: data, child: child);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Adds `IconButtonTheme.variantOf`, which returns the closest non-null `IconButtonThemeData.variant`, or `StyleVariant.material3` if none is set.
3+
version: minor

‎packages/material_ui/test/icon_button_theme_test.dart‎

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,250 @@ void main() {
364364
await tester.pumpAndSettle();
365365
expect(getOverlayColor(tester), paints..rect(color: overlayColor.withOpacity(0.1)));
366366
});
367+
368+
group('IconButtonTheme.variantOf', () {
369+
// A square icon button uses a RoundedRectangleBorder in Material 3
370+
// Expressive. Material 3 ignores shapeVariant and uses a StadiumBorder.
371+
const buttonKey = Key('button');
372+
const squareButton = IconButton(
373+
key: buttonKey,
374+
onPressed: _onPressed,
375+
style: ButtonStyle(shapeVariant: ButtonShapeVariant.square),
376+
icon: Icon(Icons.add),
377+
);
378+
const customStyle = ButtonStyle(
379+
foregroundColor: WidgetStatePropertyAll<Color>(Color(0xffff0000)),
380+
);
381+
382+
ShapeBorder? buttonShape(WidgetTester tester) {
383+
return tester
384+
.widget<Material>(
385+
find.descendant(of: find.byKey(buttonKey), matching: find.byType(Material)),
386+
)
387+
.shape;
388+
}
389+
390+
// Builds the square button and reports the variant that applies to it.
391+
Widget buttonWithVariant(ValueSetter<StyleVariant> onVariant) {
392+
return Builder(
393+
builder: (BuildContext context) {
394+
onVariant(IconButtonTheme.variantOf(context));
395+
return squareButton;
396+
},
397+
);
398+
}
399+
400+
testWidgets('returns material3 when no theme specifies a variant', (WidgetTester tester) async {
401+
StyleVariant? variant;
402+
await tester.pumpWidget(
403+
MaterialApp(
404+
home: Center(
405+
child: IconButtonTheme(
406+
data: const IconButtonThemeData(style: customStyle),
407+
child: buttonWithVariant((StyleVariant value) => variant = value),
408+
),
409+
),
410+
),
411+
);
412+
413+
expect(variant, StyleVariant.material3);
414+
expect(buttonShape(tester), isA<StadiumBorder>());
415+
});
416+
417+
testWidgets('returns the variant of ThemeData.iconButtonTheme', (WidgetTester tester) async {
418+
StyleVariant? variant;
419+
await tester.pumpWidget(
420+
MaterialApp(
421+
theme: ThemeData(
422+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
423+
),
424+
home: Center(child: buttonWithVariant((StyleVariant value) => variant = value)),
425+
),
426+
);
427+
428+
expect(variant, StyleVariant.material3Expressive);
429+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
430+
});
431+
432+
testWidgets('a nested IconButtonTheme without a variant inherits it from an IconButtonTheme', (
433+
WidgetTester tester,
434+
) async {
435+
StyleVariant? variant;
436+
await tester.pumpWidget(
437+
MaterialApp(
438+
home: Center(
439+
child: IconButtonTheme(
440+
data: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
441+
child: IconButtonTheme(
442+
data: const IconButtonThemeData(style: customStyle),
443+
child: buttonWithVariant((StyleVariant value) => variant = value),
444+
),
445+
),
446+
),
447+
),
448+
);
449+
450+
expect(variant, StyleVariant.material3Expressive);
451+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
452+
});
453+
454+
testWidgets('an IconButtonTheme without a variant inherits it from ThemeData.iconButtonTheme', (
455+
WidgetTester tester,
456+
) async {
457+
StyleVariant? variant;
458+
await tester.pumpWidget(
459+
MaterialApp(
460+
theme: ThemeData(
461+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
462+
),
463+
home: Center(
464+
child: IconButtonTheme(
465+
data: const IconButtonThemeData(style: customStyle),
466+
child: buttonWithVariant((StyleVariant value) => variant = value),
467+
),
468+
),
469+
),
470+
);
471+
472+
expect(variant, StyleVariant.material3Expressive);
473+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
474+
});
475+
476+
testWidgets('an explicit variant overrides the variant of the ancestors', (
477+
WidgetTester tester,
478+
) async {
479+
StyleVariant? variant;
480+
await tester.pumpWidget(
481+
MaterialApp(
482+
home: Center(
483+
child: IconButtonTheme(
484+
data: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
485+
child: IconButtonTheme(
486+
data: const IconButtonThemeData(
487+
style: customStyle,
488+
variant: StyleVariant.material3,
489+
),
490+
child: buttonWithVariant((StyleVariant value) => variant = value),
491+
),
492+
),
493+
),
494+
),
495+
);
496+
497+
expect(variant, StyleVariant.material3);
498+
expect(buttonShape(tester), isA<StadiumBorder>());
499+
});
500+
501+
testWidgets('an enclosing IconButtonTheme takes precedence over a closer Theme', (
502+
WidgetTester tester,
503+
) async {
504+
StyleVariant? variant;
505+
await tester.pumpWidget(
506+
MaterialApp(
507+
home: Center(
508+
child: IconButtonTheme(
509+
data: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
510+
child: Theme(
511+
data: ThemeData(
512+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3),
513+
),
514+
child: buttonWithVariant((StyleVariant value) => variant = value),
515+
),
516+
),
517+
),
518+
),
519+
);
520+
521+
expect(variant, StyleVariant.material3Expressive);
522+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
523+
});
524+
525+
testWidgets('IconButton is rebuilt when an IconButtonTheme above the closest one changes', (
526+
WidgetTester tester,
527+
) async {
528+
final variant = ValueNotifier<StyleVariant?>(StyleVariant.material3Expressive);
529+
addTearDown(variant.dispose);
530+
await tester.pumpWidget(
531+
MaterialApp(
532+
home: Center(
533+
child: ValueListenableBuilder<StyleVariant?>(
534+
valueListenable: variant,
535+
// The subtree is const, so it is only rebuilt through its
536+
// dependency on the outer IconButtonTheme.
537+
child: const IconButtonTheme(
538+
data: IconButtonThemeData(style: customStyle),
539+
child: squareButton,
540+
),
541+
builder: (BuildContext context, StyleVariant? value, Widget? child) {
542+
return IconButtonTheme(
543+
data: IconButtonThemeData(variant: value),
544+
child: child!,
545+
);
546+
},
547+
),
548+
),
549+
),
550+
);
551+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
552+
553+
variant.value = StyleVariant.material3;
554+
await tester.pumpAndSettle();
555+
expect(buttonShape(tester), isA<StadiumBorder>());
556+
});
557+
558+
testWidgets('IconButton in a ListTile uses the inherited variant', (WidgetTester tester) async {
559+
await tester.pumpWidget(
560+
MaterialApp(
561+
theme: ThemeData(
562+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
563+
),
564+
home: const Material(
565+
child: ListTile(title: Text('Title'), trailing: squareButton),
566+
),
567+
),
568+
);
569+
570+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
571+
});
572+
573+
testWidgets('IconButton in an InputDecorator suffix icon uses the inherited variant', (
574+
WidgetTester tester,
575+
) async {
576+
await tester.pumpWidget(
577+
MaterialApp(
578+
theme: ThemeData(
579+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
580+
),
581+
home: const Material(
582+
child: TextField(decoration: InputDecoration(suffixIcon: squareButton)),
583+
),
584+
),
585+
);
586+
587+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
588+
});
589+
590+
testWidgets(
591+
'IconButton in an AppBar with a custom foregroundColor uses the inherited variant',
592+
(WidgetTester tester) async {
593+
await tester.pumpWidget(
594+
MaterialApp(
595+
theme: ThemeData(
596+
iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive),
597+
),
598+
home: Scaffold(
599+
appBar: AppBar(
600+
foregroundColor: const Color(0xffff0000),
601+
actions: const <Widget>[squareButton],
602+
),
603+
),
604+
),
605+
);
606+
607+
expect(buttonShape(tester), isA<RoundedRectangleBorder>());
608+
},
609+
);
610+
});
367611
}
612+
613+
void _onPressed() {}

0 commit comments

Comments
 (0)