defaultStyleOf method
Returns a ButtonStyle that's based primarily on the Theme's ThemeData.textTheme and ThemeData.colorScheme, but has most values filled out (non-null).
The returned style can be overridden by the style parameter and by the style returned by themeStyleOf that some button-specific themes like TextButtonTheme or ElevatedButtonTheme override. For example the default style of the TextButton subclass can be overridden with its TextButton.style constructor parameter, or with a TextButtonTheme.
Concrete button subclasses should return a ButtonStyle with as many non-null properties as possible, where all of the non-null WidgetStateProperty properties resolve to non-null values.
Properties that can be null
Some properties, like ButtonStyle.fixedSize would override other values in the same ButtonStyle if set, so they are allowed to be null. Here is a summary of properties that are allowed to be null when returned in the ButtonStyle returned by this function, an why:
- ButtonStyle.fixedSize because it would override other values in the same ButtonStyle, like ButtonStyle.maximumSize.
- ButtonStyle.side because null is a valid value for a button that has no side. OutlinedButton returns a non-null default for this, however.
- ButtonStyle.backgroundBuilder and ButtonStyle.foregroundBuilder because they would override the ButtonStyle.foregroundColor and ButtonStyle.backgroundColor of the same ButtonStyle.
See also:
- themeStyleOf, returns the ButtonStyle of this button's component theme.
Implementation
@override
ButtonStyle defaultStyleOf(BuildContext context) {
final ThemeData theme = Theme.of(context);
final ColorScheme colorScheme = theme.colorScheme;
return Theme.of(context).useMaterial3
? _MTextButtonDefaultsM3(
context: context,
backgroundColor: backgroundColor,
borderRadius: borderRadius,
clearPadding: clearPadding,
disabledForegroundColor: disabledColor,
elevation: elevation,
fixedSize: fixedSize,
foregroundColor: foregroundColor,
height: height,
maximumSize: maximumSize,
minimumSize: minimumSize,
noHighlight: noHighlight,
noSplash: noSplash,
overlayColor: overlayColor,
padding: padding,
radius: radius,
shadowColor: shadowColor,
shape: shape,
side: side,
splashFactory: splashFactory,
tapTargetSize: tapTargetSize,
textStyle: textStyle,
width: width,
)
: _MTextButtonDefaultsM2(
foregroundColor: foregroundColor ?? colorScheme.primary,
disabledForegroundColor: disabledColor ?? colorScheme.onSurface.withOpacity(0.38),
backgroundColor: backgroundColor ?? Colors.transparent,
disabledBackgroundColor: Colors.transparent,
shadowColor: shadowColor ?? theme.shadowColor,
elevation: elevation ?? 0,
textStyle: textStyle ?? theme.textTheme.labelLarge,
padding: padding ?? (clearPadding ? EdgeInsets.zero : _scaledPadding(context)),
minimumSize: minimumSize ?? (clearPadding ? Size.zero : const Size(64, 36)),
maximumSize: maximumSize ?? Size.infinite,
shape: shape ??
RoundedRectangleBorder(borderRadius: borderRadius ?? BorderRadius.all(Radius.circular(radius ?? 4))),
enabledMouseCursor: SystemMouseCursors.click,
disabledMouseCursor: SystemMouseCursors.basic,
visualDensity: theme.visualDensity,
tapTargetSize:
tapTargetSize ?? (clearPadding ? MaterialTapTargetSize.shrinkWrap : theme.materialTapTargetSize),
animationDuration: kThemeChangeDuration,
enableFeedback: true,
alignment: Alignment.center,
splashFactory: splashFactory ?? (noSplash ? NoSplash.splashFactory : InkRipple.splashFactory),
fixedSize: fixedSize ??
(width != null || height != null ? Size(width ?? double.infinity, height ?? double.infinity) : null),
noHighlighting: noHighlight,
overlayColor: overlayColor,
side: side,
);
}