defaultDialog<T> method

Future<T?> defaultDialog<T>({
  1. String title = "Alert",
  2. EdgeInsetsGeometry? titlePadding,
  3. TextStyle? titleStyle,
  4. Widget? content,
  5. EdgeInsetsGeometry? contentPadding,
  6. VoidCallback? onConfirm,
  7. VoidCallback? onCancel,
  8. VoidCallback? onCustom,
  9. Color? cancelTextColor,
  10. Color? confirmTextColor,
  11. String? textConfirm,
  12. String? textCancel,
  13. String? textCustom,
  14. Widget? confirm,
  15. Widget? cancel,
  16. Widget? custom,
  17. Color? backgroundColor,
  18. bool barrierDismissible = true,
  19. Color? buttonColor,
  20. String middleText = "Dialog made in 3 lines of code",
  21. TextStyle? middleTextStyle,
  22. double radius = 20.0,
  23. List<Widget>? actions,
  24. WillPopCallback? onWillPop,
  25. GlobalKey<NavigatorState>? navigatorKey,
})

Custom UI Dialog.

Implementation

Future<T?> defaultDialog<T>({
  String title = "Alert",
  EdgeInsetsGeometry? titlePadding,
  TextStyle? titleStyle,
  Widget? content,
  EdgeInsetsGeometry? contentPadding,
  VoidCallback? onConfirm,
  VoidCallback? onCancel,
  VoidCallback? onCustom,
  Color? cancelTextColor,
  Color? confirmTextColor,
  String? textConfirm,
  String? textCancel,
  String? textCustom,
  Widget? confirm,
  Widget? cancel,
  Widget? custom,
  Color? backgroundColor,
  bool barrierDismissible = true,
  Color? buttonColor,
  String middleText = "Dialog made in 3 lines of code",
  TextStyle? middleTextStyle,
  double radius = 20.0,
  //   ThemeData themeData,
  List<Widget>? actions,

  // onWillPop Scope
  WillPopCallback? onWillPop,

  // the navigator used to push the dialog
  GlobalKey<NavigatorState>? navigatorKey,
}) {
  var leanCancel = onCancel != null || textCancel != null;
  var leanConfirm = onConfirm != null || textConfirm != null;
  actions ??= [];

  if (cancel != null) {
    actions.add(cancel);
  } else {
    if (leanCancel) {
      actions.add(TextButton(
        style: TextButton.styleFrom(
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
          shape: RoundedRectangleBorder(
              side: BorderSide(
                  color: buttonColor ?? theme.colorScheme.secondary,
                  width: 2,
                  style: BorderStyle.solid),
              borderRadius: BorderRadius.circular(100)),
        ),
        onPressed: () {
          onCancel?.call();
          back();
        },
        child: Text(
          textCancel ?? "Cancel",
          style: TextStyle(
              color: cancelTextColor ?? theme.colorScheme.secondary),
        ),
      ));
    }
  }
  if (confirm != null) {
    actions.add(confirm);
  } else {
    if (leanConfirm) {
      actions.add(TextButton(
          style: TextButton.styleFrom(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
            backgroundColor: buttonColor ?? theme.colorScheme.secondary,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(100)),
          ),
          child: Text(
            textConfirm ?? "Ok",
            style: TextStyle(
                color: confirmTextColor ?? theme.colorScheme.background),
          ),
          onPressed: () {
            onConfirm?.call();
          }));
    }
  }

  Widget baseAlertDialog = AlertDialog(
    titlePadding: titlePadding ?? const EdgeInsets.all(8),
    contentPadding: contentPadding ?? const EdgeInsets.all(8),

    backgroundColor: backgroundColor ?? theme.dialogBackgroundColor,
    shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(radius))),
    title: Text(title, textAlign: TextAlign.center, style: titleStyle),
    content: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        content ??
            Text(middleText,
                textAlign: TextAlign.center, style: middleTextStyle),
        const SizedBox(height: 16),
        ButtonTheme(
          minWidth: 78.0,
          height: 34.0,
          child: Wrap(
            alignment: WrapAlignment.center,
            spacing: 8,
            runSpacing: 8,
            children: actions,
          ),
        )
      ],
    ),
    // actions: actions, // ?? <Widget>[cancelButton, confirmButton],
    buttonPadding: EdgeInsets.zero,
  );

  return dialog<T>(
    onWillPop != null
        ? WillPopScope(
            onWillPop: onWillPop,
            child: baseAlertDialog,
          )
        : baseAlertDialog,
    barrierDismissible: barrierDismissible,
    navigatorKey: navigatorKey,
  );
}