HarmonyOS 弹窗优化

CustomDialogController后期有没有可能使用上下文传参的方式创建?

HarmonyOS
2024-12-24 16:22:05
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

当前不支持将弹窗代码分离到类中,可以将不同弹窗的UI样式与函数封装到自定义组件中,通过自定义组件调用灵活复用弹窗功能。

参考demo如下:

@Entry
@Component
struct Index8 {
  @State message: string = 'Hello World';
  @State ifOpen: boolean = false;

  build() {
    Column() {
      Button('打开弹窗').onClick(() => {
        this.ifOpen = true
      })
      CustomDialogChild({
        //控制弹窗内容展示
        textValue: '测试在别的页面传参并打开弹窗',
        isOpen: this.ifOpen,
        //控制弹窗UI样式渲染
        param: '1'
      })
    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}

/**
 * 将UI样式封装在自定义组件中,并通过@BuilderParam传递给具体弹窗
 */
@Component
export struct CustomDialogChild {
  @Prop textValue: string = ''
  @State inputValue: string = 'click me'
  @Link @Watch('onStateChange') isOpen: boolean;
  @State param: string = '1'
  /**
   * 构建UI样式
   */
  @Builder
  setUI() {
    Text('这是一个UI')
  }

  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => {
        this.onCancel()
      },
      confirm: () => {
        this.onAccept()
      },
      getUI: () => {
        //控制不同样式的UI渲染
        if (this.param === '1') {
          this.setUI()
        } else {

        }
      },
      textValue: $textValue,
      inputValue: $inputValue,
      isOpen: $isOpen
    }),
    cancel: this.exitApp,
    autoCancel: false,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
  })

  onStateChange() {
    if (this.isOpen) {
      this.dialogController?.open()
    } else {
      this.dialogController?.close();
    }
  }

  // 在自定义组件即将析构销毁时将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')
  }

  exitApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
  }
}

@CustomDialog
struct CustomDialogExample {
  @Link textValue: string
  @Link inputValue: string;
  @Link isOpen: boolean;
  @BuilderParam
  getUI: () => void
  controller?: CustomDialogController
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  build() {
    Column() {
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.cancel();
              this.isOpen = false;
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            if (this.controller != undefined) {
              this.inputValue = this.textValue
              this.controller.close()
              this.isOpen = false;
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
      this.getUI()
    }.borderRadius(10)
    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
  }
}
分享
微博
QQ
微信
回复
2024-12-24 19:09:18
相关问题
HarmonyOS 希望优化自定义弹窗的使用
465浏览 • 1回复 待解决
HarmonyOS 路由使用优化
250浏览 • 1回复 待解决
HarmonyOS 性能分析和优化
605浏览 • 1回复 待解决
HarmonyOS Tab + List 性能优化
407浏览 • 1回复 待解决
HarmonyOS Swiper滑动性能优化
113浏览 • 1回复 待解决
HarmonyOS 弹窗onWillDismiss
120浏览 • 1回复 待解决
HarmonyOS 怎么优化so包大小
161浏览 • 1回复 待解决
HarmonyOS webview加载速度 性能优化
434浏览 • 1回复 待解决
HarmonyOS web页面的性能优化
272浏览 • 1回复 待解决
HarmonyOS 加载弹窗模版
445浏览 • 1回复 待解决
HarmonyOS 弹窗高度问题
236浏览 • 1回复 待解决
HarmonyOS 底部弹窗问题
939浏览 • 1回复 待解决
HarmonyOS 弹窗显示异常
373浏览 • 1回复 待解决
HarmonyOS 权限弹窗问题
264浏览 • 1回复 待解决
HarmonyOS 自定义弹窗如何更新弹窗的UI
341浏览 • 1回复 待解决
HarmonyOS 全局弹窗显示
184浏览 • 1回复 待解决
HarmonyOS 全局弹窗问题
814浏览 • 1回复 待解决
HarmonyOS 弹窗异常问题
771浏览 • 1回复 待解决
HarmonyOS 全局弹窗demo
210浏览 • 1回复 待解决
HarmonyOS 权限弹窗问题
296浏览 • 1回复 待解决