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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
分享
微博
QQ
微信
回复
2024-12-20 19:09:51


相关问题
自定义状态获取
1600浏览 • 1回复 待解决
如何实现一个自定义询问
1193浏览 • 1回复 待解决
使用自定义函数创建一个UI组
972浏览 • 1回复 待解决
HarmonyOS 如何设置自定义颜色
712浏览 • 1回复 待解决
HarmonyOS 自定义不能全屏
922浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
1281浏览 • 1回复 待解决
HarmonyOS 自定义封装问题
826浏览 • 1回复 待解决
如何封装一个自定义Dialog对话
3045浏览 • 1回复 待解决
@Builder自定义构建函数,如何回参?
1133浏览 • 1回复 待解决
如何在自定义函数中创建一个UI组件
2591浏览 • 1回复 待解决
是否可以自定义权限文字
2551浏览 • 1回复 待解决
HarmonyOS app版本升级需要自定义
916浏览 • 1回复 待解决