HarmonyOS 自定义弹框的描述显示一个@Builder装饰的函数

自定义弹框的描述显示一个@Builder装饰的函数

HarmonyOS
2024-12-20 16:47:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考以下demo:

@Builder
function customDialogBuilder(price:string,id_no:string) {
  Column({space:15}) {
    Row() {
      Text('运费:' + price)
        .fontSize(10)
    }
    Row() {
      Text('货单:' + id_no)
        .fontSize(10)
    }
  }
  .width('100%')
  .height(200)
  .padding(5)
  .backgroundColor("#FF0000")
}

@CustomDialog
struct CustomDialogExample {
  @Link title: string
  @Link price: string
  @Link id_no: string
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  build() {
    Column() {
      Text(this.title).fontSize(20).margin({ top: 10, bottom: 10 })
      customDialogBuilder(this.price,this.id_no)
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }.borderRadius(10)
  }
}

@Entry
@Component
struct Index {

  @State title: string = ''
  @State price: string = ''
  @State id_no: string = ''
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => {
        this.onCancel()
      },
      confirm: () => {
        this.onAccept()
      },
      title: $title,
      price: $price,
      id_no: $id_no,
    }),
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
  })

  // 在自定义组件即将析构销毁时将dialogControlle置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  build() {
    Row() {
      Column() {
        Button('自定义弹框')
          .width('100vp')
          .height('100vp')
          .onClick(() => {
            if (this.dialogController != null) {
              this.title = '自定义弹框title'
              this.price = '100元'
              this.id_no = '1234567895549'
              this.dialogController.open()
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-20 19:09:51
相关问题
自定义状态获取
1214浏览 • 1回复 待解决
如何实现一个自定义询问
780浏览 • 1回复 待解决
使用自定义函数创建一个UI组
599浏览 • 1回复 待解决
HarmonyOS 如何设置自定义颜色
325浏览 • 1回复 待解决
HarmonyOS 自定义不能全屏
480浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
889浏览 • 1回复 待解决
HarmonyOS 自定义封装问题
386浏览 • 1回复 待解决
如何封装一个自定义Dialog对话
2577浏览 • 1回复 待解决
@Builder自定义构建函数,如何回参?
685浏览 • 1回复 待解决
是否可以自定义权限文字
2053浏览 • 1回复 待解决
如何在自定义函数中创建一个UI组件
2122浏览 • 1回复 待解决
HarmonyOS app版本升级需要自定义
544浏览 • 1回复 待解决
HarmonyOS 自定义遮罩透传问题
310浏览 • 1回复 待解决