Skip to content

Commit 15e15cc

Browse files
authored
[material_ui] Add Material 3 Expressive IconButton (flutter#12832)
This PR is to port flutter/packages#12093 which was created in `m3e_migration` feature branch. ### The original PR description This PR is to add Material 3 Expressive IconButton support behind an explicit component-level opt-in. This PR adds: * 3 new `ButtonStyle` properties used by the IconButton M3E migration: * `sizeVariant` * `iconButtonWidth` * `shapeVariant` * `IconButtonThemeData.variant` so `IconButtons` can opt into `StyleVariant.material3Expressive`. * `IconButtonTemplateM3E` to generate `IconButton` defaults from the M3E token data. * Adds the generated M3E `IconButton` defaults in: * `packages/material_ui/lib/src/generated/icon_button_defaults_m3e.g.dart` * Material 3 Expressive defines an extra-small IconButton with a 32dp visual container while retaining a 48dp interaction target. `ThemeData.materialTapTargetSize` defaults to `shrinkWrap` on desktop, so inheriting it would reduce the interaction target to 32dp. M3E therefore defaults to `MaterialTapTargetSize.padded`. Applications can still override ButtonStyle.tapTargetSize explicitly. ### TODO: * I'll add a separate PR/demo showing the Material 3 Expressive IconButton variants. * Will also update IconButton shape morphing to use physics-based motion animation. <img width="1858" height="1514" alt="image" src="https://github.com/user-attachments/assets/1914ca8e-a1e2-4881-aa9b-083f26d44b50" /> ## Pre-Review Checklist
1 parent d8933fd commit 15e15cc

14 files changed

Lines changed: 3115 additions & 21 deletions

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

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,55 @@ import 'theme_data.dart';
3131
// late BuildContext context;
3232
// typedef MyAppHome = Placeholder;
3333

34+
/// Defines size variants for Material 3 Expressive button components.
35+
///
36+
/// Each size variant represents a named visual scale. For example, a Material
37+
/// 3 Expressive [IconButton] uses this value to choose its container size, icon
38+
/// size, and padding.
39+
enum ButtonSizeVariant {
40+
/// The smallest button size.
41+
xSmall,
42+
43+
/// A small button size.
44+
small,
45+
46+
/// A medium button size.
47+
medium,
48+
49+
/// A large button size.
50+
large,
51+
52+
/// The largest button size.
53+
xLarge,
54+
}
55+
56+
/// Defines the width variants for Material 3 Expressive [IconButton].
57+
enum IconButtonWidthVariant {
58+
/// A narrower icon button.
59+
narrow,
60+
61+
/// The standard icon button width.
62+
standard,
63+
64+
/// A wider icon button.
65+
wide,
66+
}
67+
68+
/// Defines shape variants for Material 3 Expressive button components.
69+
enum ButtonShapeVariant {
70+
/// A rounded button shape.
71+
///
72+
/// For example, a Material 3 Expressive [IconButton] with this variant uses
73+
/// rounder default container shapes.
74+
round,
75+
76+
/// A squared button shape.
77+
///
78+
/// For example, a Material 3 Expressive [IconButton] with this variant uses
79+
/// more squared default container shapes.
80+
square,
81+
}
82+
3483
/// The type for [ButtonStyle.backgroundBuilder] and [ButtonStyle.foregroundBuilder].
3584
///
3685
/// The [states] parameter is the button's current pressed/hovered/etc state. The [child] is
@@ -196,6 +245,9 @@ class ButtonStyle with Diagnosticable {
196245
this.splashFactory,
197246
this.backgroundBuilder,
198247
this.foregroundBuilder,
248+
this.sizeVariant,
249+
this.iconButtonWidth,
250+
this.shapeVariant,
199251
});
200252

201253
/// The style for a button's [Text] widget descendants.
@@ -432,6 +484,42 @@ class ButtonStyle with Diagnosticable {
432484
/// configuring clipping.
433485
final ButtonLayerBuilder? foregroundBuilder;
434486

487+
/// The Material 3 Expressive size variant for this button.
488+
///
489+
/// A size variant selects a component-defined token set rather than a single
490+
/// dimension. For example, an [IconButton] uses this value to choose its
491+
/// default container size, padding, and icon size when it is using
492+
/// [StyleVariant.material3Expressive].
493+
///
494+
/// Use [minimumSize], [fixedSize], [maximumSize], [padding], or [iconSize] to
495+
/// directly override those individual style properties.
496+
final ButtonSizeVariant? sizeVariant;
497+
498+
/// The Material 3 Expressive width variant for an [IconButton].
499+
///
500+
/// A width variant selects the leading and trailing space tokens used to
501+
/// compute an icon button's default horizontal padding and minimum width when
502+
/// it is using [StyleVariant.material3Expressive]. It does not change the
503+
/// button's height or icon size.
504+
///
505+
/// This property is only used by icon buttons. Use [padding],
506+
/// [minimumSize], [fixedSize], or [maximumSize] to directly override the
507+
/// resulting layout.
508+
final IconButtonWidthVariant? iconButtonWidth;
509+
510+
/// The Material 3 Expressive shape variant for this button.
511+
///
512+
/// A shape variant selects between component-defined token families, such as
513+
/// round and square shapes. For [IconButton], this affects the default
514+
/// container and selected-state shapes when it is using
515+
/// [StyleVariant.material3Expressive]; state-specific tokens, such as the
516+
/// pressed shape, are still resolved by the component defaults.
517+
///
518+
/// Use [shape] to provide a specific [OutlinedBorder] or stateful shape
519+
/// override instead of selecting one of the Material 3 Expressive shape
520+
/// variants.
521+
final ButtonShapeVariant? shapeVariant;
522+
435523
/// Returns a copy of this ButtonStyle with the given fields replaced with
436524
/// the new values.
437525
ButtonStyle copyWith({
@@ -460,6 +548,9 @@ class ButtonStyle with Diagnosticable {
460548
InteractiveInkFeatureFactory? splashFactory,
461549
ButtonLayerBuilder? backgroundBuilder,
462550
ButtonLayerBuilder? foregroundBuilder,
551+
ButtonSizeVariant? sizeVariant,
552+
IconButtonWidthVariant? iconButtonWidth,
553+
ButtonShapeVariant? shapeVariant,
463554
}) {
464555
return ButtonStyle(
465556
textStyle: textStyle ?? this.textStyle,
@@ -487,6 +578,9 @@ class ButtonStyle with Diagnosticable {
487578
splashFactory: splashFactory ?? this.splashFactory,
488579
backgroundBuilder: backgroundBuilder ?? this.backgroundBuilder,
489580
foregroundBuilder: foregroundBuilder ?? this.foregroundBuilder,
581+
sizeVariant: sizeVariant ?? this.sizeVariant,
582+
iconButtonWidth: iconButtonWidth ?? this.iconButtonWidth,
583+
shapeVariant: shapeVariant ?? this.shapeVariant,
490584
);
491585
}
492586

@@ -525,6 +619,9 @@ class ButtonStyle with Diagnosticable {
525619
splashFactory: splashFactory ?? style.splashFactory,
526620
backgroundBuilder: backgroundBuilder ?? style.backgroundBuilder,
527621
foregroundBuilder: foregroundBuilder ?? style.foregroundBuilder,
622+
sizeVariant: sizeVariant ?? style.sizeVariant,
623+
iconButtonWidth: iconButtonWidth ?? style.iconButtonWidth,
624+
shapeVariant: shapeVariant ?? style.shapeVariant,
528625
);
529626
}
530627

@@ -556,6 +653,9 @@ class ButtonStyle with Diagnosticable {
556653
splashFactory,
557654
backgroundBuilder,
558655
foregroundBuilder,
656+
sizeVariant,
657+
iconButtonWidth,
658+
shapeVariant,
559659
];
560660
return Object.hashAll(values);
561661
}
@@ -593,7 +693,10 @@ class ButtonStyle with Diagnosticable {
593693
other.alignment == alignment &&
594694
other.splashFactory == splashFactory &&
595695
other.backgroundBuilder == backgroundBuilder &&
596-
other.foregroundBuilder == foregroundBuilder;
696+
other.foregroundBuilder == foregroundBuilder &&
697+
other.sizeVariant == sizeVariant &&
698+
other.iconButtonWidth == iconButtonWidth &&
699+
other.shapeVariant == shapeVariant;
597700
}
598701

599702
@override
@@ -715,6 +818,13 @@ class ButtonStyle with Diagnosticable {
715818
defaultValue: null,
716819
),
717820
);
821+
properties.add(EnumProperty<ButtonSizeVariant>('sizeVariant', sizeVariant, defaultValue: null));
822+
properties.add(
823+
EnumProperty<IconButtonWidthVariant>('iconButtonWidth', iconButtonWidth, defaultValue: null),
824+
);
825+
properties.add(
826+
EnumProperty<ButtonShapeVariant>('shapeVariant', shapeVariant, defaultValue: null),
827+
);
718828
}
719829

720830
/// Linearly interpolate between two [ButtonStyle]s.
@@ -778,6 +888,9 @@ class ButtonStyle with Diagnosticable {
778888
splashFactory: t < 0.5 ? a?.splashFactory : b?.splashFactory,
779889
backgroundBuilder: t < 0.5 ? a?.backgroundBuilder : b?.backgroundBuilder,
780890
foregroundBuilder: t < 0.5 ? a?.foregroundBuilder : b?.foregroundBuilder,
891+
sizeVariant: t < 0.5 ? a?.sizeVariant : b?.sizeVariant,
892+
iconButtonWidth: t < 0.5 ? a?.iconButtonWidth : b?.iconButtonWidth,
893+
shapeVariant: t < 0.5 ? a?.shapeVariant : b?.shapeVariant,
781894
);
782895
}
783896
}

0 commit comments

Comments
 (0)