HarmonyOS 自定义弹窗\(CustomDialog\)不直接在组件内new CustomDialogController\(\{\}\) ,弹窗无法显示?

自定义弹窗:

@CustomDialog
export struct TwoButtonDialog {
  onClickLeft?: () => void
  onClickRight?: () => void
  title: string = ''
  content: string = ''
  leftBtnText: string = '取消'
  rightBtnText: string = '确定'

  controller?: CustomDialogController

  build() {
    Column() {
      if (this.title.length > 0) {
        Text(this.title)
          .fontColor(0x333333)
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
      }

      Text(this.content)
        .fontColor(0x333333)
        .fontSize(14)
        .margin({ top: this.title.length > 0 ? 15 : 0 })
        .constraintSize({
          minHeight: 44
        })


      Row() {

        Button(this.leftBtnText)
          .height(44)
          .layoutWeight(1)
          .fontColor($r('app.color.app_main_color'))
          .fontSize(16)
          .borderWidth(1)
          .borderColor($r('app.color.app_main_color'))
          .backgroundColor(Color.White)
          .onClick(() => {
            if (this.controller) {
              this.controller.close()
            }
            if (this.onClickLeft) {
              this.onClickLeft()
            }
          })

        Divider()
          .width(11)
          .backgroundColor(Color.Transparent)
          .color(Color.Transparent)

        Button(this.rightBtnText)
          .height(44)
          .layoutWeight(1)
          .fontColor(Color.White)
          .fontSize(16)
          .backgroundColor($r('app.color.app_main_color'))
          .onClick(() => {
            if (this.controller) {
              this.controller.close()
            }
            if (this.onClickRight) {
              this.onClickRight()
            }
          })

      }
      .width('100%')
      .margin({ top: 20 })
    }
    .width('100%')
    .backgroundColor(Color.White)
    .borderRadius(12)
    .alignItems(HorizontalAlign.Center)
    .padding({
      left: 20,
      right: 20,
      top: 30,
      bottom: 30
    })
  }
}

//在封闭一个静态方法:
export class DialogUtil{

  static showDialog(){

    let dialogController: CustomDialogController = new CustomDialogController({
      builder:TwoButtonDialog({
        title:'提示',
        content:‘你好’,
        leftBtnText:'取消',
        rightBtnText:'确定',
        onClickLeft: ()=> {
        },
        onClickRight: ()=> {

        },
      }),
      backgroundColor:Color.White,
      cornerRadius:12
    });
    dialogController.open()

  }

}

然后在Page的按钮onClick方法中调用:DialogUtil.showDialog()

自定义弹窗无法显示

阅读 520
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进