confirmAlert static method
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;
}