showAlert method

Future showAlert({
  1. required String title,
  2. String subTitle = '',
  3. Color titleColor = Colors.black,
  4. Color subTitleColor = Colors.grey,
  5. double titleFontSize = 20,
  6. double subTitleFontSize = 16,
  7. required List<DialogAction> actions,
  8. bool barrierDismissible = true,
})

显示通用对话框

title 对话框标题(必填) subTitle 对话框副标题(默认空字符串) titleColor 标题文本颜色(默认黑色) subTitleColor 副标题文本颜色(默认灰色) titleFontSize 标题字体大小(默认20) subTitleFontSize 副标题字体大小(默认16) actions 操作按钮列表(必填) barrierDismissible 点击背景是否可关闭(默认true)

返回值 Future对象,可用于等待对话框关闭

Implementation

Future showAlert({
  required String title,
  String subTitle = '',
  Color titleColor = Colors.black,
  Color subTitleColor = Colors.grey,
  double titleFontSize = 20,
  double subTitleFontSize = 16,
  required List<DialogAction> actions,
  bool barrierDismissible = true,
}) async {
  return showDialog(
      context: this,
      builder: (ctx) {
        return AlertDialog(
          title: Text(
            title,
            style: TextStyle(color: titleColor, fontSize: titleFontSize),
          ),
          content: Text(
            subTitle,
            style:
                TextStyle(color: subTitleColor, fontSize: subTitleFontSize),
          ),
          actions: actions.map((e) {
            return InkWell(
                child: Container(
                  height: 35,
                  width: 110,
                  padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: e.color,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Text(
                    e.title,
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  ),
                  margin: EdgeInsets.all(8),
                ),
                onTap: () {
                  if (e.action != null) {
                    e.action!();
                  }
                  if (e.close) {
                    Navigator.of(ctx).pop();
                  }
                });
          }).toList(),
        );
      },
      barrierDismissible: barrierDismissible);
}