CustomDialog的默认层级高于页面,可以使用NavDestination的Dialog模式实现实现customdialog弹窗,再调用dialoghub弹窗。
【解决方案】
NavDestinationMode.DIALOG实现自定义弹窗方案,示例代码如下:
创建Navigation根页面:
@Entry
@Component
struct Index {
@Provide('pathStack') pathStack: NavPathStack = new NavPathStack();
build() {
Navigation(this.pathStack) {
Column() {
Button('Navigation页')
.onClick(() => {
this.pathStack.pushPathByName('Index2', null)
})
.backgroundColor(0x777474)
.fontColor(0x000000)
}
.width('100%')
.height('100%')
}
.mode(NavigationMode.Auto)
.width('100%')
.height('100%')
}
}
创建Index2子页面,并定义到弹窗页面的路由:
@Builder
export function Index2Builder() {
Index2()
}
@Component
export struct Index2 {
@Consume('pathStack') pathStack: NavPathStack;
build() {
NavDestination() {
Row() {
Column() {
Button('click')
.onClick(() => {
this.pathStack.pushPathByName('CustomDialog', ''); // 推送弹窗页面
})
.backgroundColor(0x777474)
.fontColor(0x000000)
}
.width('100%')
}
.height('100%')
.backgroundColor(0x885555)
}
.title('Index2')
}
}
创建并自定义CustomDialog子页面,并定义到Index3页面的路由:
@Builder
export function CustomDialogBuilder() {
CustomDialog()
}
@Component
export struct CustomDialog {
@Consume('pathStack') pathStack: NavPathStack;
build() {
NavDestination() {
Stack({ alignContent: Alignment.Center }) {
Text('123123') // 弹窗遮罩
.onClick(() => {
this.pathStack.pop() // 点击遮罩散出该页面,达到关闭弹窗效果
})
.width('100%')
.height('100%')
.opacity(0.1) // 遮罩透明度调节
.backgroundColor(0x000000)
Column() {
Text('我是自定义弹窗')
Button('jump')
.onClick(() => {
this.pathStack.pushPathByName('Index3', ''); // 弹窗内推送Index3页面
}).backgroundColor(0xffffff).fontColor(Color.Red)
}
.justifyContent(FlexAlign.Center)
.backgroundColor(Color.White)
.borderRadius(10)
.height(100)
.width('95%')
}.height('100%').width('100%')
}
.backgroundColor('rgba(0,0,0,0.5)')
.hideTitleBar(true)
.mode(NavDestinationMode.DIALOG)
}
}
创建Index3子页面:
@Builder
export function Index3Builder() {
Index3()
}
@Component
struct Index3 {
@Consume('pathStack') pathStack: NavPathStack;
build() {
NavDestination() {
Text('Index3')
.width('100%')
.height('100%')
.backgroundColor(Color.Blue)
}
.title('Index3')
}
}