styleFrom static method
- Color? foregroundColor,
- Color? backgroundColor,
- Color? disabledForegroundColor,
- Color? disabledBackgroundColor,
- Color? shadowColor,
- Color? surfaceTintColor,
- Color? iconColor,
- Color? disabledIconColor,
- Color? overlayColor,
- 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,
- ButtonLayerBuilder? backgroundBuilder,
- ButtonLayerBuilder? foregroundBuilder,
A static convenience method that constructs an elevated button ButtonStyle given simple values.
The foregroundColor
and disabledForegroundColor
colors are used
to create a MaterialStateProperty ButtonStyle.foregroundColor, and
a derived ButtonStyle.overlayColor if overlayColor
isn't specified.
If overlayColor
is specified and its value is Colors.transparent
then the pressed/focused/hovered highlights are effectively defeated.
Otherwise a MaterialStateProperty with the same opacities as the
default is created.
The backgroundColor
and disabledBackgroundColor
colors are
used to create a MaterialStateProperty ButtonStyle.backgroundColor.
Similarly, the enabledMouseCursor
and disabledMouseCursor
parameters are used to construct ButtonStyle.mouseCursor and
iconColor
, disabledIconColor
are used to construct
ButtonStyle.iconColor.
The button's elevations are defined relative to the elevation
parameter. The disabled elevation is the same as the parameter
value, elevation
+ 2 is used when the button is hovered
or focused, and elevation + 6 is used when the button is pressed.
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 ElevatedButton, as well as its overlay color, with all of the standard opacity adjustments for the pressed, focused, and hovered states, one could write:
ElevatedButton(
style: ElevatedButton.styleFrom(foregroundColor: Colors.green),
onPressed: () {
// ...
},
child: const Text('Jump'),
),
And to change the fill color:
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
onPressed: () {
// ...
},
child: const Text('Meow'),
),
Implementation
static ButtonStyle styleFrom({
Color? foregroundColor,
Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor,
Color? surfaceTintColor,
Color? iconColor,
Color? disabledIconColor,
Color? overlayColor,
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,
ButtonLayerBuilder? backgroundBuilder,
ButtonLayerBuilder? foregroundBuilder,
}) {
final MaterialStateProperty<Color?>? foregroundColorProp = switch ((foregroundColor, disabledForegroundColor)) {
(null, null) => null,
(_, _) => _ElevatedButtonDefaultColor(foregroundColor, disabledForegroundColor),
};
final MaterialStateProperty<Color?>? backgroundColorProp = switch ((backgroundColor, disabledBackgroundColor)) {
(null, null) => null,
(_, _) => _ElevatedButtonDefaultColor(backgroundColor, disabledBackgroundColor),
};
final MaterialStateProperty<Color?>? iconColorProp = switch ((iconColor, disabledIconColor)) {
(null, null) => null,
(_, _) => _ElevatedButtonDefaultColor(iconColor, disabledIconColor),
};
final MaterialStateProperty<Color?>? overlayColorProp = switch ((foregroundColor, overlayColor)) {
(null, null) => null,
(_, final Color overlayColor) when overlayColor.value == 0 => const MaterialStatePropertyAll<Color?>(Colors.transparent),
(_, _) => _ElevatedButtonDefaultOverlay((overlayColor ?? foregroundColor)!),
};
final MaterialStateProperty<double>? elevationValue = switch (elevation) {
null => null,
_ => _ElevatedButtonDefaultElevation(elevation),
};
final MaterialStateProperty<MouseCursor?> mouseCursor = _ElevatedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);
return ButtonStyle(
textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle),
backgroundColor: backgroundColorProp,
foregroundColor: foregroundColorProp,
overlayColor: overlayColorProp,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
iconColor: iconColorProp,
elevation: elevationValue,
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,
backgroundBuilder: backgroundBuilder,
foregroundBuilder: foregroundBuilder,
);
}