confirmAlert static method

Future<bool> confirmAlert({
  1. required BuildContext context,
  2. required String content,
  3. String? title,
  4. Color? titleColor,
  5. Color? contentColor,
  6. String? confirmButtonTitle,
  7. bool showCancel = true,
  8. bool cupertinoStyle = false,
})

The Confirmable info-alert with customizable confirm button and cancel button.

Implementation

static Future<bool> confirmAlert({
  required BuildContext context,
  required String content,
  String? title,
  Color? titleColor,
  Color? contentColor,
  String? confirmButtonTitle,
  bool showCancel = true,
  bool cupertinoStyle = false,
}) {
  final completer = Completer<bool>();
  if (cupertinoStyle) {
    CupertinoAlertDialog dialog = CupertinoAlertDialog(
      title: (null != title && title.isEmpty)
          ? null
          : (null == titleColor
          ? Text(title ?? '提示')
          : Text(
        title ?? '提示',
        style: TextStyle(color: titleColor),
      )),
      content: SingleChildScrollView(
        child: (null == contentColor
            ? Text(content)
            : Text(
          content,
          style: TextStyle(color: contentColor),
        )),
      ),
      actions: <Widget>[
        showCancel ?
        TextButton(
          child: const Text(
            '取消',
            style: TextStyle(color: Colors.redAccent),
          ),
          onPressed: () {
            Navigator.of(context).pop();
            completer.complete(Future<bool>.value(false));
          },
        ):const SizedBox(width: 0.01),
        TextButton(
          child: Text(confirmButtonTitle ?? '确定'),
          onPressed: () {
            Navigator.of(context).pop();
            completer.complete(Future<bool>.value(true));
          },
        ),
      ],
    );
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return dialog;
    });
  } else {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: (null != title && title.isEmpty)
              ? null
              : (null == titleColor
              ? Text(title ?? '提示')
              : Text(
            title ?? '提示',
            style: TextStyle(color: titleColor),
          )),
          content: SingleChildScrollView(
            child: (null == contentColor
                ? Text(content)
                : Text(
              content,
              style: TextStyle(color: contentColor),
            )),
          ),
          actions: <Widget>[
            showCancel ?
            TextButton(
              child: const Text(
                '取消',
                style: TextStyle(color: Colors.redAccent),
              ),
              onPressed: () {
                Navigator.of(context).pop();
                completer.complete(Future<bool>.value(false));
              },
            ):const SizedBox(width: 0.01),
            TextButton(
              child: Text(confirmButtonTitle ?? '确定'),
              onPressed: () {
                Navigator.of(context).pop();
                completer.complete(Future<bool>.value(true));
              },
            ),
          ],
        );
      },
    );
  }
  return completer.future;
}