全局UI方法-弹窗一警告弹窗(AlertDialog)

本文详细介绍了Flutter中的AlertDialog组件,包括其属性(如标题、消息、自动取消、按钮配置、对齐方式和偏移量)、不同类型参数对象(AlertDialogParamWithConfirm和AlertDialogParamWithButtons)以及使用示例。展示了如何设置不同样式和响应回调。
摘要由CSDN通过智能技术生成

1、描述

        显示警告弹窗组件,可设置文本内容与响应回调。

2、属性

名称参数类型参数描述
showAlertDialogParamWithConfirm | AlertDialogParamWithButtons定义并显示AlertDialog组件。

2.1、AlertDialogParamWithConfirm对象说明:

参数名称参数类型必填参数描述
titleResourceStr弹窗的标题。
messageResourceStr弹窗的内容。
autoCancelboolean

点击遮障层(弹窗之外的空白区域)时,是否关闭弹窗。

默认值:true。

confirm

{

value:ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action:() => void

}

确认按钮的文本内容,文本颜色,按钮背景色以及点击回调。
cancel

() => void

点击遮障层关闭Dialog时的回调。
alignmentDialogAlignment弹窗在竖直方向上的对其方式。默认值:DialogAlignment.Default。
offsetOffset弹窗相对于alignment所在位置的偏移量。
gridCountnumber弹窗容器宽度所占用栅格数。

2.2、AlertDialogParamWithButtons对象说明:

参数名称参数类型必填参数描述
titleResourceStr弹窗的标题。
messageResourceStr弹窗的内容。
autoCancelboolean

点击遮障层(弹窗之外的空白区域)时,是否关闭弹窗。

默认值:true。

primaryButton

{

value:ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action:() => void

}

按钮的文本内容,文本颜色,按钮背景色以及点击回调。
secondaryButton

{

value:ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action:() => void

}

按钮的文本内容,文本颜色,按钮背景色以及点击回调。
cancel

() => void

点击遮障层关闭Dialog时的回调。
alignmentDialogAlignment弹窗在竖直方向上的对其方式。默认值:DialogAlignment.Default。
offsetOffset弹窗相对于alignment所在位置的偏移量。
gridCountnumber弹窗容器宽度所占用栅格数。

2.3、DialogAlignment枚举说明

名称描述
Top垂直顶部对齐。
Center垂直居中对齐。
Bottom垂直底部对齐。
Default默认对齐。
TopStart左上对齐。
TopEnd右上对齐。
CenterStart左中对齐。
CenterEnd右中对齐。
BottomStart左下对齐。
BottomEnd右下对齐。

2.4、Offset

相对布局完成位置坐标偏移量。

名称

类型

必填

说明

dx

Length

水平方向偏移量。

dy

Length

竖直方向偏移量。

3、属性对应解析

1.属性只使用message时

2.属性使用title和message时

3.autoCancel

        当不设置该属性时,它的默认值是true,即点击弹窗之外的空白区域时,弹窗关闭。

        当设置autoCancel为false时,点击弹窗之外的空白区域时,弹窗不消失。

4.show属性使用模式不同,UI显示效果也不同

AlertDialogParamWithConfirm:显示的按钮只有一个。

AlertDialogParamWithButtons:按钮最多可以设置两个。

5.alignment:设置弹窗弹出的位置

6.offset:相对布局完成位置坐标偏移量

7.gridCount:其实就是弹窗的宽度占比吧?

4、示例

@Entry
@Component
struct AlertDialogPage {
  @State message: string = '显示警告弹窗组件,可设置文本内容与响应回调。'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .width("96%")
          .margin({ top: 12 })

        Button("AlertDialogParamWithConfirm")
          .width("96%")
          .fontSize(20)
          .margin({ top: 12 })
          .onClick(() => {
            AlertDialog.show({
              title: "我是AlertDialog的标题",
              message: "我是AlertDialog的描述内容",
              autoCancel: true,
              confirm: {
                value: "取消",
                fontColor: "#317aff",
                action: () => {
                  console.info("AlertDialog click confirm");
                }
              },
              cancel: () => {
                console.info("AlertDialog click cancel  autoCancel is true");
              },
              alignment: DialogAlignment.Bottom,
              offset: { dx: 0, dy: -20 },
              gridCount: 10
            })
          })

        Button("AlertDialogParamWithConfirm")
          .width("96%")
          .fontSize(20)
          .margin({ top: 12 })
          .onClick(() => {
            AlertDialog.show({
              title: "我是AlertDialog的标题",
              message: "我是AlertDialog的描述内容",
              autoCancel: false,
              primaryButton: {
                value: "取消",
                fontColor: "#317aff",
                // backgroundColor: Color.Blue,
                action: () => {
                  console.info("AlertDialog click primaryButton");
                }
              },
              secondaryButton: {
                value: "确认",
                fontColor: "#317aff",
                // backgroundColor: Color.Blue,
                action: () => {
                  console.info("AlertDialog click secondaryButton");
                }
              },
              cancel: () => {
                console.info("AlertDialog click cancel  autoCancel is true");
              },
              alignment: DialogAlignment.Bottom,
              offset: { dx: 0, dy: -20 },
              gridCount: 4

            })
          })


      }
      .width('100%')
      .height("100%")
    }
    .height('100%')
  }
}

5、效果图

  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxhzdm

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值