当前的设计是这样的弹窗的层级和页面是相互独立的,弹窗始终位于页面之上, api12后路由跳转时弹窗不会关闭可参考以下几种方法实现需求:1、自定义弹框//使用stack可以实现假的dialog覆盖原页面上面 @Entry @Component struct Index { @State textValue: string = 'Hello World' // 显隐控制设置为不占用 @State visible: Visibility = Visibility.None build() { 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(() => { //跳转下一页面 }).backgroundColor(0xffffff).fontColor(Color.Red) }.margin({ bottom: 10 }) } .backgroundColor(0xffffff) .visibility(this.visible) .clip(true) .borderRadius(20) } } }.width('95%')//设置弹窗宽度 } } } //使用NavDestination的Dialog模式实现自定义弹窗 @Component export struct PrivacyDialog { @Consume('pageInfo') pageStack: NavPathStack; @State isAgree: string = "Not Agree"; build() { NavDestination() { Stack({ alignContent: Alignment.Center }) { Column() { }.width("100%").height("100%").backgroundColor('rgba(0,0,0,0.5)') Column() { Text("注册应用账号").fontSize(30).height('20%') Text("请您仔细阅读一下协议并同意,我们将全力保护您的个人信息安全,您可以使用账号登录APP。").height('40%') Divider() Row() { Text("《应用隐私政策》").onClick(ent => { let pathInfo: NavPathInfo = new NavPathInfo('PrivacyItem', null, (popInfo: PopInfo) => { this.isAgree = popInfo.result.toString(); }) this.pageStack.pushDestination(pathInfo, true) }) Text(this.isAgree) }.height('20%') Divider() Row() { Button("不同意").onClick(ent => { this.pageStack.pop("Not Agree", true) }).width('30%') Button("同意").onClick(ent => { this.pageStack.pop("Agree", true) }).width('30%') }.height('20%') }.backgroundColor(Color.White).height('50%').width('80%') } }.hideTitleBar(true).mode(NavDestinationMode.DIALOG) } }2、跳转的时候关闭弹窗,在页面回调或者onWillShow中重新弹出弹框// PageOne.ets import { BusinessError } from '@kit.BasicServicesKit'; import { promptAction } from '@kit.ArkUI'; class TmpClass{ count:number = 10 } @Builder export function PageOneBuilder(name: string, param: Object) { PageOne() } @Component export struct PageOne { pageInfo: NavPathStack = new NavPathStack(); @State message: string = 'Hello World' private customDialogComponentId: number = 0 private configAgree: boolean = false //协议是否查看或者签署 @Builder customDialogComponent(configAgree: boolean) { Column() { if (!configAgree){ Text('弹窗').fontSize(30) Row({ space: 50 }) { Button("确认").onClick(() => { promptAction.closeCustomDialog(this.customDialogComponentId) this.pushNextPage() }) Button("取消").onClick(() => { promptAction.closeCustomDialog(this.customDialogComponentId) }) } }else{ Text('已查看或者签署完') } }.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween) } openDialog(configAgree: boolean){ promptAction.openCustomDialog({ builder: () => { this.customDialogComponent(configAgree) } }).then((dialogId: number) => { this.customDialogComponentId = dialogId }) } pushNextPage(){ let tmp = new TmpClass() this.pageInfo.pushDestinationByName('pageTwo', tmp, (popInfo)=>{ console.log('返回参数:popInfo',JSON.stringify(popInfo)) //根据回调判断是否查看签署协议 if (true) { this.configAgree = true this.openDialog(this.configAgree) } }).catch((error: BusinessError)=>{ console.error(`[pushDestinationByName]failed, error code = ${error.code}, error.message = ${error.message}.`); }).then(()=>{ console.error('[pushDestinationByName]success.'); }); } build() { NavDestination() { Column() { Button('隐私协议') .width('80%') .height(40) .margin(20) .onClick(()=>{ this.openDialog(this.configAgree) }) }.width('100%').height('100%') }.title('pageOne') .onBackPressed(() => { this.pageInfo.pop({number: 1}) // 弹出路由栈栈顶元素。 return true }).onReady((context: NavDestinationContext) => { this.pageInfo = context.pathStack; }) } }
当前的设计是这样的弹窗的层级和页面是相互独立的,弹窗始终位于页面之上, api12后路由跳转时弹窗不会关闭可参考以下几种方法实现需求:
1、自定义弹框
2、跳转的时候关闭弹窗,在页面回调或者onWillShow中重新弹出弹框