HarmonyOS CustomDialog中打开Component类型的页面,新开的页面在dialog下层?

CustomDialog中打开component类型的新页面,导致新开的页面在dialog的下层。期待新开的页面在dailog上层

阅读 439
1 个回答

想要实现Dialog跳转页面之后 再返回Dialog不消失 ,可以使用Stack组件模拟实现Dialog的效果。

参考demo:

import router from '@ohos.router';
@Entry
@Component
struct a {
  @State textValue: string = 'Hello World'
  // 显隐控制设置为不占用
  @State visible: Visibility = Visibility.None

  build() {
    // 使用stack可以实现假的dialog覆盖原页面上面
    Stack() {
      Row() {
        // 初始页面
        Column() {
          Text('Hello World')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
          // 触发dialog的地方
          Button('click')
            .onClick(() => {
              //用于检测点击事件是否透传到原来的页面,我测了一下是没有透传的,符合dialog规范
              console.log("hit me!")
              if (this.visible == Visibility.Visible) {
                this.visible = Visibility.None
              } else {
                this.visible = Visibility.Visible
              }
            })
            .backgroundColor(0x777474)
            .fontColor(0x000000)
        }
        .width('100%')
      }
      .height('100%')
      .backgroundColor(0x885555)

      Column() {
        // 这个可以调节对话框效果,栅格布局,xs,sm,md,lg分别为四种规格
        // 下面的breakpoints是用来区别当前属于哪个类型的断点
        // gridRow里的栅格数量为总数,gridCol里的就是偏移和假Dialog所占据的栅格数
        GridRow({
          columns:{xs:10 ,sm: 4, md: 8, lg: 12},
          breakpoints: { value: ["400vp", "600vp", "800vp"],
            reference: BreakpointsReference.WindowSize },
        })
        {
          GridCol({
            span:{xs:10 ,sm: 2, md: 4, lg: 8},
            offset:{xs:2,sm: 1, md: 2, lg: 2}
          }){

            // 这里放的就是原Dialog里的column里的东西,稍微改改应该就可以用了
            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.visible == Visibility.Visible) {
                      this.visible = Visibility.None
                    } else {
                      this.visible = Visibility.Visible
                    }

                  }).backgroundColor(0xffffff).fontColor(Color.Black)
                Button('jump')
                  .onClick(() => {
                    router.pushUrl({
                      url: 'pages/Index'
                    })
                  }).backgroundColor(0xffffff).fontColor(Color.Red)
              }.margin({ bottom: 10 })
            }
            .backgroundColor(0xffffff)
            .visibility(this.visible)
            .clip(true)
            .borderRadius(20)
          }
        }
      }.width('95%')//设置弹窗宽度
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进