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()

          }
        })

最终效果:

​​​​​​​

相关推荐
听麟12 分钟前
HarmonyOS 6.0+ PC端虚拟仿真训练系统开发实战:3D引擎集成与交互联动落地
笔记·深度学习·3d·华为·交互·harmonyos
江湖有缘13 分钟前
基于华为openEuler系统部署Gitblit服务器
运维·服务器·华为
一只大侠的侠39 分钟前
Flutter开源鸿蒙跨平台训练营 Day17Calendar 日历组件开发全解
flutter·开源·harmonyos
前端世界1 小时前
从一个 entry 写到十几个模块:鸿蒙模块化开发的真实落地方案(含可运行 Demo)
华为·harmonyos
一只大侠的侠1 小时前
Flutter开源鸿蒙跨平台训练营 Day14React Native表单开发
flutter·开源·harmonyos
听麟1 小时前
HarmonyOS 6.0+ APP AR文旅导览系统开发实战:空间定位与文物交互落地
人工智能·深度学习·华为·ar·wpf·harmonyos
空白诗2 小时前
高级进阶React Native 鸿蒙跨平台开发:slider 滑块组件 - 音量调节器完整实现
react native·react.js·harmonyos
●VON2 小时前
HarmonyOS应用开发实战(基础篇)Day01-《ArkTS基本知识》
学习·华为·harmonyos·鸿蒙·von
BlackWolfSky2 小时前
鸿蒙高级课程笔记2—应用性能优化
笔记·华为·harmonyos
ujainu2 小时前
护眼又美观:Flutter + OpenHarmony 鸿蒙记事本一键切换夜间模式(四)
android·flutter·harmonyos