styleFrom method Null safety
- {Color? foregroundColor,
- Color? backgroundColor,
- Color? disabledForegroundColor,
- Color? disabledBackgroundColor,
- 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,
- @Deprecated('Use backgroundColor instead. ' 'This feature was deprecated after v3.1.0.') Color? primary,
- @Deprecated('Use foregroundColor instead. ' 'This feature was deprecated after v3.1.0.') Color? onPrimary,
- @Deprecated('Use disabledForegroundColor and disabledBackgroundColor instead. ' 'This feature was deprecated after v3.1.0.') Color? onSurface}
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.
The backgroundColor
and disabledBackgroundColor
colors are
used to create a MaterialStateProperty ButtonStyle.backgroundColor.
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.
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 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),
)
And to change the fill color:
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
)
Implementation
static ButtonStyle styleFrom({
Color? foregroundColor,
Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
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,
@Deprecated(
'Use backgroundColor instead. '
'This feature was deprecated after v3.1.0.'
)
Color? primary,
@Deprecated(
'Use foregroundColor instead. '
'This feature was deprecated after v3.1.0.'
)
Color? onPrimary,
@Deprecated(
'Use disabledForegroundColor and disabledBackgroundColor instead. '
'This feature was deprecated after v3.1.0.'
)
Color? onSurface,
}) {
final Color? background = backgroundColor ?? primary;
final Color? disabledBackground = disabledBackgroundColor ?? onSurface?.withOpacity(0.12);
final MaterialStateProperty<Color?>? backgroundColorProp = (background == null && disabledBackground == null)
? null
: _ElevatedButtonDefaultColor(background, disabledBackground);
final Color? foreground = foregroundColor ?? onPrimary;
final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38);
final MaterialStateProperty<Color?>? foregroundColorProp = (foreground == null && disabledForeground == null)
? null
: _ElevatedButtonDefaultColor(foreground, disabledForeground);
final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null
: _ElevatedButtonDefaultOverlay(foreground);
final MaterialStateProperty<double>? elevationValue = (elevation == null)
? null
: _ElevatedButtonDefaultElevation(elevation);
final MaterialStateProperty<MouseCursor?>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _ElevatedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);
return ButtonStyle(
textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle),
backgroundColor: backgroundColorProp,
foregroundColor: foregroundColorProp,
overlayColor: overlayColor,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
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,
);
}