withBackgroundColor method

AbstractButtonStyle withBackgroundColor({
  1. Color? color,
  2. Color? hoverColor,
  3. Color? focusColor,
  4. Color? disabledColor,
})

Creates a copy with custom background colors for different states.

Modifies the decoration to apply state-specific background colors. Only works with BoxDecoration; other decoration types are returned unchanged.

Parameters:

  • color: Background color for normal state
  • hoverColor: Background color when hovered
  • focusColor: Background color when focused
  • disabledColor: Background color when disabled

Example:

final style = ButtonVariance.primary.withBackgroundColor(
  color: Colors.blue,
  hoverColor: Colors.blue.shade700,
);

Implementation

AbstractButtonStyle withBackgroundColor({
  Color? color,
  Color? hoverColor,
  Color? focusColor,
  Color? disabledColor,
}) {
  return copyWith(
    decoration: (context, states, decoration) {
      if (decoration is BoxDecoration) {
        return decoration.copyWith(
          color: states.disabled
              ? disabledColor ?? decoration.color
              : states.hovered
              ? hoverColor ?? decoration.color
              : states.focused
              ? focusColor ?? decoration.color
              : color,
        );
      }
      return decoration;
    },
  );
}