styleFrom method Null safety
- {Color? primary,
- Color? onSurface,
- Color? backgroundColor,
- Color? shadowColor,
- Color? surfaceTintColor,
- double? elevation,
- TextStyle? textStyle,
- EdgeInsetsGeometry? padding,
- Size? minimumSize,
- Size? fixedSize,
- Size? maximumSize,
- BorderSide? side,
- OutlinedBorder? shape,
- MouseCursor? enabledMouseCursor,
- MouseCursor? disabledMouseCursor,
- VisualDensity? visualDensity,
- MaterialTapTargetSize? tapTargetSize,
- Duration? animationDuration,
- bool? enableFeedback,
- AlignmentGeometry? alignment,
- InteractiveInkFeatureFactory? splashFactory}
A static convenience method that constructs a text button ButtonStyle given simple values.
The primary
, and onSurface
colors are used to create a
MaterialStateProperty ButtonStyle.foregroundColor value in the same
way that defaultStyleOf uses the ColorScheme colors with the same
names. Specify a value for primary
to specify the color of the button's
text and icons as well as the overlay colors used to indicate the hover,
focus, and pressed states. Use onSurface
to specify the button's
disabled text and icon color.
Similarly, the enabledMouseCursor
and disabledMouseCursor
parameters are used to construct ButtonStyle.mouseCursor.
All of the other parameters are either used directly or used to create a MaterialStateProperty with a single value for all states.
All parameters default to null. By default this method returns a ButtonStyle that doesn't override anything.
For example, to override the default text and icon colors for a TextButton, as well as its overlay color, with all of the standard opacity adjustments for the pressed, focused, and hovered states, one could write:
TextButton(
style: TextButton.styleFrom(primary: Colors.green),
)
Implementation
static ButtonStyle styleFrom({
Color? primary,
Color? onSurface,
Color? backgroundColor,
Color? shadowColor,
Color? surfaceTintColor,
double? elevation,
TextStyle? textStyle,
EdgeInsetsGeometry? padding,
Size? minimumSize,
Size? fixedSize,
Size? maximumSize,
BorderSide? side,
OutlinedBorder? shape,
MouseCursor? enabledMouseCursor,
MouseCursor? disabledMouseCursor,
VisualDensity? visualDensity,
MaterialTapTargetSize? tapTargetSize,
Duration? animationDuration,
bool? enableFeedback,
AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory,
}) {
final MaterialStateProperty<Color?>? foregroundColor = (onSurface == null && primary == null)
? null
: _TextButtonDefaultForeground(primary, onSurface);
final MaterialStateProperty<Color?>? overlayColor = (primary == null)
? null
: _TextButtonDefaultOverlay(primary);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _TextButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
return ButtonStyle(
textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
backgroundColor: ButtonStyleButton.allOrNull<Color>(backgroundColor),
foregroundColor: foregroundColor,
overlayColor: overlayColor,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
elevation: ButtonStyleButton.allOrNull<double>(elevation),
padding: ButtonStyleButton.allOrNull<EdgeInsetsGeometry>(padding),
minimumSize: ButtonStyleButton.allOrNull<Size>(minimumSize),
fixedSize: ButtonStyleButton.allOrNull<Size>(fixedSize),
maximumSize: ButtonStyleButton.allOrNull<Size>(maximumSize),
side: ButtonStyleButton.allOrNull<BorderSide>(side),
shape: ButtonStyleButton.allOrNull<OutlinedBorder>(shape),
mouseCursor: mouseCursor,
visualDensity: visualDensity,
tapTargetSize: tapTargetSize,
animationDuration: animationDuration,
enableFeedback: enableFeedback,
alignment: alignment,
splashFactory: splashFactory,
);
}