buttonStyle function
- BuildContext context,
- ZetaWidgetBorder borderType,
- ZetaButtonType type,
- Color? backgroundColor,
Shared buttonStyle for buttons and icon buttons
Implementation
ButtonStyle buttonStyle(
BuildContext context,
ZetaWidgetBorder borderType,
ZetaButtonType type,
Color? backgroundColor,
) {
final ZetaColors colors = Zeta.of(context).colors;
final ZetaColorSwatch color =
backgroundColor != null ? ZetaColorSwatch.fromColor(backgroundColor) : type.color(colors);
final bool isSolid = type.solid || backgroundColor != null;
return ButtonStyle(
minimumSize: WidgetStateProperty.all(Size.square(Zeta.of(context).spacing.xl_4)),
shape: WidgetStateProperty.all(
RoundedRectangleBorder(borderRadius: borderType.radius(context)),
),
backgroundColor: WidgetStateProperty.resolveWith<Color?>(
(states) {
if (states.contains(WidgetState.disabled)) {
return colors.surfaceDisabled;
}
if (states.contains(WidgetState.pressed)) {
return isSolid ? color.shade70 : colors.primary.shade10;
}
if (states.contains(WidgetState.hovered)) {
return isSolid ? color.shade50 : colors.cool.shade20;
}
if (backgroundColor != null) return backgroundColor;
return isSolid ? color : colors.surfacePrimary;
},
),
foregroundColor: WidgetStateProperty.resolveWith<Color?>(
(states) {
if (states.contains(WidgetState.disabled)) {
return colors.textDisabled;
}
switch (type) {
case ZetaButtonType.outline:
case ZetaButtonType.text:
return colors.primary;
case ZetaButtonType.outlineSubtle:
return colors.textDefault;
case ZetaButtonType.primary:
case ZetaButtonType.secondary:
case ZetaButtonType.positive:
case ZetaButtonType.negative:
return color.onColor;
}
},
),
overlayColor: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
return null;
}),
side: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (type.border && states.contains(WidgetState.disabled)) {
return BorderSide(color: colors.borderDisabled);
}
// TODO(UX-1134): This removes a defualt border when focused, rather than adding a second border when focused.
if (states.contains(WidgetState.focused)) {
return BorderSide(color: colors.blue, width: ZetaBorders.medium);
}
if (type.border) {
return BorderSide(
color: type == ZetaButtonType.outline ? colors.primary.border : colors.borderSubtle,
);
}
return null;
}),
elevation: const WidgetStatePropertyAll(0),
padding: WidgetStateProperty.all(EdgeInsets.zero),
);
}