diff --git a/packages/material_ui/lib/src/button_style.dart b/packages/material_ui/lib/src/button_style.dart index e251296f42ed..95666b215b46 100644 --- a/packages/material_ui/lib/src/button_style.dart +++ b/packages/material_ui/lib/src/button_style.dart @@ -31,6 +31,47 @@ import 'theme_data.dart'; // late BuildContext context; // typedef MyAppHome = Placeholder; +/// Defines size variants for Material 3 Expressive button components. +/// +/// Components interpret each size variant according to their own token set. +enum ButtonSize { + /// Extra small button size. + xSmall, + + /// Small button size. This is the default for icon buttons. + small, + + /// Medium button size. + medium, + + /// Large button size. + large, + + /// Extra large button size. + xLarge, +} + +/// Defines the width variants for Material 3 Expressive [IconButton]. +enum IconButtonWidth { + /// Uses the narrow leading and trailing space tokens. + narrow, + + /// Uses the default leading and trailing space tokens. + standard, + + /// Uses the wide leading and trailing space tokens. + wide, +} + +/// Defines the shape variants for Material 3 Expressive [IconButton]. +enum IconButtonShape { + /// Uses the round shape tokens. + round, + + /// Uses the square shape tokens. + square, +} + /// The type for [ButtonStyle.backgroundBuilder] and [ButtonStyle.foregroundBuilder]. /// /// The [states] parameter is the button's current pressed/hovered/etc state. The [child] is @@ -187,6 +228,9 @@ class ButtonStyle with Diagnosticable { this.splashFactory, this.backgroundBuilder, this.foregroundBuilder, + this.size, + this.iconButtonWidth, + this.iconButtonShape, }); /// The style for a button's [Text] widget descendants. @@ -423,6 +467,15 @@ class ButtonStyle with Diagnosticable { /// configuring clipping. final ButtonLayerBuilder? foregroundBuilder; + /// The size variant for this button. + final ButtonSize? size; + + /// The width variant for this icon button. + final IconButtonWidth? iconButtonWidth; + + /// The shape variant for this icon button. + final IconButtonShape? iconButtonShape; + /// Returns a copy of this ButtonStyle with the given fields replaced with /// the new values. ButtonStyle copyWith({ @@ -451,6 +504,9 @@ class ButtonStyle with Diagnosticable { InteractiveInkFeatureFactory? splashFactory, ButtonLayerBuilder? backgroundBuilder, ButtonLayerBuilder? foregroundBuilder, + ButtonSize? size, + IconButtonWidth? iconButtonWidth, + IconButtonShape? iconButtonShape, }) { return ButtonStyle( textStyle: textStyle ?? this.textStyle, @@ -478,6 +534,9 @@ class ButtonStyle with Diagnosticable { splashFactory: splashFactory ?? this.splashFactory, backgroundBuilder: backgroundBuilder ?? this.backgroundBuilder, foregroundBuilder: foregroundBuilder ?? this.foregroundBuilder, + size: size ?? this.size, + iconButtonWidth: iconButtonWidth ?? this.iconButtonWidth, + iconButtonShape: iconButtonShape ?? this.iconButtonShape, ); } @@ -516,6 +575,9 @@ class ButtonStyle with Diagnosticable { splashFactory: splashFactory ?? style.splashFactory, backgroundBuilder: backgroundBuilder ?? style.backgroundBuilder, foregroundBuilder: foregroundBuilder ?? style.foregroundBuilder, + size: size ?? style.size, + iconButtonWidth: iconButtonWidth ?? style.iconButtonWidth, + iconButtonShape: iconButtonShape ?? style.iconButtonShape, ); } @@ -547,6 +609,9 @@ class ButtonStyle with Diagnosticable { splashFactory, backgroundBuilder, foregroundBuilder, + size, + iconButtonWidth, + iconButtonShape, ]; return Object.hashAll(values); } @@ -584,7 +649,10 @@ class ButtonStyle with Diagnosticable { other.alignment == alignment && other.splashFactory == splashFactory && other.backgroundBuilder == backgroundBuilder && - other.foregroundBuilder == foregroundBuilder; + other.foregroundBuilder == foregroundBuilder && + other.size == size && + other.iconButtonWidth == iconButtonWidth && + other.iconButtonShape == iconButtonShape; } @override @@ -706,6 +774,13 @@ class ButtonStyle with Diagnosticable { defaultValue: null, ), ); + properties.add(EnumProperty('size', size, defaultValue: null)); + properties.add( + EnumProperty('iconButtonWidth', iconButtonWidth, defaultValue: null), + ); + properties.add( + EnumProperty('iconButtonShape', iconButtonShape, defaultValue: null), + ); } /// Linearly interpolate between two [ButtonStyle]s. @@ -769,6 +844,9 @@ class ButtonStyle with Diagnosticable { splashFactory: t < 0.5 ? a?.splashFactory : b?.splashFactory, backgroundBuilder: t < 0.5 ? a?.backgroundBuilder : b?.backgroundBuilder, foregroundBuilder: t < 0.5 ? a?.foregroundBuilder : b?.foregroundBuilder, + size: t < 0.5 ? a?.size : b?.size, + iconButtonWidth: t < 0.5 ? a?.iconButtonWidth : b?.iconButtonWidth, + iconButtonShape: t < 0.5 ? a?.iconButtonShape : b?.iconButtonShape, ); } } diff --git a/packages/material_ui/lib/src/generated/icon_button_defaults_m3e.g.dart b/packages/material_ui/lib/src/generated/icon_button_defaults_m3e.g.dart new file mode 100644 index 000000000000..9ee830af00c8 --- /dev/null +++ b/packages/material_ui/lib/src/generated/icon_button_defaults_m3e.g.dart @@ -0,0 +1,1056 @@ +// Copyright 2013 The Flutter Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Do not edit by hand. The code is generated from data in the Material +// Design token database by the script: +// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart. +part of '../icon_button.dart'; + +class _IconButtonDefaultsM3E extends ButtonStyle { + _IconButtonDefaultsM3E( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.38); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.primary; + } + return _colors.onSurfaceVariant; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return _colors.primary.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.primary.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.primary.withOpacity(0.1); + } + } + if (states.contains(WidgetState.pressed)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSurfaceVariant.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get padding => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 6.0, 4.0, 6.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(6.0, 6.0, 6.0, 6.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(10.0, 6.0, 10.0, 6.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 8.0, 4.0, 8.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(8.0, 8.0, 8.0, 8.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(14.0, 8.0, 14.0, 8.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(12.0, 16.0, 12.0, 16.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(16.0, 16.0, 16.0, 16.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(24.0, 16.0, 24.0, 16.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(16.0, 32.0, 16.0, 32.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(32.0, 32.0, 32.0, 32.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(48.0, 32.0, 48.0, 32.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(32.0, 48.0, 32.0, 48.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(48.0, 48.0, 48.0, 48.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(72.0, 48.0, 72.0, 48.0), + }, + }); + + @override + WidgetStateProperty? get minimumSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(28.0, 32.0), + IconButtonWidth.standard => const Size(32.0, 32.0), + IconButtonWidth.wide => const Size(40.0, 32.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(32.0, 40.0), + IconButtonWidth.standard => const Size(40.0, 40.0), + IconButtonWidth.wide => const Size(52.0, 40.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(48.0, 56.0), + IconButtonWidth.standard => const Size(56.0, 56.0), + IconButtonWidth.wide => const Size(72.0, 56.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(64.0, 96.0), + IconButtonWidth.standard => const Size(96.0, 96.0), + IconButtonWidth.wide => const Size(128.0, 96.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(104.0, 136.0), + IconButtonWidth.standard => const Size(136.0, 136.0), + IconButtonWidth.wide => const Size(184.0, 136.0), + }, + }); + + @override + WidgetStateProperty? get maximumSize => const MaterialStatePropertyAll(Size.infinite); + + @override + WidgetStateProperty? get iconSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 20.0, + ButtonSize.small => 24.0, + ButtonSize.medium => 24.0, + ButtonSize.large => 32.0, + ButtonSize.xLarge => 40.0, + }); + + @override + WidgetStateProperty? get shape => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + }; + } + if (toggleable && states.contains(WidgetState.selected)) { + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + }; + } + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + }; + }); + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} + +class _M3EFilledIconButtonDefaults extends ButtonStyle { + _M3EFilledIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.1); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.primary; + } + if (toggleable) { + return _colors.surfaceContainer; + } + return _colors.primary; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.38); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.onPrimary; + } + if (toggleable) { + return _colors.onSurfaceVariant; + } + return _colors.onPrimary; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return _colors.onPrimary.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onPrimary.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onPrimary.withOpacity(0.1); + } + } + if (toggleable) { + if (states.contains(WidgetState.pressed)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSurfaceVariant.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + } + if (states.contains(WidgetState.pressed)) { + return _colors.onPrimary.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onPrimary.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onPrimary.withOpacity(0.1); + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get padding => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 6.0, 4.0, 6.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(6.0, 6.0, 6.0, 6.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(10.0, 6.0, 10.0, 6.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 8.0, 4.0, 8.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(8.0, 8.0, 8.0, 8.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(14.0, 8.0, 14.0, 8.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(12.0, 16.0, 12.0, 16.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(16.0, 16.0, 16.0, 16.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(24.0, 16.0, 24.0, 16.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(16.0, 32.0, 16.0, 32.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(32.0, 32.0, 32.0, 32.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(48.0, 32.0, 48.0, 32.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(32.0, 48.0, 32.0, 48.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(48.0, 48.0, 48.0, 48.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(72.0, 48.0, 72.0, 48.0), + }, + }); + + @override + WidgetStateProperty? get minimumSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(28.0, 32.0), + IconButtonWidth.standard => const Size(32.0, 32.0), + IconButtonWidth.wide => const Size(40.0, 32.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(32.0, 40.0), + IconButtonWidth.standard => const Size(40.0, 40.0), + IconButtonWidth.wide => const Size(52.0, 40.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(48.0, 56.0), + IconButtonWidth.standard => const Size(56.0, 56.0), + IconButtonWidth.wide => const Size(72.0, 56.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(64.0, 96.0), + IconButtonWidth.standard => const Size(96.0, 96.0), + IconButtonWidth.wide => const Size(128.0, 96.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(104.0, 136.0), + IconButtonWidth.standard => const Size(136.0, 136.0), + IconButtonWidth.wide => const Size(184.0, 136.0), + }, + }); + + @override + WidgetStateProperty? get maximumSize => const MaterialStatePropertyAll(Size.infinite); + + @override + WidgetStateProperty? get iconSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 20.0, + ButtonSize.small => 24.0, + ButtonSize.medium => 24.0, + ButtonSize.large => 32.0, + ButtonSize.xLarge => 40.0, + }); + + @override + WidgetStateProperty? get shape => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + }; + } + if (toggleable && states.contains(WidgetState.selected)) { + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + }; + } + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + }; + }); + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} + +class _M3EFilledTonalIconButtonDefaults extends ButtonStyle { + _M3EFilledTonalIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.1); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.secondary; + } + if (toggleable) { + return _colors.secondaryContainer; + } + return _colors.secondaryContainer; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.38); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.onSecondary; + } + if (toggleable) { + return _colors.onSecondaryContainer; + } + return _colors.onSecondaryContainer; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return _colors.onSecondary.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSecondary.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSecondary.withOpacity(0.1); + } + } + if (toggleable) { + if (states.contains(WidgetState.pressed)) { + return _colors.onSecondaryContainer.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSecondaryContainer.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSecondaryContainer.withOpacity(0.1); + } + } + if (states.contains(WidgetState.pressed)) { + return _colors.onSecondaryContainer.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSecondaryContainer.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSecondaryContainer.withOpacity(0.1); + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get padding => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 6.0, 4.0, 6.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(6.0, 6.0, 6.0, 6.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(10.0, 6.0, 10.0, 6.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 8.0, 4.0, 8.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(8.0, 8.0, 8.0, 8.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(14.0, 8.0, 14.0, 8.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(12.0, 16.0, 12.0, 16.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(16.0, 16.0, 16.0, 16.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(24.0, 16.0, 24.0, 16.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(16.0, 32.0, 16.0, 32.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(32.0, 32.0, 32.0, 32.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(48.0, 32.0, 48.0, 32.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(32.0, 48.0, 32.0, 48.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(48.0, 48.0, 48.0, 48.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(72.0, 48.0, 72.0, 48.0), + }, + }); + + @override + WidgetStateProperty? get minimumSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(28.0, 32.0), + IconButtonWidth.standard => const Size(32.0, 32.0), + IconButtonWidth.wide => const Size(40.0, 32.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(32.0, 40.0), + IconButtonWidth.standard => const Size(40.0, 40.0), + IconButtonWidth.wide => const Size(52.0, 40.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(48.0, 56.0), + IconButtonWidth.standard => const Size(56.0, 56.0), + IconButtonWidth.wide => const Size(72.0, 56.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(64.0, 96.0), + IconButtonWidth.standard => const Size(96.0, 96.0), + IconButtonWidth.wide => const Size(128.0, 96.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(104.0, 136.0), + IconButtonWidth.standard => const Size(136.0, 136.0), + IconButtonWidth.wide => const Size(184.0, 136.0), + }, + }); + + @override + WidgetStateProperty? get maximumSize => const MaterialStatePropertyAll(Size.infinite); + + @override + WidgetStateProperty? get iconSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 20.0, + ButtonSize.small => 24.0, + ButtonSize.medium => 24.0, + ButtonSize.large => 32.0, + ButtonSize.xLarge => 40.0, + }); + + @override + WidgetStateProperty? get shape => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + }; + } + if (toggleable && states.contains(WidgetState.selected)) { + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + }; + } + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + }; + }); + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} + +class _M3EOutlinedIconButtonDefaults extends ButtonStyle { + _M3EOutlinedIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.onSurface.withOpacity(0.1); + } + return Colors.transparent; + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.inverseSurface; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return _colors.onSurface.withOpacity(0.38); + } + if (toggleable && states.contains(WidgetState.selected)) { + return _colors.onInverseSurface; + } + return _colors.onSurfaceVariant; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return _colors.onInverseSurface.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onInverseSurface.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onInverseSurface.withOpacity(0.1); + } + } + if (states.contains(WidgetState.pressed)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + if (states.contains(WidgetState.hovered)) { + return _colors.onSurfaceVariant.withOpacity(0.08); + } + if (states.contains(WidgetState.focused)) { + return _colors.onSurfaceVariant.withOpacity(0.1); + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get padding => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 6.0, 4.0, 6.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(6.0, 6.0, 6.0, 6.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(10.0, 6.0, 10.0, 6.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(4.0, 8.0, 4.0, 8.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(8.0, 8.0, 8.0, 8.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(14.0, 8.0, 14.0, 8.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(12.0, 16.0, 12.0, 16.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(16.0, 16.0, 16.0, 16.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(24.0, 16.0, 24.0, 16.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(16.0, 32.0, 16.0, 32.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(32.0, 32.0, 32.0, 32.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(48.0, 32.0, 48.0, 32.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB(32.0, 48.0, 32.0, 48.0), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB(48.0, 48.0, 48.0, 48.0), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB(72.0, 48.0, 72.0, 48.0), + }, + }); + + @override + WidgetStateProperty? get minimumSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(28.0, 32.0), + IconButtonWidth.standard => const Size(32.0, 32.0), + IconButtonWidth.wide => const Size(40.0, 32.0), + }, + ButtonSize.small => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(32.0, 40.0), + IconButtonWidth.standard => const Size(40.0, 40.0), + IconButtonWidth.wide => const Size(52.0, 40.0), + }, + ButtonSize.medium => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(48.0, 56.0), + IconButtonWidth.standard => const Size(56.0, 56.0), + IconButtonWidth.wide => const Size(72.0, 56.0), + }, + ButtonSize.large => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(64.0, 96.0), + IconButtonWidth.standard => const Size(96.0, 96.0), + IconButtonWidth.wide => const Size(128.0, 96.0), + }, + ButtonSize.xLarge => switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(104.0, 136.0), + IconButtonWidth.standard => const Size(136.0, 136.0), + IconButtonWidth.wide => const Size(184.0, 136.0), + }, + }); + + @override + WidgetStateProperty? get maximumSize => const MaterialStatePropertyAll(Size.infinite); + + @override + WidgetStateProperty? get iconSize => + MaterialStatePropertyAll(switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 20.0, + ButtonSize.small => 24.0, + ButtonSize.medium => 24.0, + ButtonSize.large => 32.0, + ButtonSize.xLarge => 40.0, + }); + + @override + WidgetStateProperty? get shape => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + }; + } + if (toggleable && states.contains(WidgetState.selected)) { + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + }; + } + return switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }, + IconButtonShape.square => switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.small => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0)), + ), + ButtonSize.medium => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16.0)), + ), + ButtonSize.large => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + ButtonSize.xLarge => const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28.0)), + ), + }, + }; + }); + + @override + WidgetStateProperty? get side => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + return null; + } + if (states.contains(WidgetState.disabled)) { + return BorderSide( + color: _colors.outlineVariant, + width: switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 1.0, + ButtonSize.small => 1.0, + ButtonSize.medium => 1.0, + ButtonSize.large => 2.0, + ButtonSize.xLarge => 3.0, + }, + ); + } + return BorderSide( + color: _colors.outlineVariant, + width: switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => 1.0, + ButtonSize.small => 1.0, + ButtonSize.medium => 1.0, + ButtonSize.large => 2.0, + ButtonSize.xLarge => 3.0, + }, + ); + }); + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} diff --git a/packages/material_ui/lib/src/icon_button.dart b/packages/material_ui/lib/src/icon_button.dart index 9f026b7aa3dc..54351408d99a 100644 --- a/packages/material_ui/lib/src/icon_button.dart +++ b/packages/material_ui/lib/src/icon_button.dart @@ -31,6 +31,8 @@ import 'theme.dart'; import 'theme_data.dart'; import 'tooltip.dart'; +part 'generated/icon_button_defaults_m3e.g.dart'; + // Examples can assume: // late BuildContext context; @@ -626,6 +628,11 @@ class IconButton extends StatelessWidget { /// create a [WidgetStateProperty] with a single value for all /// states. /// + /// The [size], [iconButtonWidth], and [iconButtonShape] parameters configure + /// the Material 3 Expressive token size, width, and shape through + /// [ButtonStyle.size], [ButtonStyle.iconButtonWidth], and + /// [ButtonStyle.iconButtonShape]. + /// /// All parameters default to null, by default this method returns /// a [ButtonStyle] that doesn't override anything. /// @@ -670,6 +677,9 @@ class IconButton extends StatelessWidget { bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory, + ButtonSize? size, + IconButtonWidth? iconButtonWidth, + IconButtonShape? iconButtonShape, }) { final Color? overlayFallback = overlayColor ?? foregroundColor; WidgetStateProperty? overlayColorProp; @@ -710,6 +720,9 @@ class IconButton extends StatelessWidget { enableFeedback: enableFeedback, alignment: alignment, splashFactory: splashFactory, + size: size, + iconButtonWidth: iconButtonWidth, + iconButtonShape: iconButtonShape, ); } @@ -718,6 +731,9 @@ class IconButton extends StatelessWidget { final ThemeData theme = Theme.of(context); if (theme.useMaterial3) { + final StyleVariant effectiveVariant = + IconButtonTheme.of(context).variant ?? StyleVariant.material3; + final Size? minSize = constraints == null ? null : Size(constraints!.minWidth, constraints!.minHeight); @@ -761,6 +777,7 @@ class IconButton extends StatelessWidget { focusNode: focusNode, isSelected: isSelected, variant: _variant, + styleVariant: effectiveVariant, tooltip: tooltip, statesController: statesController, child: effectiveIcon, @@ -867,6 +884,7 @@ class _SelectableIconButton extends StatefulWidget { this.onHover, this.statesController, required this.variant, + required this.styleVariant, required this.autofocus, required this.onPressed, this.tooltip, @@ -877,6 +895,7 @@ class _SelectableIconButton extends StatefulWidget { final ButtonStyle? style; final FocusNode? focusNode; final _IconButtonVariant variant; + final StyleVariant styleVariant; final bool autofocus; final VoidCallback? onPressed; final String? tooltip; @@ -945,7 +964,8 @@ class _SelectableIconButtonState extends State<_SelectableIconButton> { onPressed: widget.onPressed, onHover: widget.onHover, onLongPress: widget.onPressed != null ? widget.onLongPress : null, - variant: widget.variant, + iconButtonVariant: widget.variant, + styleVariant: widget.styleVariant, toggleable: toggleable, tooltip: widget.tooltip, child: Semantics(selected: widget.isSelected, child: widget.child), @@ -968,15 +988,29 @@ class _IconButtonM3 extends ButtonStyleButton { super.onLongPress, super.autofocus = false, super.statesController, - required this.variant, + required this.iconButtonVariant, + required this.styleVariant, required this.toggleable, super.tooltip, required Widget super.child, }) : super(onFocusChange: null, clipBehavior: Clip.none); - final _IconButtonVariant variant; + final _IconButtonVariant iconButtonVariant; + final StyleVariant styleVariant; final bool toggleable; + ButtonSize? _effectiveSize(BuildContext context) { + return style?.size ?? IconButtonTheme.of(context).style?.size; + } + + IconButtonWidth? _effectiveWidth(BuildContext context) { + return style?.iconButtonWidth ?? IconButtonTheme.of(context).style?.iconButtonWidth; + } + + IconButtonShape? _effectiveShape(BuildContext context) { + return style?.iconButtonShape ?? IconButtonTheme.of(context).style?.iconButtonShape; + } + /// ## Material 3 defaults /// /// If [ThemeData.useMaterial3] is set to true the following defaults will @@ -1014,11 +1048,47 @@ class _IconButtonM3 extends ButtonStyleButton { /// * `splashFactory` - Theme.splashFactory @override ButtonStyle defaultStyleOf(BuildContext context) { - return switch (variant) { - _IconButtonVariant.filled => _FilledIconButtonDefaultsM3(context, toggleable), - _IconButtonVariant.filledTonal => _FilledTonalIconButtonDefaultsM3(context, toggleable), - _IconButtonVariant.outlined => _OutlinedIconButtonDefaultsM3(context, toggleable), - _IconButtonVariant.standard => _IconButtonDefaultsM3(context, toggleable), + final ButtonSize? effectiveSize = _effectiveSize(context); + final IconButtonWidth? effectiveWidth = _effectiveWidth(context); + final IconButtonShape? effectiveShape = _effectiveShape(context); + + return switch (styleVariant) { + StyleVariant.material3 => switch (iconButtonVariant) { + _IconButtonVariant.filled => _FilledIconButtonDefaultsM3(context, toggleable), + _IconButtonVariant.filledTonal => _FilledTonalIconButtonDefaultsM3(context, toggleable), + _IconButtonVariant.outlined => _OutlinedIconButtonDefaultsM3(context, toggleable), + _IconButtonVariant.standard => _IconButtonDefaultsM3(context, toggleable), + }, + StyleVariant.material3Expressive => switch (iconButtonVariant) { + _IconButtonVariant.filled => _M3EFilledIconButtonDefaults( + context, + toggleable, + effectiveSize, + effectiveWidth, + effectiveShape, + ), + _IconButtonVariant.filledTonal => _M3EFilledTonalIconButtonDefaults( + context, + toggleable, + effectiveSize, + effectiveWidth, + effectiveShape, + ), + _IconButtonVariant.outlined => _M3EOutlinedIconButtonDefaults( + context, + toggleable, + effectiveSize, + effectiveWidth, + effectiveShape, + ), + _IconButtonVariant.standard => _IconButtonDefaultsM3E( + context, + toggleable, + effectiveSize, + effectiveWidth, + effectiveShape, + ), + }, }; } diff --git a/packages/material_ui/lib/src/icon_button_theme.dart b/packages/material_ui/lib/src/icon_button_theme.dart index 0d38686dba0f..cd86c3bf3879 100644 --- a/packages/material_ui/lib/src/icon_button_theme.dart +++ b/packages/material_ui/lib/src/icon_button_theme.dart @@ -10,6 +10,7 @@ import 'package:flutter/widgets.dart'; import 'button_style.dart'; import 'theme.dart'; +import 'theme_data.dart'; // Examples can assume: // late BuildContext context; @@ -39,7 +40,7 @@ class IconButtonThemeData with Diagnosticable { /// Creates a [IconButtonThemeData]. /// /// The [style] may be null. - const IconButtonThemeData({this.style}); + const IconButtonThemeData({this.style, this.variant}); /// Overrides for [IconButton]'s default style if [ThemeData.useMaterial3] /// is set to true. @@ -50,16 +51,27 @@ class IconButtonThemeData with Diagnosticable { /// If [style] is null, then this theme doesn't override anything. final ButtonStyle? style; + /// The style variant of Material Design used by [IconButton]. + /// + /// Set this to [StyleVariant.material3Expressive] to opt icon buttons into + /// the Material 3 Expressive defaults. + /// + /// If null, [IconButton] uses [StyleVariant.material3]. + final StyleVariant? variant; + /// Linearly interpolate between two icon button themes. static IconButtonThemeData? lerp(IconButtonThemeData? a, IconButtonThemeData? b, double t) { if (identical(a, b)) { return a; } - return IconButtonThemeData(style: ButtonStyle.lerp(a?.style, b?.style, t)); + return IconButtonThemeData( + style: ButtonStyle.lerp(a?.style, b?.style, t), + variant: t < 0.5 ? a?.variant : b?.variant, + ); } @override - int get hashCode => style.hashCode; + int get hashCode => Object.hash(style, variant); @override bool operator ==(Object other) { @@ -69,13 +81,14 @@ class IconButtonThemeData with Diagnosticable { if (other.runtimeType != runtimeType) { return false; } - return other is IconButtonThemeData && other.style == style; + return other is IconButtonThemeData && other.style == style && other.variant == variant; } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('style', style, defaultValue: null)); + properties.add(EnumProperty('variant', variant, defaultValue: null)); } } diff --git a/packages/material_ui/test/icon_button_test.dart b/packages/material_ui/test/icon_button_test.dart index 1bb186526103..95cafa50c451 100644 --- a/packages/material_ui/test/icon_button_test.dart +++ b/packages/material_ui/test/icon_button_test.dart @@ -3571,6 +3571,740 @@ void main() { ); expect(tester.getSize(find.byType(IconButton)), Size.zero); }); + + Widget buildM3EApp({required Widget child, ThemeData? theme}) { + return MaterialApp( + theme: + theme ?? + ThemeData( + useMaterial3: true, + iconButtonTheme: const IconButtonThemeData(variant: StyleVariant.material3Expressive), + ), + home: Scaffold(body: Center(child: child)), + ); + } + + Finder m3eIconButtonMaterialFinder() { + return find.descendant(of: find.byType(IconButton), matching: find.byType(Material)); + } + + Material m3eIconButtonMaterial(WidgetTester tester) { + return tester.widget(m3eIconButtonMaterialFinder()); + } + + Size m3eIconButtonMaterialSize(WidgetTester tester) { + return tester.getSize(m3eIconButtonMaterialFinder()); + } + + ColorScheme m3eColorScheme(WidgetTester tester) { + return Theme.of(tester.element(find.byType(IconButton))).colorScheme; + } + + Color? m3eIconColor(WidgetTester tester, IconData icon) { + return IconTheme.of(tester.element(find.byIcon(icon))).color; + } + + group('M3E IconButton size variants', () { + testWidgets('default size is small (40x40)', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + // ButtonStyleButton renders with minimum size 40x40, but tap target + // padding brings it to 48x48. + expect(m3eIconButtonMaterialSize(tester), const Size(40.0, 40.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(48.0, 48.0)); + }); + + testWidgets('xSmall size renders at 32dp minimum', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.xSmall), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(32.0, 32.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(48.0, 48.0)); + }); + + testWidgets('styleFrom sets the size variant', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: IconButton.styleFrom(size: ButtonSize.medium), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(56.0, 56.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(56.0, 56.0)); + }); + + testWidgets('medium size renders at 56dp minimum', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.medium), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(56.0, 56.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(56.0, 56.0)); + }); + + testWidgets('large size renders at 96dp minimum', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.large), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(96.0, 96.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(96.0, 96.0)); + }); + + testWidgets('xLarge size renders at 136dp minimum', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.xLarge), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(136.0, 136.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(136.0, 136.0)); + }); + }); + + group('M3E IconButton width variants', () { + testWidgets('small IconButton supports narrow, standard, and wide widths', ( + WidgetTester tester, + ) async { + Future materialSizeFor(IconButtonWidth width) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: ButtonStyle(iconButtonWidth: width), + ), + ), + ); + return m3eIconButtonMaterialSize(tester); + } + + expect(await materialSizeFor(IconButtonWidth.narrow), const Size(32.0, 40.0)); + expect(await materialSizeFor(IconButtonWidth.standard), const Size(40.0, 40.0)); + expect(await materialSizeFor(IconButtonWidth.wide), const Size(52.0, 40.0)); + + expect(m3eIconButtonMaterial(tester).animationDuration, kThemeChangeDuration); + }); + + testWidgets('IconButtonThemeData style width sets default width', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + theme: ThemeData( + useMaterial3: true, + iconButtonTheme: const IconButtonThemeData( + style: ButtonStyle(iconButtonWidth: IconButtonWidth.wide), + variant: StyleVariant.material3Expressive, + ), + ), + child: IconButton(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(52.0, 40.0)); + }); + }); + + group('M3E IconButton shape', () { + OutlinedBorder materialShape(WidgetTester tester) { + final Material material = tester.widget( + find.descendant(of: find.byType(IconButton), matching: find.byType(Material)), + ); + return material.shape! as OutlinedBorder; + } + + testWidgets('default shape resolves M3E token shapes by state', (WidgetTester tester) async { + final statesController = MaterialStatesController(); + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + statesController: statesController, + isSelected: true, + onPressed: () {}, + icon: const Icon(Icons.add), + ), + ), + ); + expect( + materialShape(tester), + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0))), + ); + + statesController.update(WidgetState.pressed, true); + await tester.pumpAndSettle(); + + expect( + materialShape(tester), + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))), + ); + statesController.dispose(); + }); + + testWidgets('square shape resolves M3E token shapes by state', (WidgetTester tester) async { + final statesController = MaterialStatesController(); + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + statesController: statesController, + isSelected: false, + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle( + size: ButtonSize.medium, + iconButtonShape: IconButtonShape.square, + ), + ), + ), + ); + + expect( + materialShape(tester), + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))), + ); + + statesController.update(WidgetState.pressed, true); + await tester.pumpAndSettle(); + + expect( + materialShape(tester), + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0))), + ); + + statesController.update(WidgetState.pressed, false); + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + statesController: statesController, + isSelected: true, + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle( + size: ButtonSize.medium, + iconButtonShape: IconButtonShape.square, + ), + ), + ), + ); + await tester.pumpAndSettle(); + + expect(materialShape(tester), const StadiumBorder()); + statesController.dispose(); + }); + + testWidgets('ButtonStyle.shape remains the stateful shape override API', ( + WidgetTester tester, + ) async { + final statesController = MaterialStatesController(); + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + statesController: statesController, + onPressed: () {}, + icon: const Icon(Icons.add), + style: ButtonStyle( + shape: WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(4.0)), + ); + } + return const StadiumBorder(); + }), + ), + ), + ), + ); + + expect(materialShape(tester), const StadiumBorder()); + + statesController.update(WidgetState.pressed, true); + await tester.pumpAndSettle(); + + expect( + materialShape(tester), + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0))), + ); + statesController.dispose(); + }); + }); + + group('M3E IconButton variants', () { + testWidgets('standard variant has transparent background', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + expect(m3eIconButtonMaterial(tester).color, Colors.transparent); + expect(m3eIconButtonMaterial(tester).shape, const StadiumBorder()); + }); + + testWidgets('filled variant resolves default container color', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton.filled(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + expect(m3eIconButtonMaterial(tester).color, m3eColorScheme(tester).primary); + expect(m3eIconButtonMaterial(tester).shape, const StadiumBorder()); + }); + + testWidgets('filledTonal variant resolves default container color', ( + WidgetTester tester, + ) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton.filledTonal(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + expect(m3eIconButtonMaterial(tester).color, m3eColorScheme(tester).secondaryContainer); + expect(m3eIconButtonMaterial(tester).shape, const StadiumBorder()); + }); + + testWidgets('outlined variant resolves default side and transparent background', ( + WidgetTester tester, + ) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton.outlined(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + final shape = m3eIconButtonMaterial(tester).shape! as StadiumBorder; + expect(m3eIconButtonMaterial(tester).color, Colors.transparent); + expect(shape.side, BorderSide(color: m3eColorScheme(tester).outlineVariant)); + }); + + testWidgets('filled variant with style size', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton.filled( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.large), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(96.0, 96.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(96.0, 96.0)); + }); + + testWidgets('outlined variant with style size', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton.outlined( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.medium), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(56.0, 56.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(56.0, 56.0)); + }); + }); + + group('M3E IconButton theme integration', () { + testWidgets('IconButtonThemeData style size sets default size', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + theme: ThemeData( + useMaterial3: true, + iconButtonTheme: const IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.large), + variant: StyleVariant.material3Expressive, + ), + ), + child: IconButton(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(96.0, 96.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(96.0, 96.0)); + }); + + testWidgets('widget size overrides theme size', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + theme: ThemeData( + useMaterial3: true, + iconButtonTheme: const IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.large), + variant: StyleVariant.material3Expressive, + ), + ), + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.xSmall), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(32.0, 32.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(48.0, 48.0)); + }); + + testWidgets('IconButtonTheme wrapping sets size', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButtonTheme( + data: const IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.medium), + variant: StyleVariant.material3Expressive, + ), + child: IconButton(onPressed: () {}, icon: const Icon(Icons.add)), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(56.0, 56.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(56.0, 56.0)); + }); + }); + + group('M3E IconButton selection', () { + testWidgets('isSelected shows selectedIcon', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: true, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + + expect(find.byIcon(Icons.favorite), findsOneWidget); + expect(find.byIcon(Icons.favorite_border), findsNothing); + }); + + testWidgets('isSelected exposes selected semantics', (WidgetTester tester) async { + final SemanticsHandle handle = tester.ensureSemantics(); + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: true, + icon: const Icon(Icons.favorite_border, semanticLabel: 'favorite'), + selectedIcon: const Icon(Icons.favorite, semanticLabel: 'favorite'), + ), + ), + ); + + expect( + tester.getSemantics(find.byType(IconButton)), + matchesSemantics( + hasTapAction: true, + hasFocusAction: true, + hasEnabledState: true, + isButton: true, + isEnabled: true, + isFocusable: true, + hasSelectedState: true, + isSelected: true, + label: 'favorite', + ), + ); + handle.dispose(); + }); + + testWidgets('external selected state does not affect non-toggleable visual state', ( + WidgetTester tester, + ) async { + final statesController = MaterialStatesController(); + statesController.update(WidgetState.selected, true); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + statesController: statesController, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + + final Material material = tester.widget( + find.descendant(of: find.byType(IconButton), matching: find.byType(Material)), + ); + expect(material.shape, const StadiumBorder()); + expect(find.byIcon(Icons.favorite_border), findsOneWidget); + expect(find.byIcon(Icons.favorite), findsNothing); + }); + + testWidgets('isSelected false shows regular icon', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: false, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + + expect(find.byIcon(Icons.favorite_border), findsOneWidget); + expect(find.byIcon(Icons.favorite), findsNothing); + }); + + testWidgets('isSelected updates selected widget state when toggled through null', ( + WidgetTester tester, + ) async { + final statesController = MaterialStatesController(); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: true, + statesController: statesController, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + expect(statesController.value, contains(WidgetState.selected)); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + statesController: statesController, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + expect(statesController.value, isNot(contains(WidgetState.selected))); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: false, + statesController: statesController, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + expect(statesController.value, isNot(contains(WidgetState.selected))); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + isSelected: true, + statesController: statesController, + icon: const Icon(Icons.favorite_border), + selectedIcon: const Icon(Icons.favorite), + ), + ), + ); + expect(statesController.value, contains(WidgetState.selected)); + }); + }); + + group('M3E IconButton disabled state', () { + testWidgets('disabled button has reduced opacity colors', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp(child: const IconButton(onPressed: null, icon: Icon(Icons.add))), + ); + + expect(m3eIconButtonMaterial(tester).color, Colors.transparent); + expect(m3eIconColor(tester, Icons.add), m3eColorScheme(tester).onSurface.withOpacity(0.38)); + }); + + testWidgets('onLongPress without onPressed keeps button disabled', (WidgetTester tester) async { + var longPressed = false; + final SemanticsHandle handle = tester.ensureSemantics(); + + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: null, + onLongPress: () { + longPressed = true; + }, + icon: const Icon(Icons.add, semanticLabel: 'add'), + ), + ), + ); + + expect( + tester.getSemantics(find.byType(IconButton)), + matchesSemantics(hasEnabledState: true, isButton: true, label: 'add'), + ); + + await tester.longPress(find.byType(IconButton)); + expect(longPressed, isFalse); + handle.dispose(); + }); + + testWidgets('disabled filled button has reduced background', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp(child: const IconButton.filled(onPressed: null, icon: Icon(Icons.add))), + ); + + expect( + m3eIconButtonMaterial(tester).color, + m3eColorScheme(tester).onSurface.withOpacity(0.1), + ); + expect(m3eIconColor(tester, Icons.add), m3eColorScheme(tester).onSurface.withOpacity(0.38)); + }); + }); + + group('IconButtonThemeData', () { + test('equality', () { + const a = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.small, + iconButtonWidth: IconButtonWidth.standard, + iconButtonShape: IconButtonShape.round, + ), + ); + const b = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.small, + iconButtonWidth: IconButtonWidth.standard, + iconButtonShape: IconButtonShape.round, + ), + ); + const c = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.large, + iconButtonWidth: IconButtonWidth.wide, + iconButtonShape: IconButtonShape.square, + ), + ); + + expect(a, equals(b)); + expect(a, isNot(equals(c))); + }); + + test('hashCode', () { + const a = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.small, + iconButtonWidth: IconButtonWidth.narrow, + iconButtonShape: IconButtonShape.round, + ), + ); + const b = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.small, + iconButtonWidth: IconButtonWidth.narrow, + iconButtonShape: IconButtonShape.round, + ), + ); + + expect(a.hashCode, equals(b.hashCode)); + }); + + test('lerp', () { + const a = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.small, + iconButtonWidth: IconButtonWidth.narrow, + iconButtonShape: IconButtonShape.round, + ), + ); + const b = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.large, + iconButtonWidth: IconButtonWidth.wide, + iconButtonShape: IconButtonShape.square, + ), + ); + + expect(IconButtonThemeData.lerp(a, b, 0.0)?.style?.size, ButtonSize.small); + expect(IconButtonThemeData.lerp(a, b, 0.4)?.style?.size, ButtonSize.small); + expect(IconButtonThemeData.lerp(a, b, 0.5)?.style?.size, ButtonSize.large); + expect(IconButtonThemeData.lerp(a, b, 1.0)?.style?.size, ButtonSize.large); + expect(IconButtonThemeData.lerp(a, b, 0.4)?.style?.iconButtonWidth, IconButtonWidth.narrow); + expect(IconButtonThemeData.lerp(a, b, 0.5)?.style?.iconButtonWidth, IconButtonWidth.wide); + expect(IconButtonThemeData.lerp(a, b, 0.4)?.style?.iconButtonShape, IconButtonShape.round); + expect(IconButtonThemeData.lerp(a, b, 0.5)?.style?.iconButtonShape, IconButtonShape.square); + }); + + test('debugFillProperties includes size, width, and shape', () { + const data = IconButtonThemeData( + style: ButtonStyle( + size: ButtonSize.medium, + iconButtonWidth: IconButtonWidth.wide, + iconButtonShape: IconButtonShape.square, + ), + ); + final builder = DiagnosticPropertiesBuilder(); + data.debugFillProperties(builder); + + final List descriptions = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()) + .toList(); + + expect(descriptions, contains(contains('size: medium'))); + expect(descriptions, contains(contains('iconButtonWidth: wide'))); + expect(descriptions, contains(contains('iconButtonShape: square'))); + }); + }); + + group('M3E IconButton variant opt in', () { + testWidgets('IconButtonThemeData variant enables M3E defaults', (WidgetTester tester) async { + await tester.pumpWidget( + buildM3EApp( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.add), + style: const ButtonStyle(size: ButtonSize.medium), + ), + ), + ); + + expect(m3eIconButtonMaterialSize(tester), const Size(56.0, 56.0)); + expect(tester.getSize(find.byType(IconButton)), const Size(56.0, 56.0)); + }); + }); } Widget buildAllVariants({ diff --git a/packages/material_ui/test/icon_button_theme_test.dart b/packages/material_ui/test/icon_button_theme_test.dart index c271ba758bd4..c4671671f0c6 100644 --- a/packages/material_ui/test/icon_button_theme_test.dart +++ b/packages/material_ui/test/icon_button_theme_test.dart @@ -20,6 +20,36 @@ void main() { expect(identical(IconButtonThemeData.lerp(data, data, 0.5), data), true); }); + test('IconButtonThemeData supports Material 3 Expressive variant', () { + const data = IconButtonThemeData(variant: StyleVariant.material3Expressive); + + expect(data.variant, StyleVariant.material3Expressive); + }); + + test('IconButtonThemeData variant defaults to null', () { + const data = IconButtonThemeData(); + + expect(data.variant, isNull); + }); + + testWidgets('IconButton supports Material 3 Expressive style variants', ( + WidgetTester tester, + ) async { + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(useMaterial3: true), + home: const Scaffold( + body: IconButtonTheme( + data: IconButtonThemeData(variant: StyleVariant.material3Expressive), + child: IconButton(onPressed: null, icon: Icon(Icons.ac_unit)), + ), + ), + ), + ); + + expect(tester.takeException(), isNull); + }); + testWidgets('Passing no IconButtonTheme returns defaults', (WidgetTester tester) async { const colorScheme = ColorScheme.light(); await tester.pumpWidget( diff --git a/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart b/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart index c7ab01c7e138..23a3c9c2f4cb 100644 --- a/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart +++ b/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart @@ -30,7 +30,8 @@ import '../templates/app_bar_template.dart'; // import '../templates/expansion_tile_template.dart'; // import '../templates/fab_template.dart'; // import '../templates/filter_chip_template.dart'; -// import '../templates/icon_button_template.dart'; +import '../templates/icon_button_template.dart'; + // import '../templates/input_chip_template.dart'; // import '../templates/input_decorator_template.dart'; // import '../templates/list_tile_template.dart'; @@ -81,7 +82,7 @@ Future main(List args) async { // const ExpansionTileTemplateM3().generateFile(verbose: verbose); // const FabTemplateM3().generateFile(verbose: verbose); // const FilterChipTemplateM3().generateFile(verbose: verbose); - // const IconButtonTemplateM3().generateFile(verbose: verbose); + const IconButtonTemplate().generateFile(verbose: verbose); // const InputChipTemplateM3().generateFile(verbose: verbose); // const InputDecoratorTemplateM3().generateFile(verbose: verbose); // const ListTileTemplateM3().generateFile(verbose: verbose); diff --git a/packages/material_ui/tool/gen_defaults/templates/icon_button_template.dart b/packages/material_ui/tool/gen_defaults/templates/icon_button_template.dart new file mode 100644 index 000000000000..962491931571 --- /dev/null +++ b/packages/material_ui/tool/gen_defaults/templates/icon_button_template.dart @@ -0,0 +1,771 @@ +// Copyright 2013 The Flutter Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import '../data/color_role.dart'; +import '../data/icon_button_filled.dart'; +import '../data/icon_button_large.dart'; +import '../data/icon_button_medium.dart'; +import '../data/icon_button_outlined.dart'; +import '../data/icon_button_small.dart'; +import '../data/icon_button_standard.dart'; +import '../data/icon_button_tonal.dart'; +import '../data/icon_button_xlarge.dart'; +import '../data/icon_button_xsmall.dart'; +import 'template.dart'; + +class IconButtonTemplate extends TokenTemplateM3E { + const IconButtonTemplate(); + + @override + String get name => 'Icon Button'; + + @override + String get parentFilePath => 'icon_button.dart'; + + String tokenColor(TokenColorRole role) { + return switch (role) { + TokenColorRole.inverseOnSurface => '_colors.onInverseSurface', + _ => color(role, '_colors'), + }; + } + + String componentColor(TokenColorRole role, double opacity) { + final String value = tokenColor(role); + if (opacity == 1.0) { + return value; + } + return '$value.withOpacity($opacity)'; + } + + @override + String generateContents(String className) { + return ''' +${_generateStandardDefaults(className)} +${_generateFilledDefaults()} +${_generateFilledTonalDefaults()} +${_generateOutlinedDefaults()} +'''; + } + + String _sizeSwitch({ + required String xSmall, + required String small, + required String medium, + required String large, + required String xLarge, + }) { + return ''' +switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => $xSmall, + ButtonSize.small => $small, + ButtonSize.medium => $medium, + ButtonSize.large => $large, + ButtonSize.xLarge => $xLarge, + }'''; + } + + String get _paddingSwitch { + return _sizeSwitch( + xSmall: _edgeInsetsSwitch( + defaultLeading: TokenIconButtonXsmall.defaultLeadingSpace, + defaultTrailing: TokenIconButtonXsmall.defaultTrailingSpace, + narrowLeading: TokenIconButtonXsmall.narrowLeadingSpace, + narrowTrailing: TokenIconButtonXsmall.narrowTrailingSpace, + wideLeading: TokenIconButtonXsmall.wideLeadingSpace, + wideTrailing: TokenIconButtonXsmall.wideTrailingSpace, + ), + small: _edgeInsetsSwitch( + defaultLeading: TokenIconButtonSmall.defaultLeadingSpace, + defaultTrailing: TokenIconButtonSmall.defaultTrailingSpace, + narrowLeading: TokenIconButtonSmall.narrowLeadingSpace, + narrowTrailing: TokenIconButtonSmall.narrowTrailingSpace, + wideLeading: TokenIconButtonSmall.wideLeadingSpace, + wideTrailing: TokenIconButtonSmall.wideTrailingSpace, + ), + medium: _edgeInsetsSwitch( + defaultLeading: TokenIconButtonMedium.defaultLeadingSpace, + defaultTrailing: TokenIconButtonMedium.defaultTrailingSpace, + narrowLeading: TokenIconButtonMedium.narrowLeadingSpace, + narrowTrailing: TokenIconButtonMedium.narrowTrailingSpace, + wideLeading: TokenIconButtonMedium.wideLeadingSpace, + wideTrailing: TokenIconButtonMedium.wideTrailingSpace, + ), + large: _edgeInsetsSwitch( + defaultLeading: TokenIconButtonLarge.defaultLeadingSpace, + defaultTrailing: TokenIconButtonLarge.defaultTrailingSpace, + narrowLeading: TokenIconButtonLarge.narrowLeadingSpace, + narrowTrailing: TokenIconButtonLarge.narrowTrailingSpace, + wideLeading: TokenIconButtonLarge.wideLeadingSpace, + wideTrailing: TokenIconButtonLarge.wideTrailingSpace, + ), + xLarge: _edgeInsetsSwitch( + defaultLeading: TokenIconButtonXlarge.defaultLeadingSpace, + defaultTrailing: TokenIconButtonXlarge.defaultTrailingSpace, + narrowLeading: TokenIconButtonXlarge.narrowLeadingSpace, + narrowTrailing: TokenIconButtonXlarge.narrowTrailingSpace, + wideLeading: TokenIconButtonXlarge.wideLeadingSpace, + wideTrailing: TokenIconButtonXlarge.wideTrailingSpace, + ), + ); + } + + String _edgeInsetsSwitch({ + required double defaultLeading, + required double defaultTrailing, + required double narrowLeading, + required double narrowTrailing, + required double wideLeading, + required double wideTrailing, + }) { + return ''' +switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const EdgeInsetsDirectional.fromSTEB($narrowLeading, $defaultLeading, $narrowTrailing, $defaultTrailing), + IconButtonWidth.standard => const EdgeInsetsDirectional.fromSTEB($defaultLeading, $defaultLeading, $defaultTrailing, $defaultTrailing), + IconButtonWidth.wide => const EdgeInsetsDirectional.fromSTEB($wideLeading, $defaultLeading, $wideTrailing, $defaultTrailing), + }'''; + } + + String get _minimumSizeSwitch { + return _sizeSwitch( + xSmall: _minimumSizeWidthSwitch( + iconSize: TokenIconButtonXsmall.iconSize, + height: TokenIconButtonXsmall.containerHeight, + defaultLeading: TokenIconButtonXsmall.defaultLeadingSpace, + defaultTrailing: TokenIconButtonXsmall.defaultTrailingSpace, + narrowLeading: TokenIconButtonXsmall.narrowLeadingSpace, + narrowTrailing: TokenIconButtonXsmall.narrowTrailingSpace, + wideLeading: TokenIconButtonXsmall.wideLeadingSpace, + wideTrailing: TokenIconButtonXsmall.wideTrailingSpace, + ), + small: _minimumSizeWidthSwitch( + iconSize: TokenIconButtonSmall.iconSize, + height: TokenIconButtonSmall.containerHeight, + defaultLeading: TokenIconButtonSmall.defaultLeadingSpace, + defaultTrailing: TokenIconButtonSmall.defaultTrailingSpace, + narrowLeading: TokenIconButtonSmall.narrowLeadingSpace, + narrowTrailing: TokenIconButtonSmall.narrowTrailingSpace, + wideLeading: TokenIconButtonSmall.wideLeadingSpace, + wideTrailing: TokenIconButtonSmall.wideTrailingSpace, + ), + medium: _minimumSizeWidthSwitch( + iconSize: TokenIconButtonMedium.iconSize, + height: TokenIconButtonMedium.containerHeight, + defaultLeading: TokenIconButtonMedium.defaultLeadingSpace, + defaultTrailing: TokenIconButtonMedium.defaultTrailingSpace, + narrowLeading: TokenIconButtonMedium.narrowLeadingSpace, + narrowTrailing: TokenIconButtonMedium.narrowTrailingSpace, + wideLeading: TokenIconButtonMedium.wideLeadingSpace, + wideTrailing: TokenIconButtonMedium.wideTrailingSpace, + ), + large: _minimumSizeWidthSwitch( + iconSize: TokenIconButtonLarge.iconSize, + height: TokenIconButtonLarge.containerHeight, + defaultLeading: TokenIconButtonLarge.defaultLeadingSpace, + defaultTrailing: TokenIconButtonLarge.defaultTrailingSpace, + narrowLeading: TokenIconButtonLarge.narrowLeadingSpace, + narrowTrailing: TokenIconButtonLarge.narrowTrailingSpace, + wideLeading: TokenIconButtonLarge.wideLeadingSpace, + wideTrailing: TokenIconButtonLarge.wideTrailingSpace, + ), + xLarge: _minimumSizeWidthSwitch( + iconSize: TokenIconButtonXlarge.iconSize, + height: TokenIconButtonXlarge.containerHeight, + defaultLeading: TokenIconButtonXlarge.defaultLeadingSpace, + defaultTrailing: TokenIconButtonXlarge.defaultTrailingSpace, + narrowLeading: TokenIconButtonXlarge.narrowLeadingSpace, + narrowTrailing: TokenIconButtonXlarge.narrowTrailingSpace, + wideLeading: TokenIconButtonXlarge.wideLeadingSpace, + wideTrailing: TokenIconButtonXlarge.wideTrailingSpace, + ), + ); + } + + String _minimumSizeWidthSwitch({ + required double iconSize, + required double height, + required double defaultLeading, + required double defaultTrailing, + required double narrowLeading, + required double narrowTrailing, + required double wideLeading, + required double wideTrailing, + }) { + return ''' +switch (buttonWidth ?? IconButtonWidth.standard) { + IconButtonWidth.narrow => const Size(${iconSize + narrowLeading + narrowTrailing}, $height), + IconButtonWidth.standard => const Size(${iconSize + defaultLeading + defaultTrailing}, $height), + IconButtonWidth.wide => const Size(${iconSize + wideLeading + wideTrailing}, $height), + }'''; + } + + String get _iconSizeSwitch { + return _sizeSwitch( + xSmall: '${TokenIconButtonXsmall.iconSize}', + small: '${TokenIconButtonSmall.iconSize}', + medium: '${TokenIconButtonMedium.iconSize}', + large: '${TokenIconButtonLarge.iconSize}', + xLarge: '${TokenIconButtonXlarge.iconSize}', + ); + } + + String get _outlineWidthSwitch { + return _sizeSwitch( + xSmall: '${TokenIconButtonXsmall.outlinedOutlineWidth}', + small: '${TokenIconButtonSmall.outlinedOutlineWidth}', + medium: '${TokenIconButtonMedium.outlinedOutlineWidth}', + large: '${TokenIconButtonLarge.outlinedOutlineWidth}', + xLarge: '${TokenIconButtonXlarge.outlinedOutlineWidth}', + ); + } + + String get _containerRoundShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.containerShapeRound), + small: shape(TokenIconButtonSmall.containerShapeRound), + medium: shape(TokenIconButtonMedium.containerShapeRound), + large: shape(TokenIconButtonLarge.containerShapeRound), + xLarge: shape(TokenIconButtonXlarge.containerShapeRound), + ); + } + + String get _containerSquareShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.containerShapeSquare), + small: shape(TokenIconButtonSmall.containerShapeSquare), + medium: shape(TokenIconButtonMedium.containerShapeSquare), + large: shape(TokenIconButtonLarge.containerShapeSquare), + xLarge: shape(TokenIconButtonXlarge.containerShapeSquare), + ); + } + + String get _containerShapeSwitch { + return ''' +switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => $_containerRoundShapeSwitch, + IconButtonShape.square => $_containerSquareShapeSwitch, + }'''; + } + + String get _pressedShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.pressedContainerShape), + small: shape(TokenIconButtonSmall.pressedContainerShape), + medium: shape(TokenIconButtonMedium.pressedContainerShape), + large: shape(TokenIconButtonLarge.pressedContainerShape), + xLarge: shape(TokenIconButtonXlarge.pressedContainerShape), + ); + } + + String get _selectedRoundShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.selectedContainerShapeRound), + small: shape(TokenIconButtonSmall.selectedContainerShapeRound), + medium: shape(TokenIconButtonMedium.selectedContainerShapeRound), + large: shape(TokenIconButtonLarge.selectedContainerShapeRound), + xLarge: shape(TokenIconButtonXlarge.selectedContainerShapeRound), + ); + } + + String get _selectedSquareShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.selectedContainerShapeSquare), + small: shape(TokenIconButtonSmall.selectedContainerShapeSquare), + medium: shape(TokenIconButtonMedium.selectedContainerShapeSquare), + large: shape(TokenIconButtonLarge.selectedContainerShapeSquare), + xLarge: shape(TokenIconButtonXlarge.selectedContainerShapeSquare), + ); + } + + String get _selectedShapeSwitch { + return ''' +switch (buttonShape ?? IconButtonShape.round) { + IconButtonShape.round => $_selectedRoundShapeSwitch, + IconButtonShape.square => $_selectedSquareShapeSwitch, + }'''; + } + + String get _sizeDependentProperties { + return ''' + @override + WidgetStateProperty? get padding => + MaterialStatePropertyAll($_paddingSwitch); + + @override + WidgetStateProperty? get minimumSize => + MaterialStatePropertyAll($_minimumSizeSwitch); + + @override + WidgetStateProperty? get maximumSize => + const MaterialStatePropertyAll(Size.infinite); + + @override + WidgetStateProperty? get iconSize => + MaterialStatePropertyAll($_iconSizeSwitch); + + @override + WidgetStateProperty? get shape => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.pressed)) { + return $_pressedShapeSwitch; + } + if (toggleable && states.contains(WidgetState.selected)) { + return $_selectedShapeSwitch; + } + return $_containerShapeSwitch; + }); +'''; + } + + String _generateStandardDefaults(String className) { + return ''' +class $className extends ButtonStyle { + $className(this.context, this.toggleable, this.buttonSize, this.buttonWidth, this.buttonShape) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonStandard.disabledIconColor, TokenIconButtonStandard.disabledIconOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonStandard.selectedIconColor)}; + } + return ${tokenColor(TokenIconButtonStandard.iconColor)}; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonStandard.selectedPressedStateLayerColor, TokenIconButtonStandard.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonStandard.selectedHoveredStateLayerColor, TokenIconButtonStandard.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonStandard.selectedFocusedStateLayerColor, TokenIconButtonStandard.focusedStateLayerOpacity)}; + } + } + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonStandard.pressedStateLayerColor, TokenIconButtonStandard.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonStandard.hoveredStateLayerColor, TokenIconButtonStandard.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonStandard.focusedStateLayerColor, TokenIconButtonStandard.focusedStateLayerOpacity)}; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => + const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + +$_sizeDependentProperties + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} +'''; + } + + String _generateFilledDefaults() { + return ''' +class _M3EFilledIconButtonDefaults extends ButtonStyle { + _M3EFilledIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonFilled.disabledContainerColor, TokenIconButtonFilled.disabledContainerOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonFilled.selectedContainerColor)}; + } + if (toggleable) { + return ${tokenColor(TokenIconButtonFilled.unselectedContainerColor)}; + } + return ${tokenColor(TokenIconButtonFilled.containerColor)}; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonFilled.disabledIconColor, TokenIconButtonFilled.disabledIconOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonFilled.selectedIconColor)}; + } + if (toggleable) { + return ${tokenColor(TokenIconButtonFilled.unselectedIconColor)}; + } + return ${tokenColor(TokenIconButtonFilled.iconColor)}; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonFilled.selectedPressedStateLayerColor, TokenIconButtonFilled.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonFilled.selectedHoveredStateLayerColor, TokenIconButtonFilled.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonFilled.selectedFocusedStateLayerColor, TokenIconButtonFilled.focusedStateLayerOpacity)}; + } + } + if (toggleable) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonFilled.unselectedPressedStateLayerColor, TokenIconButtonFilled.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonFilled.unselectedHoveredStateLayerColor, TokenIconButtonFilled.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonFilled.unselectedFocusedStateLayerColor, TokenIconButtonFilled.focusedStateLayerOpacity)}; + } + } + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonFilled.pressedStateLayerColor, TokenIconButtonFilled.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonFilled.hoveredStateLayerColor, TokenIconButtonFilled.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonFilled.focusedStateLayerColor, TokenIconButtonFilled.focusedStateLayerOpacity)}; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => + const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + +$_sizeDependentProperties + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} +'''; + } + + String _generateFilledTonalDefaults() { + return ''' +class _M3EFilledTonalIconButtonDefaults extends ButtonStyle { + _M3EFilledTonalIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonTonal.disabledContainerColor, TokenIconButtonTonal.disabledContainerOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonTonal.selectedContainerColor)}; + } + if (toggleable) { + return ${tokenColor(TokenIconButtonTonal.unselectedContainerColor)}; + } + return ${tokenColor(TokenIconButtonTonal.containerColor)}; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonTonal.disabledIconColor, TokenIconButtonTonal.disabledIconOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonTonal.selectedIconColor)}; + } + if (toggleable) { + return ${tokenColor(TokenIconButtonTonal.unselectedIconColor)}; + } + return ${tokenColor(TokenIconButtonTonal.iconColor)}; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonTonal.selectedPressedStateLayerColor, TokenIconButtonTonal.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonTonal.selectedHoveredStateLayerColor, TokenIconButtonTonal.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonTonal.selectedFocusedStateLayerColor, TokenIconButtonTonal.focusedStateLayerOpacity)}; + } + } + if (toggleable) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonTonal.unselectedPressedStateLayerColor, TokenIconButtonTonal.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonTonal.unselectedHoveredStateLayerColor, TokenIconButtonTonal.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonTonal.unselectedFocusedStateLayerColor, TokenIconButtonTonal.focusedStateLayerOpacity)}; + } + } + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonTonal.pressedStateLayerColor, TokenIconButtonTonal.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonTonal.hoveredStateLayerColor, TokenIconButtonTonal.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonTonal.focusedStateLayerColor, TokenIconButtonTonal.focusedStateLayerOpacity)}; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => + const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + +$_sizeDependentProperties + + @override + WidgetStateProperty? get side => null; + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} +'''; + } + + String _generateOutlinedDefaults() { + return ''' +class _M3EOutlinedIconButtonDefaults extends ButtonStyle { + _M3EOutlinedIconButtonDefaults( + this.context, + this.toggleable, + this.buttonSize, + this.buttonWidth, + this.buttonShape, + ) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + final IconButtonShape? buttonShape; + late final ColorScheme _colors = Theme.of(context).colorScheme; + + @override + WidgetStateProperty? get backgroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + if (toggleable && states.contains(WidgetState.selected)) { + return ${componentColor(TokenIconButtonOutlined.selectedDisabledContainerColor, TokenIconButtonOutlined.selectedDisabledContainerOpacity)}; + } + return Colors.transparent; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonOutlined.selectedContainerColor)}; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get foregroundColor => + WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return ${componentColor(TokenIconButtonOutlined.disabledIconColor, TokenIconButtonOutlined.disabledIconOpacity)}; + } + if (toggleable && states.contains(WidgetState.selected)) { + return ${tokenColor(TokenIconButtonOutlined.selectedIconColor)}; + } + return ${tokenColor(TokenIconButtonOutlined.iconColor)}; + }); + + @override + WidgetStateProperty? get overlayColor => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonOutlined.selectedPressedStateLayerColor, TokenIconButtonOutlined.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonOutlined.selectedHoveredStateLayerColor, TokenIconButtonOutlined.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonOutlined.selectedFocusedStateLayerColor, TokenIconButtonOutlined.focusedStateLayerOpacity)}; + } + } + if (states.contains(WidgetState.pressed)) { + return ${componentColor(TokenIconButtonOutlined.pressedStateLayerColor, TokenIconButtonOutlined.pressedStateLayerOpacity)}; + } + if (states.contains(WidgetState.hovered)) { + return ${componentColor(TokenIconButtonOutlined.hoveredStateLayerColor, TokenIconButtonOutlined.hoveredStateLayerOpacity)}; + } + if (states.contains(WidgetState.focused)) { + return ${componentColor(TokenIconButtonOutlined.focusedStateLayerColor, TokenIconButtonOutlined.focusedStateLayerOpacity)}; + } + return Colors.transparent; + }); + + @override + WidgetStateProperty? get elevation => + const MaterialStatePropertyAll(0.0); + + @override + WidgetStateProperty? get shadowColor => + const MaterialStatePropertyAll(Colors.transparent); + + @override + WidgetStateProperty? get surfaceTintColor => + const MaterialStatePropertyAll(Colors.transparent); + +$_sizeDependentProperties + + @override + WidgetStateProperty? get side => + WidgetStateProperty.resolveWith((Set states) { + if (toggleable && states.contains(WidgetState.selected)) { + return null; + } + if (states.contains(WidgetState.disabled)) { + return BorderSide(color: ${tokenColor(TokenIconButtonOutlined.unselectedDisabledOutlineColor)}, width: $_outlineWidthSwitch); + } + return BorderSide(color: ${tokenColor(TokenIconButtonOutlined.outlineColor)}, width: $_outlineWidthSwitch); + }); + + @override + WidgetStateProperty? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable; + + @override + VisualDensity? get visualDensity => VisualDensity.standard; + + @override + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; + + @override + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; +} +'''; + } +}