Files
2026-09-27 19:12:14 +03:00

115 lines
3.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:sign_checker/theme/tokens.dart';
enum AppButtonStyle { filled, tinted, gray, plain, destructive }
enum AppButtonSize { large, medium, small }
class AppButton extends StatelessWidget {
const AppButton({
super.key,
required this.label,
this.onPressed,
this.style = AppButtonStyle.filled,
this.size = AppButtonSize.large,
this.expanded = true,
this.icon,
this.loading = false,
});
final String label;
final VoidCallback? onPressed;
final AppButtonStyle style;
final AppButtonSize size;
final bool expanded;
final IconData? icon;
final bool loading;
double get _height => switch (size) {
AppButtonSize.large => AppSizes.buttonLarge,
AppButtonSize.medium => AppSizes.buttonMedium,
AppButtonSize.small => AppSizes.buttonSmall,
};
TextStyle get _textStyle => switch (size) {
AppButtonSize.large => AppTypography.headline,
AppButtonSize.medium => AppTypography.body,
AppButtonSize.small => AppTypography.subhead.copyWith(
fontWeight: FontWeight.w600,
),
};
@override
Widget build(BuildContext context) {
final accent = AppColors.of(context, AppColors.accent);
final red = AppColors.of(context, AppColors.systemRed);
final (Color background, Color foreground) = switch (style) {
AppButtonStyle.filled => (accent, const Color(0xFFFFFFFF)),
AppButtonStyle.tinted => (accent.withValues(alpha: 0.15), accent),
AppButtonStyle.gray => (
AppColors.of(context, AppColors.systemGray5),
AppColors.of(context, AppColors.label),
),
AppButtonStyle.plain => (const Color(0x00000000), accent),
AppButtonStyle.destructive => (red.withValues(alpha: 0.15), red),
};
final disabled = onPressed == null || loading;
final content = loading
? CupertinoActivityIndicator(
radius: size == AppButtonSize.small ? 8 : 10,
color: foreground,
)
: Row(
mainAxisSize: expanded ? MainAxisSize.max : MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (icon != null) ...[
Icon(
icon,
size: size == AppButtonSize.small ? 16 : 19,
color: foreground,
),
const SizedBox(width: AppSpacing.s2),
],
Flexible(
child: Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.inter(
textStyle: _textStyle,
color: foreground,
),
),
),
],
);
return Opacity(
opacity: disabled && !loading ? 0.35 : 1,
child: SizedBox(
height: _height,
width: expanded ? double.infinity : null,
child: CupertinoButton(
onPressed: disabled ? null : onPressed,
color: style == AppButtonStyle.plain ? null : background,
disabledColor: background,
borderRadius: BorderRadius.circular(
size == AppButtonSize.small ? AppRadii.md : AppRadii.lg,
),
padding: EdgeInsets.symmetric(
horizontal: size == AppButtonSize.small
? AppSpacing.s3
: AppSpacing.s4,
),
minimumSize: Size.zero,
child: content,
),
),
);
}
}