dialog function

Future dialog({
  1. required String titleText,
  2. void onTapOk()?,
  3. void onTapCancel()?,
  4. String? cancelButtonText,
  5. String? okButtonText,
  6. String? contentText,
  7. Widget? content,
  8. bool barrierDismissible = true,
  9. bool? returnAutomatic,
})

Implementation

Future dialog({
  required String titleText,
  void Function()? onTapOk,
  void Function()? onTapCancel,
  String? cancelButtonText,
  String? okButtonText,
  String? contentText,
  Widget? content,
  bool barrierDismissible = true,
  bool? returnAutomatic,
}) {
  Widget _title = Text(
    titleText,
    style: TextStyle(fontWeight: FontWeight.w500),
  );

  Widget _content = content ?? Text("$contentText");

  bool _cancelButtonTextExist = cancelButtonText.isNotNullAndNotEmpty;
  bool _okButtonTextExist = okButtonText.isNotNullAndNotEmpty;

  Widget? cancelButtonChild = _cancelButtonTextExist
      ? Text(
          cancelButtonText!,
          style: TextStyle(color: Colors.red, fontWeight: FontWeight.w600),
        )
      : null;
  void cancelButtonOnPressed(BuildContext context) {
    if (onTapCancel != null) {
      onTapCancel();
    } else {
      Navigator.pop(context, false);
    }
  }

  Widget? okButtonChild = _okButtonTextExist
      ? Text(
          okButtonText!,
          style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w600),
        )
      : null;
  void okButtonOnPressed(BuildContext context) {
    if (returnAutomatic == null || returnAutomatic) {
      Navigator.pop(context, true);
    }
    if (onTapOk != null) {
      onTapOk();
    }
  }

  return showModernFormDialog(
    barrierDismissible: barrierDismissible,
    context: ModernFormGlobalContext.context!,
    builder: (BuildContext context) {
      return ConstrainedBox(
        constraints: BoxConstraints(
          maxWidth: ModernFormGlobalContext.width > ModernFormUtils.webBreakdown
              ? ModernFormGlobalContext.width * .4
              : ModernFormGlobalContext.width * .6,
        ),
        child: LayoutBuilder(
          builder: (c, constraints) {
            if (Theme.of(context).platform == TargetPlatform.iOS) {
              return CupertinoAlertDialog(
                title: _title,
                content: _content,
                actions: <Widget>[
                  if (_cancelButtonTextExist)
                    CupertinoDialogAction(
                      child: cancelButtonChild!,
                      onPressed: () {
                        cancelButtonOnPressed(context);
                      },
                    ),
                  if (_okButtonTextExist)
                    CupertinoDialogAction(
                      child: okButtonChild!,
                      onPressed: () {
                        okButtonOnPressed(context);
                      },
                    ),
                ],
              );
            } else {
              return AlertDialog(
                title: _title,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(7)),
                content: _content,
                actions: <Widget>[
                  if (_cancelButtonTextExist)
                    TextButton(
                      child: cancelButtonChild!,
                      onPressed: () {
                        cancelButtonOnPressed(context);
                      },
                    ),
                  if (_okButtonTextExist)
                    TextButton(
                      child: okButtonChild!,
                      onPressed: () {
                        okButtonOnPressed(context);
                      },
                    )
                ],
              );
            }
          },
        ),
      );
    },
  );
}