masterAlert function

Widget masterAlert(
  1. BuildContext context, {
  2. required String title,
  3. String? message,
  4. Widget? child,
  5. required String buttonText,
  6. required double iconSize,
  7. required VoidCallback? onConfirm,
  8. required bool? showCancelButton,
  9. required String cancelButtonText,
  10. required String confirmButtonText,
  11. required IconData icon,
  12. required Color iconColor,
  13. VoidCallback? onCancel,
  14. ButtonStyle? confirmButtonStyle,
  15. ButtonStyle? cancelButtonStyle,
})

Implementation

Widget masterAlert(
  BuildContext context, {
  required String title,
  String? message,
  Widget? child,
  required String buttonText,
  required double iconSize,
  required VoidCallback? onConfirm,
  required bool? showCancelButton,
  required String cancelButtonText,
  required String confirmButtonText,
  required IconData icon,
  required Color iconColor,
  VoidCallback? onCancel,
  ButtonStyle? confirmButtonStyle,
  ButtonStyle? cancelButtonStyle,
}) {
  assert((child != null && message == null) || (child == null && message != null), 'showCancelButton must not be null');
  return AlertDialog(
    title: Text(title, textAlign: TextAlign.center),
    icon: Icon(
      icon,
      color: iconColor,
      size: iconSize,
    ),
    content: Container(
      constraints: const BoxConstraints(maxWidth: 400),
      child: child ??
          Text(
            message ?? "",
            textAlign: TextAlign.center,
          ),
    ),
    actions: [
      if (showCancelButton == true)
        ElevatedButton(
          onPressed: () {
            if (onCancel != null) {
              onCancel.call();
            } else {
              Navigator.pop(context);
            }
          },
          style: cancelButtonStyle ??
              ElevatedButton.styleFrom(
                backgroundColor: Colors.grey,
                foregroundColor: Colors.white,
              ),
          child: Text(cancelButtonText),
        ),
      ElevatedButton(
        onPressed: () {
          if (onConfirm != null) {
            onConfirm.call();
          } else {
            Navigator.pop(context);
          }
        },
        style: confirmButtonStyle ??
            ElevatedButton.styleFrom(
              backgroundColor: Colors.blue,
              foregroundColor: Colors.white,
            ),
        child: Text(confirmButtonText),
      ),
    ],
    actionsAlignment: MainAxisAlignment.spaceAround,
  );
}