defaultStyleOf method

  1. @override
ButtonStyle defaultStyleOf(
  1. BuildContext context
)
override

Defines the button's default appearance.

The button child's Text and Icon widgets are rendered with the ButtonStyle's foreground color. The button's InkWell adds the style's overlay color when the button is focused, hovered or pressed. The button's background color becomes its Material color.

All of the ButtonStyle's defaults appear below. In this list "Theme.foo" is shorthand for Theme.of(context).foo. Color scheme values like "onSurface(0.38)" are shorthand for onSurface.withOpacity(0.38). WidgetStateProperty valued properties that are not followed by a sublist have the same value for all states, otherwise the values are as specified for each state, and "others" means all other states.

The "default font size" below refers to the font size specified in the defaultStyleOf method (or 14.0 if unspecified), scaled by the MediaQuery.textScalerOf(context).scale method. The names of the EdgeInsets constructors and EdgeInsetsGeometry.lerp have been abbreviated for readability.

The color of the ButtonStyle.textStyle is not used, the ButtonStyle.foregroundColor color is used instead.

Material 2 defaults

  • textStyle - Theme.textTheme.button
  • backgroundColor
    • disabled - Theme.colorScheme.onSurface(0.12)
    • others - Theme.colorScheme.primary
  • foregroundColor
    • disabled - Theme.colorScheme.onSurface(0.38)
    • others - Theme.colorScheme.onPrimary
  • overlayColor
    • hovered - Theme.colorScheme.onPrimary(0.08)
    • focused or pressed - Theme.colorScheme.onPrimary(0.12)
  • shadowColor - Theme.shadowColor
  • elevation
    • disabled - 0
    • default - 2
    • hovered or focused - 4
    • pressed - 8
  • padding
    • default font size <= 14 - horizontal(16)
    • 14 < default font size <= 28 - lerp(horizontal(16), horizontal(8))
    • 28 < default font size <= 36 - lerp(horizontal(8), horizontal(4))
    • 36 < default font size - horizontal(4)
  • minimumSize - Size(64, 36)
  • fixedSize - null
  • maximumSize - Size.infinite
  • side - null
  • shape - RoundedRectangleBorder(borderRadius: BorderRadius.circular(4))
  • mouseCursor - WidgetStateMouseCursor.adaptiveClickable
  • visualDensity - theme.visualDensity
  • tapTargetSize - theme.materialTapTargetSize
  • animationDuration - kThemeChangeDuration
  • enableFeedback - true
  • alignment - Alignment.center
  • splashFactory - InkRipple.splashFactory

The default padding values for the ElevatedButton.icon factory are slightly different:

  • padding
    • default font size <= 14 - start(12) end(16)
    • 14 < default font size <= 28 - lerp(start(12) end(16), horizontal(8))
    • 28 < default font size <= 36 - lerp(horizontal(8), horizontal(4))
    • 36 < default font size - horizontal(4)

The default value for side, which defines the appearance of the button's outline, is null. That means that the outline is defined by the button shape's OutlinedBorder.side. Typically the default value of an OutlinedBorder's side is BorderSide.none, so an outline is not drawn.

Material 3 defaults

If ThemeData.useMaterial3 is set to true the following defaults will be used:

  • textStyle - Theme.textTheme.labelLarge
  • backgroundColor
    • disabled - Theme.colorScheme.onSurface(0.12)
    • others - Theme.colorScheme.surfaceContainerLow
  • foregroundColor
    • disabled - Theme.colorScheme.onSurface(0.38)
    • others - Theme.colorScheme.primary
  • overlayColor
    • hovered - Theme.colorScheme.primary(0.08)
    • focused or pressed - Theme.colorScheme.primary(0.1)
  • shadowColor - Theme.colorScheme.shadow
  • surfaceTintColor - Colors.transparent
  • elevation
    • disabled - 0
    • default - 1
    • hovered - 3
    • focused or pressed - 1
  • padding
    • default font size <= 14 - horizontal(24)
    • 14 < default font size <= 28 - lerp(horizontal(24), horizontal(12))
    • 28 < default font size <= 36 - lerp(horizontal(12), horizontal(6))
    • 36 < default font size - horizontal(6)
  • minimumSize - Size(64, 40)
  • fixedSize - null
  • maximumSize - Size.infinite
  • side - null
  • shape - StadiumBorder()
  • mouseCursor - WidgetStateMouseCursor.adaptiveClickable
  • visualDensity - Theme.visualDensity
  • tapTargetSize - Theme.materialTapTargetSize
  • animationDuration - kThemeChangeDuration
  • enableFeedback - true
  • alignment - Alignment.center
  • splashFactory - Theme.splashFactory

For the ElevatedButton.icon factory, the start (generally the left) value of ButtonStyle.padding is reduced from 24 to 16.

Material 3 Expressive defaults

If ThemeData.useMaterial3 is true and ElevatedButtonThemeData.variant is StyleVariant.material3Expressive, the following defaults will be used. Values listed per size are for ButtonSizeVariant.xSmall, ButtonSizeVariant.small, ButtonSizeVariant.medium, ButtonSizeVariant.large, and ButtonSizeVariant.xLarge respectively. The selected values only apply when isSelected is non-null.

  • textStyle - based on ButtonStyle.sizeVariant
    • xSmall or small - Theme.textTheme.labelLarge
    • medium - Theme.textTheme.titleMedium
    • large - Theme.textTheme.headlineSmall
    • xLarge - Theme.textTheme.headlineLarge
  • backgroundColor
    • disabled - Theme.colorScheme.onSurface(0.1)
    • selected - Theme.colorScheme.primary
    • others - Theme.colorScheme.surfaceContainerLow
  • foregroundColor
    • disabled - Theme.colorScheme.onSurface(0.38)
    • selected - Theme.colorScheme.onPrimary
    • others - Theme.colorScheme.primary
  • overlayColor
    • selected
      • hovered - Theme.colorScheme.onPrimary(0.08)
      • focused or pressed - Theme.colorScheme.onPrimary(0.1)
    • hovered - Theme.colorScheme.primary(0.08)
    • focused or pressed - Theme.colorScheme.primary(0.1)
  • shadowColor - Theme.colorScheme.shadow
  • surfaceTintColor - Colors.transparent
  • elevation
    • disabled - 0
    • others - 1
  • padding - based on ButtonStyle.sizeVariant; horizontal(12), horizontal(16), horizontal(24), horizontal(48), horizontal(64)
  • minimumSize - based on ButtonStyle.sizeVariant; Size(64, 32), Size(64, 40), Size(64, 56), Size(64, 96), Size(64, 136)
  • fixedSize - null
  • maximumSize - Size.infinite
  • iconSize - based on ButtonStyle.sizeVariant; 20, 20, 24, 32, 40
  • iconColor - same as foregroundColor
  • side - null
  • shape - based on ButtonStyle.sizeVariant, ButtonStyle.shapeVariant, and state
    • pressed - RoundedRectangleBorder with radius 8, 8, 12, 16, 16
    • selected
      • round - RoundedRectangleBorder with radius 12, 12, 16, 28, 28
      • square - StadiumBorder()
    • others
      • round - StadiumBorder()
      • square - RoundedRectangleBorder with radius 12, 12, 16, 28, 28
  • mouseCursor - WidgetStateMouseCursor.adaptiveClickable
  • visualDensity - VisualDensity.standard
  • tapTargetSize - MaterialTapTargetSize.padded
  • animationDuration - kThemeChangeDuration
  • enableFeedback - true
  • alignment - Alignment.center
  • splashFactory - Theme.splashFactory
  • sizeVariant - ButtonSizeVariant.small
  • shapeVariant - ButtonShapeVariant.round

For the ElevatedButton.icon factory, the padding is the same, and the gap between the icon and the label is 4, 8, 8, 12, or 16 based on ButtonStyle.sizeVariant.

Implementation

// TODO(QuncCccccc): Clean up [ThemeData.useMaterial3] once useMaterial3 is removed.
/// If [ThemeData.useMaterial3] is true and [ElevatedButtonThemeData.variant]
/// is [StyleVariant.material3Expressive], the following defaults will be
/// used. Values listed per size are for [ButtonSizeVariant.xSmall],
/// [ButtonSizeVariant.small], [ButtonSizeVariant.medium],
/// [ButtonSizeVariant.large], and [ButtonSizeVariant.xLarge] respectively.
/// The selected values only apply when [isSelected] is non-null.
///
/// * `textStyle` - based on [ButtonStyle.sizeVariant]
///   * xSmall or small - Theme.textTheme.labelLarge
///   * medium - Theme.textTheme.titleMedium
///   * large - Theme.textTheme.headlineSmall
///   * xLarge - Theme.textTheme.headlineLarge
/// * `backgroundColor`
///   * disabled - Theme.colorScheme.onSurface(0.1)
///   * selected - Theme.colorScheme.primary
///   * others - Theme.colorScheme.surfaceContainerLow
/// * `foregroundColor`
///   * disabled - Theme.colorScheme.onSurface(0.38)
///   * selected - Theme.colorScheme.onPrimary
///   * others - Theme.colorScheme.primary
/// * `overlayColor`
///   * selected
///     * hovered - Theme.colorScheme.onPrimary(0.08)
///     * focused or pressed - Theme.colorScheme.onPrimary(0.1)
///   * hovered - Theme.colorScheme.primary(0.08)
///   * focused or pressed - Theme.colorScheme.primary(0.1)
/// * `shadowColor` - Theme.colorScheme.shadow
/// * `surfaceTintColor` - Colors.transparent
/// * `elevation`
///   * disabled - 0
///   * others - 1
/// * `padding` - based on [ButtonStyle.sizeVariant]; horizontal(12),
///   horizontal(16), horizontal(24), horizontal(48), horizontal(64)
/// * `minimumSize` - based on [ButtonStyle.sizeVariant]; Size(64, 32),
///   Size(64, 40), Size(64, 56), Size(64, 96), Size(64, 136)
/// * `fixedSize` - null
/// * `maximumSize` - Size.infinite
/// * `iconSize` - based on [ButtonStyle.sizeVariant]; 20, 20, 24, 32, 40
/// * `iconColor` - same as `foregroundColor`
/// * `side` - null
/// * `shape` - based on [ButtonStyle.sizeVariant], [ButtonStyle.shapeVariant],
///   and state
///   * pressed - RoundedRectangleBorder with radius 8, 8, 12, 16, 16
///   * selected
///     * round - RoundedRectangleBorder with radius 12, 12, 16, 28, 28
///     * square - StadiumBorder()
///   * others
///     * round - StadiumBorder()
///     * square - RoundedRectangleBorder with radius 12, 12, 16, 28, 28
/// * `mouseCursor` - WidgetStateMouseCursor.adaptiveClickable
/// * `visualDensity` - VisualDensity.standard
/// * `tapTargetSize` - MaterialTapTargetSize.padded
/// * `animationDuration` - kThemeChangeDuration
/// * `enableFeedback` - true
/// * `alignment` - Alignment.center
/// * `splashFactory` - Theme.splashFactory
/// * `sizeVariant` - ButtonSizeVariant.small
/// * `shapeVariant` - ButtonShapeVariant.round
///
/// For the [ElevatedButton.icon] factory, the padding is the same, and the
/// gap between the icon and the label is 4, 8, 8, 12, or 16 based on
/// [ButtonStyle.sizeVariant].

@override
ButtonStyle defaultStyleOf(BuildContext context) {
  final ThemeData theme = Theme.of(context);
  final ColorScheme colorScheme = theme.colorScheme;
  final ElevatedButtonThemeData elevatedButtonTheme = ElevatedButtonTheme.of(context);
  final StyleVariant effectiveVariant = ElevatedButtonTheme.variantOf(context);
  final ButtonSizeVariant? effectiveSize =
      style?.sizeVariant ?? elevatedButtonTheme.style?.sizeVariant;
  final ButtonShapeVariant? effectiveShape =
      style?.shapeVariant ?? elevatedButtonTheme.style?.shapeVariant;
  final bool useExpressive =
      theme.useMaterial3 && effectiveVariant == StyleVariant.material3Expressive;
  final ButtonStyle buttonStyle = theme.useMaterial3
      ? switch (effectiveVariant) {
          StyleVariant.material3 => _ElevatedButtonDefaultsM3(context),
          StyleVariant.material3Expressive => _ElevatedButtonDefaultsM3E(
            context,
            isSelected != null,
            effectiveSize,
            effectiveShape,
          ),
        }
      : styleFrom(
          backgroundColor: colorScheme.primary,
          foregroundColor: colorScheme.onPrimary,
          disabledBackgroundColor: colorScheme.onSurface.withOpacity(0.12),
          disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38),
          shadowColor: theme.shadowColor,
          elevation: 2,
          textStyle: theme.textTheme.labelLarge,
          padding: _scaledPadding(context),
          minimumSize: const Size(64, 36),
          maximumSize: Size.infinite,
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4))),
          enabledMouseCursor: kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
          disabledMouseCursor: SystemMouseCursors.basic,
          visualDensity: theme.visualDensity,
          tapTargetSize: theme.materialTapTargetSize,
          animationDuration: kThemeChangeDuration,
          enableFeedback: true,
          alignment: Alignment.center,
          splashFactory: InkRipple.splashFactory,
        );

  // The Material 3 Expressive defaults use the same padding with or without
  // an icon.
  if (useExpressive) {
    return buttonStyle;
  }

  // Only apply padding when the ElevatedButton has an Icon.
  if (_addPadding) {
    final double defaultFontSize =
        buttonStyle.textStyle?.resolve(const <WidgetState>{})?.fontSize ?? 14.0;
    final double effectiveTextScale =
        MediaQuery.textScalerOf(context).scale(defaultFontSize) / 14.0;

    final EdgeInsetsGeometry scaledPadding = theme.useMaterial3
        ? ButtonStyleButton.scaledPadding(
            const EdgeInsetsDirectional.fromSTEB(16, 0, 24, 0),
            const EdgeInsetsDirectional.fromSTEB(8, 0, 12, 0),
            const EdgeInsetsDirectional.fromSTEB(4, 0, 6, 0),
            effectiveTextScale,
          )
        : ButtonStyleButton.scaledPadding(
            const EdgeInsetsDirectional.fromSTEB(12, 0, 16, 0),
            const EdgeInsets.symmetric(horizontal: 8),
            const EdgeInsetsDirectional.fromSTEB(8, 0, 4, 0),
            effectiveTextScale,
          );
    return buttonStyle.copyWith(
      padding: MaterialStatePropertyAll<EdgeInsetsGeometry>(scaledPadding),
    );
  }

  return buttonStyle;
}