HarmonyOS自定义弹出对话框CustomDialog并传递变量

HarmonyOS定义了一系列弹窗反馈类的组件​

和前端开发框架VUE3配套生态库element plus中的提供各种组件相比,还是要少一些。可能是手机端操作和PC端操作的差异导致的​

如果内置的弹窗不满足要求,可以基于CustomDialog自定义出各种个性化的反馈组件。

首先新建一个ets文件,使用CustomDialog装饰器定义一个自定义的dialog。其内部的语法和普通的Component是完全相同的,通过组合基础组件可以实现不同的布局。

注意需要传递的参数使用@Link进行修饰,在"实例化"的时候引用

javascript 复制代码
@CustomDialog
export default struct CustomActionSheet {
  @Link actionList: string[]
  controller: CustomDialogController
  cancel: () => void


  build() {
    Column() {
      ForEach(this.actionList, (item: string) => {
        Text(item)
          .width('100%')
          .height(24)
          .fontSize(16)
          .margin({ top: 2, bottom: 2 })
          .textAlign(TextAlign.Center)
          .onClick((e)=>{

          })
      }, item => item)


      Button('取消', { type: ButtonType.Normal }).borderRadius(4).width(80).onClick((e) => {
        this.controller.close()
        this.cancel()
      })
    }
    .justifyContent(FlexAlign.SpaceAround)
    .alignItems(HorizontalAlign.Center)
    .height(200)
    .padding({ left: 10, top: 20 })
    .borderRadius(8)
  }
}

​然后在父组件中实例化CustomDialogController,并在builder属性中引用这个CustomDialog​

javascript 复制代码
diaglogController: CustomDialogController = new CustomDialogController({
    builder: CustomActionSheet({
      cancel: this.onCancel,
      actionList: $actionList
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false
  })

在需要触发的地方调用即可​

javascript 复制代码
Button().onClick((e) => {
          if (this.diaglogController != undefined) {
            this.diaglogController.open()

          }
        })

最终效果:

​​​​​​​

相关推荐
liulian09162 分钟前
Flutter 网络状态与内容分享库:connectivity_plus 与 share_plus 的 OpenHarmony 适配指南总结
flutter·华为·学习方法·harmonyos
IntMainJhy4 分钟前
Flutter 引导页 Onboarding 第三方库 的鸿蒙化适配与实战指南
harmonyos
地鼠AI编程15 分钟前
DeepSeek V4预览版上线即开源,华为昇腾同步适配完成
华为
高工智能汽车20 分钟前
2026华为乾崑技术大会在京举行 乾崑智驾ADS 5等解决方案正式发布
华为
了不起的云计算V21 分钟前
以AI及自主创新重构教育数字化底座,华为擎云给出更优答案
人工智能·华为·重构
IntMainJhy22 分钟前
Flutter WebView 第三方库 内嵌 H5 页面的鸿蒙化适配与实战指南
flutter·华为·harmonyos
南村群童欺我老无力.29 分钟前
鸿蒙pc aboutToAppear与onPageShow的执行时机差异
华为·harmonyos
liulian09161 小时前
Flutter for OpenHarmony 用户登录与身份认证功能实现指南
flutter·华为·学习方法·harmonyos
jiejiejiejie_1 小时前
Flutter for OpenHarmony 登录认证小指南:用 Flutter 给鸿蒙 App 安上 “安全小锁”✨
安全·flutter·华为·harmonyos
前端不太难1 小时前
鸿蒙游戏:设备不再是边界
游戏·状态模式·harmonyos