HarmonyOS costumdialog弹窗在使用router跳转到下一个页面后,返回原先页面弹框会消失

使用了一个弹框承载隐私协议相关内容,可以通过弹框中的链接路由到隐私协议详情页面,但从详情页返回原来的页面时,原先的弹框就消失了,有什么办法可以在这种场景下,保持原先页的弹框不消失?

HarmonyOS
2024-08-09 15:48:17
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

当前这样的现象是这个是CustomDialog的规格,有三种方式可以规避:

1.是通过stack和自定义组件模拟一个弹窗,并通过visibility控制弹窗的显示和消失。

2.可以在业务逻辑上修改,比如用另外一个弹窗实现隐私详情页,这样就不涉及到页面的跳转,返回之前的弹窗就不会关闭。3.用Naviagtion的NavDestinationMode.DIALOG对于方式1,参考样例demo:

// Maker 
import { map, mapCommon, MapComponent } from '@kit.MapKit'; 
import { AsyncCallback } from '@kit.BasicServicesKit'; 
@Component 
export struct MapMarker { 
  @Consume map?: map.MapComponentController; 
  private mapCallback?: AsyncCallback<map.MapComponentController>; 
  MapOptions?: mapCommon.MapOptions; 
  // Marker初始化参数 
  MarkerOptions?: mapCommon.MarkerOptions; 
  callback?: AsyncCallback<map.MapComponentController>; 
  async aboutToAppear() { 
    this.mapCallback = async (err, mapController) => { 
      if (!err) { 
        this.map = mapController; 
        console.log('this.map'+JSON.stringify(this.map)) 
        this.callback?.(err, mapController) 
      } 
    } 
    build() { 
      MapComponent({ mapOptions: this.MapOptions, mapCallback: this.mapCallback }).width('100%').height('100%') 
    } 
  }import router from '@ohos.router'; 
  @Entry 
  @Component 
  struct First { 
  @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) 
      //这里开始是构造弹窗效果主要需要修改的地方,首先是加了一个半透明灰色的蒙层效果 
      Text('') 
        .onClick(() => { 
          if (this.visible == Visibility.Visible) { 
            this.visible = Visibility.None 
          } else { 
            this.visible = Visibility.Visible 
          } 
        }) 
        .width('100%') 
        .height('100%') 
          // 透明度可以自己调节一下 
        .opacity(0.16) 
        .backgroundColor(0x000000) 
        .visibility(this.visible) 
      Column() { 
        // 这个可以调节对话框效果,栅格布局,xs,sm,md,lg分别为四种规格 
        // 下面的breakpoints是用来区别当前属于哪个类型的断点 
        // gridRow里的栅格数量为总数,gridCol里的就是偏移和假Dialog所占据的栅格数 
        GridRow({ 
          columns:{xs:1 ,sm: 4, md: 8, lg: 12}, 
          breakpoints: { value: ["400vp", "600vp", "800vp"], 
            reference: BreakpointsReference.WindowSize }, 
        }) 
        { 
          GridCol({ 
            span:{xs:1 ,sm: 2, md: 4, lg: 8}, 
            offset:{xs:0 ,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/Second' 
                    }) 
                  }).backgroundColor(0xffffff).fontColor(Color.Red) 
              }.margin({ bottom: 10 }) 
            } 
            .backgroundColor(0xffffff) 
            .visibility(this.visible) 
            .clip(true) 
            .borderRadius(20) 
          } 
        } 
      }.width('95%')//设置弹窗宽度 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-09 19:25:59
相关问题
弹窗跳转到页面返回弹窗消失
875浏览 • 1回复 待解决
HarmonyOS如何跳转到发短信页面
65浏览 • 1回复 待解决
一个页面怎么实现多个AbilitySlice?
13967浏览 • 5回复 待解决