elevatedButtonTheme static method

ElevatedButtonThemeData elevatedButtonTheme({
  1. required ColorScheme colorScheme,
  2. double? radius,
  3. double elevation = kElevatedButtonElevation,
  4. EdgeInsetsGeometry? padding,
  5. Size minButtonSize = kButtonMinSize,
})

An opinionated ElevatedButtonThemeData theme.

Requires a ColorScheme in colorscheme. The color scheme would typically be equal the color scheme also used to define the color scheme for your app theme.

The button elevation defaults to 1 kElevatedButtonElevation, making the elevated button a bit more flat. Flutter SDK ElevatedButton defaults to elevation 2.

The adjustable button corner radius defaults to 20. This is the new default in M3, Flutter SDK M2 defaults to 4.

Implementation

static ElevatedButtonThemeData elevatedButtonTheme({
  /// Typically the same `ColorScheme` that is also used for your `ThemeData`.
  required final ColorScheme colorScheme,

  /// The button corner radius.
  ///
  /// Defaults to 20 [kButtonRadius].
  final double? radius,

  /// The button elevation
  ///
  /// Defaults to [kElevatedButtonElevation] 1, making it a bit more flat in
  /// its elevation state than Flutter SDK, that defaults to 2.
  /// An opinionated choice.
  final double elevation = kElevatedButtonElevation,

  /// Padding for the button theme.
  ///
  /// Defaults to null and uses `styleFrom` constructors default padding.
  ///
  /// M3 has more horizontal padding 24dp, but the tighter default padding
  /// in M2 that is 16dp looks fine as well when using stadium borders
  /// as in M3.
  /// Making the custom scalable padding and separate one for icon
  /// versions is rather involved, so sticking to defaults, but exposing the
  /// padding property for future or external use.
  final EdgeInsetsGeometry? padding,

  /// Minimum button size.
  ///
  /// Defaults to `kButtonMinSize` = Size(40, 40).
  final Size minButtonSize = kButtonMinSize,
}) =>
    ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        minimumSize: minButtonSize,
        padding: padding,
        elevation: elevation,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(radius ?? kButtonRadius),
          ),
        ), //buttonShape,
      ).copyWith(
        foregroundColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled)) {
              return colorScheme.primary
                  .blendAlpha(colorScheme.onSurface, kDisabledAlphaBlend)
                  .withAlpha(kDisabledForegroundAlpha);
            }
            return colorScheme.onPrimary;
          },
        ),
        backgroundColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled)) {
              return colorScheme.primary
                  .blendAlpha(colorScheme.onSurface, kDisabledAlphaBlend)
                  .withAlpha(kDisabledBackgroundAlpha);
            }
            return colorScheme.primary;
          },
        ),
        overlayColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.hovered)) {
              return colorScheme.onPrimary.withAlpha(kHoverBackgroundAlpha);
            }
            if (states.contains(MaterialState.focused)) {
              return colorScheme.onPrimary.withAlpha(kFocusBackgroundAlpha);
            }
            if (states.contains(MaterialState.pressed)) {
              return colorScheme.onPrimary.withAlpha(kPressedBackgroundAlpha);
            }
            return Colors.transparent;
          },
        ),
      ),
    );