styleFrom static method

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

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

The foregroundColor and disabledForegroundColor colors are used to create a WidgetStateProperty ButtonStyle.foregroundColor, and a derived ButtonStyle.overlayColor if overlayColor isn't specified.

The backgroundColor and disabledBackgroundColor colors are used to create a WidgetStateProperty ButtonStyle.backgroundColor.

Similarly, the enabledMouseCursor and disabledMouseCursor parameters are used to construct ButtonStyle.mouseCursor.

The iconColor, disabledIconColor are used to construct ButtonStyle.iconColor and iconSize is used to construct ButtonStyle.iconSize.

If overlayColor is specified and its value is Colors.transparent then the pressed/focused/hovered highlights are effectively defeated. Otherwise a WidgetStateProperty with the same opacities as the default is created.

All of the other parameters are either used directly or used to create a WidgetStateProperty 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 shape and outline for an OutlinedButton, one could write:

OutlinedButton(
  style: OutlinedButton.styleFrom(
     shape: const StadiumBorder(),
     side: const BorderSide(width: 2, color: Colors.green),
  ),
  child: const Text('Seasons of Love'),
  onPressed: () {
    // ...
  },
),

Implementation

static ButtonStyle styleFrom({
  Color? foregroundColor,
  Color? backgroundColor,
  Color? disabledForegroundColor,
  Color? disabledBackgroundColor,
  Color? shadowColor,
  Color? surfaceTintColor,
  Color? iconColor,
  double? iconSize,
  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?>? backgroundColorProp = switch ((backgroundColor, disabledBackgroundColor)) {
    (_?, null) => WidgetStatePropertyAll<Color?>(backgroundColor),
    (_, _) => ButtonStyleButton.defaultColor(backgroundColor, disabledBackgroundColor),
  };
  final MaterialStateProperty<Color?>? overlayColorProp = switch ((foregroundColor, overlayColor)) {
    (null, null) => null,
    (_, Color(a: 0.0)) => WidgetStatePropertyAll<Color?>(overlayColor),
    (_, final Color color) || (final Color color, _) => WidgetStateProperty<Color?>.fromMap(
      <WidgetState, Color?>{
        WidgetState.pressed: color.withOpacity(0.1),
        WidgetState.hovered: color.withOpacity(0.08),
        WidgetState.focused: color.withOpacity(0.1),
      },
    ),
  };

  return ButtonStyle(
    textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
    foregroundColor: ButtonStyleButton.defaultColor(foregroundColor, disabledForegroundColor),
    backgroundColor: backgroundColorProp,
    overlayColor: overlayColorProp,
    shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
    surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
    iconColor: ButtonStyleButton.defaultColor(iconColor, disabledIconColor),
    iconSize: ButtonStyleButton.allOrNull<double>(iconSize),
    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: WidgetStateProperty<MouseCursor?>.fromMap(
      <WidgetStatesConstraint, MouseCursor?>{
        WidgetState.disabled: disabledMouseCursor,
        WidgetState.any: enabledMouseCursor,
      },
    ),
    visualDensity: visualDensity,
    tapTargetSize: tapTargetSize,
    animationDuration: animationDuration,
    enableFeedback: enableFeedback,
    alignment: alignment,
    splashFactory: splashFactory,
    backgroundBuilder: backgroundBuilder,
    foregroundBuilder: foregroundBuilder,
  );
}