defaultDialog<T> method

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

Custom UI Dialog.

Implementation

Future<T?> defaultDialog<T>({
  String title = "Alert",
  EdgeInsetsGeometry? titlePadding,
  TextStyle? titleStyle,
  Widget? content,
  String? id,
  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 = "\n",
  TextStyle? middleTextStyle,
  double radius = 20.0,
  //   ThemeData themeData,
  List<Widget>? actions,

  // onWillPop Scope
  PopInvokedWithResultCallback<T>? onWillPop,

  // the navigator used to push the dialog
  GlobalKey<NavigatorState>? navigatorKey,
}) {
  final standalone = rootController.config.useStandaloneDesign;
  final secondaryColor = standalone
      ? materialTheme.colorScheme.secondary
      : theme.colorScheme.secondary;
  final surfaceColor = standalone
      ? materialTheme.colorScheme.surface
      : theme.colorScheme.surface;
  var leanCancel = onCancel != null || textCancel != null;
  var leanConfirm = onConfirm != null || textConfirm != null;
  actions ??= [];

  if (cancel != null) {
    actions.add(cancel);
  } else {
    if (leanCancel) {
      void handleCancel() {
        if (onCancel == null) {
          closeAllDialogs();
        } else {
          onCancel.call();
        }
      }

      final cancelChild = Text(
        textCancel ?? "Cancel",
        style: TextStyle(color: cancelTextColor ?? secondaryColor),
      );
      final cancelShape = RoundedRectangleBorder(
        side: BorderSide(
          color: buttonColor ?? secondaryColor,
          width: 2,
          style: BorderStyle.solid,
        ),
        borderRadius: BorderRadius.circular(radius),
      );

      actions.add(
        standalone
            ? material.TextButton(
                style: material.TextButton.styleFrom(
                  tapTargetSize: material.MaterialTapTargetSize.shrinkWrap,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: 8,
                  ),
                  shape: cancelShape,
                ),
                onPressed: handleCancel,
                child: cancelChild,
              )
            : TextButton(
                style: TextButton.styleFrom(
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: 8,
                  ),
                  shape: cancelShape,
                ),
                onPressed: handleCancel,
                child: cancelChild,
              ),
      );
    }
  }
  if (confirm != null) {
    actions.add(confirm);
  } else {
    if (leanConfirm) {
      final confirmChild = Text(
        textConfirm ?? "Ok",
        style: TextStyle(color: confirmTextColor ?? surfaceColor),
      );
      final confirmShape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(radius),
      );

      actions.add(
        standalone
            ? material.TextButton(
                style: material.TextButton.styleFrom(
                  tapTargetSize: material.MaterialTapTargetSize.shrinkWrap,
                  backgroundColor: buttonColor ?? secondaryColor,
                  shape: confirmShape,
                ),
                onPressed: () {
                  onConfirm?.call();
                },
                child: confirmChild,
              )
            : TextButton(
                style: TextButton.styleFrom(
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  backgroundColor: buttonColor ?? secondaryColor,
                  shape: confirmShape,
                ),
                onPressed: () {
                  onConfirm?.call();
                },
                child: confirmChild,
              ),
      );
    }
  }

  Widget baseAlertDialog = Builder(
    builder: (context) {
      final dialogShape = RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(radius)),
      );
      final dialogTitle = Text(
        title,
        textAlign: TextAlign.center,
        style: titleStyle,
      );
      final actionsWrap = Wrap(
        alignment: WrapAlignment.center,
        spacing: 8,
        runSpacing: 8,
        children: actions!,
      );
      final dialogContent = Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          content ??
              Text(
                middleText,
                textAlign: TextAlign.center,
                style: middleTextStyle,
              ),
          const SizedBox(height: 16),
          standalone
              ? material.ButtonTheme(
                  minWidth: 78.0,
                  height: 34.0,
                  child: actionsWrap,
                )
              : ButtonTheme(
                  minWidth: 78.0,
                  height: 34.0,
                  child: actionsWrap,
                ),
        ],
      );

      if (standalone) {
        return material.AlertDialog(
          titlePadding: titlePadding ?? const EdgeInsets.all(8),
          contentPadding: contentPadding ?? const EdgeInsets.all(8),
          backgroundColor:
              backgroundColor ??
              material.DialogTheme.of(context).backgroundColor,
          shape: dialogShape,
          title: dialogTitle,
          content: dialogContent,
          buttonPadding: EdgeInsets.zero,
        );
      }
      return AlertDialog(
        titlePadding: titlePadding ?? const EdgeInsets.all(8),
        contentPadding: contentPadding ?? const EdgeInsets.all(8),
        backgroundColor:
            backgroundColor ?? DialogTheme.of(context).backgroundColor,
        shape: dialogShape,
        title: dialogTitle,
        content: dialogContent,
        buttonPadding: EdgeInsets.zero,
      );
    },
  );

  return dialog<T>(
    onWillPop != null
        ? PopScope<T>(
            onPopInvokedWithResult: (didPop, result) =>
                onWillPop(didPop, result),
            // onPopInvoked: onWillPop,
            child: baseAlertDialog,
          )
        : baseAlertDialog,
    barrierDismissible: barrierDismissible,
    navigatorKey: navigatorKey,
    id: id,
  );
}