styleFrom method Null safety

ButtonStyle styleFrom(
  1. {Color? foregroundColor,
  2. Color? backgroundColor,
  3. Color? disabledForegroundColor,
  4. Color? disabledBackgroundColor,
  5. Color? focusColor,
  6. Color? hoverColor,
  7. Color? highlightColor,
  8. Color? shadowColor,
  9. Color? surfaceTintColor,
  10. double? elevation,
  11. Size? minimumSize,
  12. Size? fixedSize,
  13. Size? maximumSize,
  14. BorderSide? side,
  15. OutlinedBorder? shape,
  16. EdgeInsetsGeometry? padding,
  17. MouseCursor? enabledMouseCursor,
  18. MouseCursor? disabledMouseCursor,
  19. VisualDensity? visualDensity,
  20. MaterialTapTargetSize? tapTargetSize,
  21. Duration? animationDuration,
  22. bool? enableFeedback,
  23. AlignmentGeometry? alignment,
  24. InteractiveInkFeatureFactory? splashFactory}
)

A static convenience method that constructs an icon button ButtonStyle given simple values. This method is only used for Material 3.

The foregroundColor color is used to create a MaterialStateProperty ButtonStyle.foregroundColor value. Specify a value for foregroundColor to specify the color of the button's icons. The hoverColor, focusColor and highlightColor colors are used to indicate the hover, focus, and pressed states. Use backgroundColor for the button's background fill color. Use disabledForegroundColor and disabledBackgroundColor to specify the button's disabled icon and fill 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 icon color for a IconButton, as well as its overlay color, with all of the standard opacity adjustments for the pressed, focused, and hovered states, one could write:

IconButton(
  style: IconButton.styleFrom(foregroundColor: Colors.green),
)

Implementation

static ButtonStyle styleFrom({
  Color? foregroundColor,
  Color? backgroundColor,
  Color? disabledForegroundColor,
  Color? disabledBackgroundColor,
  Color? focusColor,
  Color? hoverColor,
  Color? highlightColor,
  Color? shadowColor,
  Color? surfaceTintColor,
  double? elevation,
  Size? minimumSize,
  Size? fixedSize,
  Size? maximumSize,
  BorderSide? side,
  OutlinedBorder? shape,
  EdgeInsetsGeometry? padding,
  MouseCursor? enabledMouseCursor,
  MouseCursor? disabledMouseCursor,
  VisualDensity? visualDensity,
  MaterialTapTargetSize? tapTargetSize,
  Duration? animationDuration,
  bool? enableFeedback,
  AlignmentGeometry? alignment,
  InteractiveInkFeatureFactory? splashFactory,
}) {
  final MaterialStateProperty<Color?>? buttonBackgroundColor = (backgroundColor == null && disabledBackgroundColor == null)
      ? null
      : _IconButtonDefaultBackground(backgroundColor, disabledBackgroundColor);
  final MaterialStateProperty<Color?>? buttonForegroundColor = (foregroundColor == null && disabledForegroundColor == null)
      ? null
      : _IconButtonDefaultForeground(foregroundColor, disabledForegroundColor);
  final MaterialStateProperty<Color?>? overlayColor = (foregroundColor == null && hoverColor == null && focusColor == null && highlightColor == null)
      ? null
      : _IconButtonDefaultOverlay(foregroundColor, focusColor, hoverColor, highlightColor);
  final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
      ? null
      : _IconButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);

  return ButtonStyle(
    backgroundColor: buttonBackgroundColor,
    foregroundColor: buttonForegroundColor,
    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,
  );
}