HarmonyOS 自定义模态弹窗查看文档CustomDialogController必须导入到引用的组件中才行,如果一个页面有5、6个,模态弹窗岂不是很麻烦管理,有什么优化的方案吗

自定义模态弹窗查看文档CustomDialogController必须导入到引用的组件中才行,如果一个页面有5、6个,模态弹窗岂不是很麻烦管理,有什么优化的方案吗

HarmonyOS
2024-12-20 15:13:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

弹窗是可以公用的。

1、同一页面多个弹窗是可以公用的

1.1、布局一样,数据不一样,可以传参,根据参数值展示

1.2、布局不一致,简单布局的话可以根据不同的@BuilderParam做展示,或者弹窗中写多种布局,弹窗接收一个变量,根据变量值来展示不同的内容

2、多个页面有相同的弹窗,可以封装成全局弹窗

参考demo:

//CustomDialogPage文件
import  {CustomDialogExample} from '../view/dialog/CustomDialogExample'
@Entry
@Component
struct CustomDialogPage {
  @State message: string = 'Hello World';
  @State type: number = 1;

  @Builder RowBuilder(){
    Row(){
      Text('123')
      Text('456')
    }
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }

  @Builder RowBuilder1(){
    Row(){
      Text('啦啦啦')
      Text('啦啦啦')
    }
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() },
      message:this.message,
      customBuilderParam: this.type === 0 ? this.RowBuilder :this.RowBuilder1, //根据type传入不同的@Builder函数
      type:this.type

    }),
  })

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

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

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('click me---0')
          .onClick(() => {
            this.type = 0
            this.dialogController.open()
          })
        Button('click me---1')
          .onClick(() => {
            this.type = 1
            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.
分享
微博
QQ
微信
回复
2024-12-20 17:59:12
相关问题
如何快速开发出一个自定义弹窗
1115浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
3660浏览 • 1回复 待解决
HarmonyOS 希望优化自定义弹窗使用
912浏览 • 1回复 待解决
基于bindSheet模态弹窗
2059浏览 • 1回复 待解决
HarmonyOS bindSheet半模态弹窗
1531浏览 • 1回复 待解决