From 4b56b94d167da0c8ad4ae182849b95533e025d79 Mon Sep 17 00:00:00 2001 From: Qun Cheng Date: Thu, 28 May 2026 17:24:05 -0700 Subject: [PATCH] Add M3E IconButton --- .../material_ui/lib/src/button_style.dart | 58 +- .../generated/icon_button_m3e_defaults.g.dart | 924 ++++++++++++++++++ packages/material_ui/lib/src/icon_button.dart | 73 +- .../lib/src/icon_button_theme.dart | 15 +- .../material_ui/test/icon_button_test.dart | 648 ++++++++++++ .../test/icon_button_theme_test.dart | 24 + .../material_ui/test/theme_data_test.dart | 106 +- .../tool/gen_defaults/bin/gen_defaults.dart | 5 +- .../templates/icon_button_template.dart | 713 ++++++++++++++ 9 files changed, 2550 insertions(+), 16 deletions(-) create mode 100644 packages/material_ui/lib/src/generated/icon_button_m3e_defaults.g.dart create mode 100644 packages/material_ui/tool/gen_defaults/templates/icon_button_template.dart diff --git a/packages/material_ui/lib/src/button_style.dart b/packages/material_ui/lib/src/button_style.dart index e251296f42ed..8409323a94e3 100644 --- a/packages/material_ui/lib/src/button_style.dart +++ b/packages/material_ui/lib/src/button_style.dart @@ -31,6 +31,38 @@ 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, +} + /// 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 +219,8 @@ class ButtonStyle with Diagnosticable { this.splashFactory, this.backgroundBuilder, this.foregroundBuilder, + this.size, + this.iconButtonWidth, }); /// The style for a button's [Text] widget descendants. @@ -423,6 +457,12 @@ 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; + /// Returns a copy of this ButtonStyle with the given fields replaced with /// the new values. ButtonStyle copyWith({ @@ -451,6 +491,8 @@ class ButtonStyle with Diagnosticable { InteractiveInkFeatureFactory? splashFactory, ButtonLayerBuilder? backgroundBuilder, ButtonLayerBuilder? foregroundBuilder, + ButtonSize? size, + IconButtonWidth? iconButtonWidth, }) { return ButtonStyle( textStyle: textStyle ?? this.textStyle, @@ -478,6 +520,8 @@ class ButtonStyle with Diagnosticable { splashFactory: splashFactory ?? this.splashFactory, backgroundBuilder: backgroundBuilder ?? this.backgroundBuilder, foregroundBuilder: foregroundBuilder ?? this.foregroundBuilder, + size: size ?? this.size, + iconButtonWidth: iconButtonWidth ?? this.iconButtonWidth, ); } @@ -516,6 +560,8 @@ class ButtonStyle with Diagnosticable { splashFactory: splashFactory ?? style.splashFactory, backgroundBuilder: backgroundBuilder ?? style.backgroundBuilder, foregroundBuilder: foregroundBuilder ?? style.foregroundBuilder, + size: size ?? style.size, + iconButtonWidth: iconButtonWidth ?? style.iconButtonWidth, ); } @@ -547,6 +593,8 @@ class ButtonStyle with Diagnosticable { splashFactory, backgroundBuilder, foregroundBuilder, + size, + iconButtonWidth, ]; return Object.hashAll(values); } @@ -584,7 +632,9 @@ 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; } @override @@ -706,6 +756,10 @@ class ButtonStyle with Diagnosticable { defaultValue: null, ), ); + properties.add(EnumProperty('size', size, defaultValue: null)); + properties.add( + EnumProperty('iconButtonWidth', iconButtonWidth, defaultValue: null), + ); } /// Linearly interpolate between two [ButtonStyle]s. @@ -769,6 +823,8 @@ 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, ); } } diff --git a/packages/material_ui/lib/src/generated/icon_button_m3e_defaults.g.dart b/packages/material_ui/lib/src/generated/icon_button_m3e_defaults.g.dart new file mode 100644 index 000000000000..b1e109b2fa88 --- /dev/null +++ b/packages/material_ui/lib/src/generated/icon_button_m3e_defaults.g.dart @@ -0,0 +1,924 @@ +// 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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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 (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)), + ), + }; + } + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }; + }); + + @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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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 (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)), + ), + }; + } + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }; + }); + + @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, + ) : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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 (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)), + ), + }; + } + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }; + }); + + @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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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 (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)), + ), + }; + } + return switch (buttonSize ?? ButtonSize.small) { + ButtonSize.xSmall => const StadiumBorder(), + ButtonSize.small => const StadiumBorder(), + ButtonSize.medium => const StadiumBorder(), + ButtonSize.large => const StadiumBorder(), + ButtonSize.xLarge => const StadiumBorder(), + }; + }); + + @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..bce6969bd428 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_m3e_defaults.g.dart'; + // Examples can assume: // late BuildContext context; @@ -626,6 +628,10 @@ class IconButton extends StatelessWidget { /// create a [WidgetStateProperty] with a single value for all /// states. /// + /// The [size] and [iconButtonWidth] parameters configure the Material 3 + /// Expressive token size and width through [ButtonStyle.size] and + /// [ButtonStyle.iconButtonWidth]. + /// /// All parameters default to null, by default this method returns /// a [ButtonStyle] that doesn't override anything. /// @@ -670,6 +676,8 @@ class IconButton extends StatelessWidget { bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory, + ButtonSize? size, + IconButtonWidth? iconButtonWidth, }) { final Color? overlayFallback = overlayColor ?? foregroundColor; WidgetStateProperty? overlayColorProp; @@ -710,6 +718,8 @@ class IconButton extends StatelessWidget { enableFeedback: enableFeedback, alignment: alignment, splashFactory: splashFactory, + size: size, + iconButtonWidth: iconButtonWidth, ); } @@ -718,6 +728,8 @@ class IconButton extends StatelessWidget { final ThemeData theme = Theme.of(context); if (theme.useMaterial3) { + final StyleVariant effectiveVariant = IconButtonTheme.of(context).variant ?? theme.variant; + final Size? minSize = constraints == null ? null : Size(constraints!.minWidth, constraints!.minHeight); @@ -761,6 +773,7 @@ class IconButton extends StatelessWidget { focusNode: focusNode, isSelected: isSelected, variant: _variant, + styleVariant: effectiveVariant, tooltip: tooltip, statesController: statesController, child: effectiveIcon, @@ -867,6 +880,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 +891,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 +960,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 +984,25 @@ 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; + } + /// ## Material 3 defaults /// /// If [ThemeData.useMaterial3] is set to true the following defaults will @@ -1014,11 +1040,42 @@ 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); + + 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, + ), + _IconButtonVariant.filledTonal => _M3EFilledTonalIconButtonDefaults( + context, + toggleable, + effectiveSize, + effectiveWidth, + ), + _IconButtonVariant.outlined => _M3EOutlinedIconButtonDefaults( + context, + toggleable, + effectiveSize, + effectiveWidth, + ), + _IconButtonVariant.standard => _IconButtonDefaultsM3E( + context, + toggleable, + effectiveSize, + effectiveWidth, + ), + }, }; } diff --git a/packages/material_ui/lib/src/icon_button_theme.dart b/packages/material_ui/lib/src/icon_button_theme.dart index 0d38686dba0f..7f8713fc79d0 100644 --- a/packages/material_ui/lib/src/icon_button_theme.dart +++ b/packages/material_ui/lib/src/icon_button_theme.dart @@ -39,7 +39,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 +50,22 @@ 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]. + 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 +75,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..8679d7e8e1f1 100644 --- a/packages/material_ui/test/icon_button_test.dart +++ b/packages/material_ui/test/icon_button_test.dart @@ -3571,6 +3571,654 @@ 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('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), + ); + const b = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.small, iconButtonWidth: IconButtonWidth.standard), + ); + const c = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.large, iconButtonWidth: IconButtonWidth.wide), + ); + + expect(a, equals(b)); + expect(a, isNot(equals(c))); + }); + + test('hashCode', () { + const a = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.small, iconButtonWidth: IconButtonWidth.narrow), + ); + const b = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.small, iconButtonWidth: IconButtonWidth.narrow), + ); + + expect(a.hashCode, equals(b.hashCode)); + }); + + test('lerp', () { + const a = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.small, iconButtonWidth: IconButtonWidth.narrow), + ); + const b = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.large, iconButtonWidth: IconButtonWidth.wide), + ); + + 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); + }); + + test('debugFillProperties includes size and width', () { + const data = IconButtonThemeData( + style: ButtonStyle(size: ButtonSize.medium, iconButtonWidth: IconButtonWidth.wide), + ); + 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'))); + }); + }); + + 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..0abc6a1957c2 100644 --- a/packages/material_ui/test/icon_button_theme_test.dart +++ b/packages/material_ui/test/icon_button_theme_test.dart @@ -20,6 +20,30 @@ 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); + }); + + 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/test/theme_data_test.dart b/packages/material_ui/test/theme_data_test.dart index ca97ec327c0f..4ec3fd5fec20 100644 --- a/packages/material_ui/test/theme_data_test.dart +++ b/packages/material_ui/test/theme_data_test.dart @@ -24,6 +24,105 @@ void main() { expect(dawn.primaryColor, Color.lerp(dark.primaryColor, light.primaryColor, 0.25)); }); + test('ThemeData defaults to Material 3 style variant', () { + expect(ThemeData().variant, StyleVariant.material3); + expect(ThemeData.light().variant, StyleVariant.material3); + expect(ThemeData.dark().variant, StyleVariant.material3); + expect(ThemeData.fallback().variant, StyleVariant.material3); + }); + + test('ThemeData asserts on unsupported style variants', () { + Matcher throwsUnsupportedStyleVariantAssertion() { + return throwsA( + isA().having( + (AssertionError error) => error.message, + 'message', + kUnsupportedStyleVariantAssertionMessage, + ), + ); + } + + expect( + () => ThemeData(variant: .material3Expressive), + throwsUnsupportedStyleVariantAssertion(), + ); + expect( + () => ThemeData.light(variant: .material3Expressive), + throwsUnsupportedStyleVariantAssertion(), + ); + expect( + () => ThemeData.dark(variant: .material3Expressive), + throwsUnsupportedStyleVariantAssertion(), + ); + expect( + () => ThemeData.fallback(variant: .material3Expressive), + throwsUnsupportedStyleVariantAssertion(), + ); + expect( + () => ThemeData.from(colorScheme: const ColorScheme.light(), variant: .material3Expressive), + throwsUnsupportedStyleVariantAssertion(), + ); + }); + + test('ThemeData.copyWith asserts on unsupported style variants', () { + final theme = ThemeData(); + + expect( + () => theme.copyWith(variant: .material3Expressive), + throwsA( + isA().having( + (AssertionError error) => error.message, + 'message', + kUnsupportedStyleVariantAssertionMessage, + ), + ), + ); + }); + + test('component theme data asserts on unsupported style variants', () { + Matcher throwsUnsupportedStyleVariantAssertion() { + return throwsA( + isA().having( + (AssertionError error) => error.message, + 'message', + kUnsupportedStyleVariantAssertionMessage, + ), + ); + } + + final constructors = [ + () => ThemeData(appBarTheme: AppBarThemeData(variant: .material3Expressive)), + () => ThemeData(elevatedButtonTheme: ElevatedButtonThemeData(variant: .material3Expressive)), + () => ThemeData(filledButtonTheme: FilledButtonThemeData(variant: .material3Expressive)), + () => ThemeData( + floatingActionButtonTheme: FloatingActionButtonThemeData(variant: .material3Expressive), + ), + () => ThemeData(menuButtonTheme: MenuButtonThemeData(variant: .material3Expressive)), + () => ThemeData(menuTheme: MenuThemeData(variant: .material3Expressive)), + () => ThemeData(navigationBarTheme: NavigationBarThemeData(variant: .material3Expressive)), + () => ThemeData(navigationRailTheme: NavigationRailThemeData(variant: .material3Expressive)), + () => ThemeData(outlinedButtonTheme: OutlinedButtonThemeData(variant: .material3Expressive)), + () => ThemeData( + progressIndicatorTheme: ProgressIndicatorThemeData(variant: .material3Expressive), + ), + () => ThemeData(searchBarTheme: SearchBarThemeData(variant: .material3Expressive)), + () => ThemeData(searchViewTheme: SearchViewThemeData(variant: .material3Expressive)), + () => ThemeData(sliderTheme: SliderThemeData(variant: .material3Expressive)), + () => ThemeData(textButtonTheme: TextButtonThemeData(variant: .material3Expressive)), + ]; + + for (final constructor in constructors) { + expect(constructor, throwsUnsupportedStyleVariantAssertion()); + } + }); + + test('ThemeData.lerp preserves style variants', () { + final material3 = ThemeData(variant: StyleVariant.material3); + + expect(ThemeData.lerp(material3, material3, 0.25).variant, StyleVariant.material3); + expect(ThemeData.lerp(material3, material3, 0.75).variant, StyleVariant.material3); + }); + test('ThemeData objects with .styleFrom() members are equal', () { ThemeData createThemeData() { return ThemeData( @@ -1290,6 +1389,7 @@ void main() { scrollbarTheme: const ScrollbarThemeData(radius: Radius.circular(10.0)), splashFactory: InkRipple.splashFactory, useMaterial3: false, + variant: StyleVariant.material3, visualDensity: VisualDensity.standard, // COLOR canvasColor: Colors.black, @@ -1419,6 +1519,7 @@ void main() { scrollbarTheme: const ScrollbarThemeData(radius: Radius.circular(10.0)), splashFactory: InkRipple.splashFactory, useMaterial3: true, + variant: StyleVariant.material3, visualDensity: VisualDensity.standard, // COLOR canvasColor: Colors.white, @@ -1523,6 +1624,7 @@ void main() { scrollbarTheme: otherTheme.scrollbarTheme, splashFactory: otherTheme.splashFactory, useMaterial3: otherTheme.useMaterial3, + variant: otherTheme.variant, visualDensity: otherTheme.visualDensity, // COLOR canvasColor: otherTheme.canvasColor, @@ -1615,6 +1717,7 @@ void main() { expect(themeDataCopy.scrollbarTheme, equals(otherTheme.scrollbarTheme)); expect(themeDataCopy.splashFactory, equals(otherTheme.splashFactory)); expect(themeDataCopy.useMaterial3, equals(otherTheme.useMaterial3)); + expect(themeDataCopy.variant, equals(otherTheme.variant)); expect(themeDataCopy.visualDensity, equals(otherTheme.visualDensity)); // COLOR expect(themeDataCopy.canvasColor, equals(otherTheme.canvasColor)); @@ -1761,8 +1864,9 @@ void main() { 'platform', 'scrollbarTheme', 'splashFactory', - 'visualDensity', 'useMaterial3', + 'variant', + 'visualDensity', // COLOR 'colorScheme', 'primaryColor', 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 2da295bc4e99..38c5fdd7b8aa 100644 --- a/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart +++ b/packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart @@ -12,6 +12,8 @@ import 'package:args/args.dart'; +import '../templates/icon_button_template.dart'; + // TODO(elliette): Import template files. // import '../templates/x_template.dart'; @@ -23,6 +25,5 @@ Future main(List args) async { // TODO(elliette): Add token logger when verbose flag is used. // ignore: unused_local_variable final verbose = argResults['verbose'] as bool; - // TODO(elliette): Invoke template generators. - // const XTemplate().generateFile(verbose: verbose); + const IconButtonTemplate().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..53d54acdf52d --- /dev/null +++ b/packages/material_ui/tool/gen_defaults/templates/icon_button_template.dart @@ -0,0 +1,713 @@ +// 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 M3ETokenTemplate { + 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 _containerShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.containerShapeRound), + small: shape(TokenIconButtonSmall.containerShapeRound), + medium: shape(TokenIconButtonMedium.containerShapeRound), + large: shape(TokenIconButtonLarge.containerShapeRound), + xLarge: shape(TokenIconButtonXlarge.containerShapeRound), + ); + } + + 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 _selectedShapeSwitch { + return _sizeSwitch( + xSmall: shape(TokenIconButtonXsmall.selectedContainerShapeRound), + small: shape(TokenIconButtonSmall.selectedContainerShapeRound), + medium: shape(TokenIconButtonMedium.selectedContainerShapeRound), + large: shape(TokenIconButtonLarge.selectedContainerShapeRound), + xLarge: shape(TokenIconButtonXlarge.selectedContainerShapeRound), + ); + } + + 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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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) + : super( + animationDuration: kThemeChangeDuration, + enableFeedback: true, + alignment: Alignment.center, + ); + + final BuildContext context; + final bool toggleable; + final ButtonSize? buttonSize; + final IconButtonWidth? buttonWidth; + 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; +} +'''; + } +}