static Future<bool?> showAlertDialog({
required BuildContext context,
String? title,
String? content,
String? okText,
String? loadingTitle,
String? loadingSubTitle,
String? errorText,
Future<bool> Function()? onConfirmed,
}) async {
return await showDialog<bool>(
context: context,
builder: (ctx) {
bool isOk = false;
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
if (isOk) {
return AlertDialog(
title: Text(loadingTitle ?? "正在加载"),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 16),
const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(
strokeWidth: 3,
),
),
const SizedBox(height: 16),
Text(loadingSubTitle ?? '请等待...'),
],
),
);
} else {
return AlertDialog(
title: title != null ? Text(title) : null,
content: content != null
? Text(
content,
style: const TextStyle(height: 1.6),
)
: null,
actions: [
FilledButton.tonal(
onPressed: () {
AppNavigator.pop(ctx);
},
child: Text(S.of(ctx).cancel),
),
FilledButton(
onPressed: () async {
setState(() {
isOk = true;
});
if (onConfirmed != null) {
onConfirmed().then((value) {
if (value) {
AppNavigator.pop<bool>(ctx, result: value);
} else {
setState(() {
isOk = false;
});
AppDialog.showSnackBar(ctx, errorText ?? "处理失败请重试");
}
});
} else {
AppNavigator.pop<bool>(ctx, result: true);
}
},
child: Text(okText ?? S.of(ctx).confirm),
),
],
);
}
});
},
);
}