styleFrom method Null safety

ButtonStyle styleFrom(
  1. {Color? primary,
  2. Color? onPrimary,
  3. Color? onSurface,
  4. Color? shadowColor,
  5. double? elevation,
  6. TextStyle? textStyle,
  7. EdgeInsetsGeometry? padding,
  8. Size? minimumSize,
  9. Size? fixedSize,
  10. BorderSide? side,
  11. OutlinedBorder? shape,
  12. MouseCursor? enabledMouseCursor,
  13. MouseCursor? disabledMouseCursor,
  14. VisualDensity? visualDensity,
  15. MaterialTapTargetSize? tapTargetSize,
  16. Duration? animationDuration,
  17. bool? enableFeedback,
  18. AlignmentGeometry? alignment,
  19. InteractiveInkFeatureFactory? splashFactory}
)

A static convenience method that constructs an elevated button ButtonStyle given simple values.

The onPrimary, and onSurface colors are used to 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 onPrimary 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 primary for the button's background fill color and onSurface to specify the button's disabled text, icon, and fill color.

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(primary: Colors.green),
)

Implementation

static ButtonStyle styleFrom({
  Color? primary,
  Color? onPrimary,
  Color? onSurface,
  Color? shadowColor,
  double? elevation,
  TextStyle? textStyle,
  EdgeInsetsGeometry? padding,
  Size? minimumSize,
  Size? fixedSize,
  BorderSide? side,
  OutlinedBorder? shape,
  MouseCursor? enabledMouseCursor,
  MouseCursor? disabledMouseCursor,
  VisualDensity? visualDensity,
  MaterialTapTargetSize? tapTargetSize,
  Duration? animationDuration,
  bool? enableFeedback,
  AlignmentGeometry? alignment,
  InteractiveInkFeatureFactory? splashFactory,
}) {
  final MaterialStateProperty<Color?>? backgroundColor = (onSurface == null && primary == null)
    ? null
    : _ElevatedButtonDefaultBackground(primary, onSurface);
  final MaterialStateProperty<Color?>? foregroundColor = (onSurface == null && onPrimary == null)
    ? null
    : _ElevatedButtonDefaultForeground(onPrimary, onSurface);
  final MaterialStateProperty<Color?>? overlayColor = (onPrimary == null)
    ? null
    : _ElevatedButtonDefaultOverlay(onPrimary);
  final MaterialStateProperty<double>? elevationValue = (elevation == null)
    ? null
    : _ElevatedButtonDefaultElevation(elevation);
  final MaterialStateProperty<MouseCursor?>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
    ? null
    : _ElevatedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

  return ButtonStyle(
    textStyle: MaterialStateProperty.all<TextStyle?>(textStyle),
    backgroundColor: backgroundColor,
    foregroundColor: foregroundColor,
    overlayColor: overlayColor,
    shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
    elevation: elevationValue,
    padding: ButtonStyleButton.allOrNull<EdgeInsetsGeometry>(padding),
    minimumSize: ButtonStyleButton.allOrNull<Size>(minimumSize),
    fixedSize: ButtonStyleButton.allOrNull<Size>(fixedSize),
    side: ButtonStyleButton.allOrNull<BorderSide>(side),
    shape: ButtonStyleButton.allOrNull<OutlinedBorder>(shape),
    mouseCursor: mouseCursor,
    visualDensity: visualDensity,
    tapTargetSize: tapTargetSize,
    animationDuration: animationDuration,
    enableFeedback: enableFeedback,
    alignment: alignment,
    splashFactory: splashFactory,
  );
}