Files
westone-kurir/lib/widget/custom_button.dart
Sas Andy 2a356ebdef login ui
2024-08-16 10:27:05 +07:00

84 lines
3.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:westone_kurirapp/app/constant.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
enum ButtonType { primary, secondary, error, warning, custom, success }
class CustomButton extends HookConsumerWidget {
const CustomButton(
{super.key,
required this.onPressed,
this.text = '',
this.loading = false,
this.disabled = false,
this.type = ButtonType.primary,
this.color = Colors.black,
this.overlayColor = Colors.white54});
final void Function()? onPressed;
final bool loading;
final bool disabled;
final String text;
final ButtonType type;
final Color color;
final Color overlayColor;
@override
Widget build(BuildContext context, WidgetRef ref) {
final colorState = useState(Constant.primary);
final overlayColorState = useState(Constant.primaryDarken);
switch (type) {
case ButtonType.primary:
colorState.value = Constant.primary;
overlayColorState.value = Constant.primaryDarken;
break;
case ButtonType.secondary:
colorState.value = Constant.secondary;
overlayColorState.value = Constant.secondaryDarken;
break;
case ButtonType.error:
colorState.value = Constant.error;
overlayColorState.value = Constant.errorDarken;
break;
case ButtonType.warning:
colorState.value = Constant.warning;
overlayColorState.value = Constant.warningDarken;
break;
case ButtonType.success:
colorState.value = Constant.success;
overlayColorState.value = Constant.successDarken;
break;
case ButtonType.custom:
colorState.value = color;
overlayColorState.value = overlayColor;
break;
default:
colorState.value = Constant.primary;
overlayColorState.value = Constant.primaryDarken;
}
return ElevatedButton(
onPressed: loading || disabled ? null : onPressed,
style: ButtonStyle(
padding: WidgetStateProperty.all(EdgeInsets.symmetric(
vertical: Constant.getActualY(context: context, y: 12))),
// surfaceTintColor: MaterialStateProperty.all(Constant.secondary),
// shadowColor: MaterialStateProperty.all(Constant.secondary),
overlayColor: WidgetStateProperty.all(overlayColorState.value),
// foregroundColor: MaterialStateProperty.all(Constant.secondary),
backgroundColor: WidgetStateProperty.all(colorState.value),
shape: WidgetStateProperty.all(const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(4))))),
child: loading
? LoadingAnimationWidget.staggeredDotsWave(
color: Colors.white, size: 25)
: Text(
text,
style: Constant.subtitle1(context: context)
.copyWith(color: Colors.white, fontWeight: FontWeight.bold),
),
);
}
}