NavDestinationMode.DIALOG是一个弹窗模式的NavDestination页面,每次push一个Dialog页面,它就会出现在当前弹窗之上,形成层级结构。它们的层级顺序即为路由栈中的顺序。
@Entry @Component struct NavDialog { @Provide('NavPathStack') pageStack: NavPathStack = new NavPathStack(); @Builder PagesMap(name: string) { if (name === 'DialogPage1') { DialogPage1(); } else if (name === 'DialogPage2') { DialogPage2(); } else if (name === 'DialogPage3') { DialogPage3(); } } build() { Navigation(this.pageStack) { Button('依次打开弹窗1,2,3') .margin(20) .width('80%') .onClick(() => { this.pageStack.pushPathByName('DialogPage1', ''); this.pageStack.pushPathByName('DialogPage2', ''); this.pageStack.pushPathByName('DialogPage3', ''); }); Button('依次打开弹窗1,2,3后依次关闭弹窗3,2,1') .margin(20) .width('80%') .onClick(() => { this.pageStack.pushPathByName('DialogPage1', ''); this.pageStack.pushPathByName('DialogPage2', ''); this.pageStack.pushPathByName('DialogPage3', ''); setTimeout(() => { this.pageStack.removeByName('DialogPage3'); }, 1000); setTimeout(() => { this.pageStack.removeByName('DialogPage2'); }, 2000); setTimeout(() => { this.pageStack.removeByName('DialogPage1'); }, 3000); }); } .mode(NavigationMode.Stack) .title('Main') .navDestination(this.PagesMap); } } @Component export struct DialogPage1 { @Consume('NavPathStack') pageStack: NavPathStack; build() { NavDestination() { Stack({ alignContent: Alignment.Center }) { Column() { Text('Dialog NavDestination1') .fontSize(20) .margin({ bottom: 100 }); Button('Close').onClick(() => { this.pageStack.pop(); }).width('30%'); } .justifyContent(FlexAlign.Center) .backgroundColor(Color.White) .borderRadius(10) .height('30%') .width('80%'); }.height('100%').width('100%'); } .backgroundColor('rgba(0,0,0,0.5)') .hideTitleBar(true) .mode(NavDestinationMode.DIALOG); } } @Component export struct DialogPage2 { @Consume('NavPathStack') pageStack: NavPathStack; build() { NavDestination() { Stack({ alignContent: Alignment.Center }) { Column() { Text('Dialog NavDestination2') .fontSize(20) .margin({ bottom: 100 }); Button('Close').onClick(() => { this.pageStack.pop(); }).width('30%'); } .justifyContent(FlexAlign.Center) .backgroundColor(Color.White) .borderRadius(10) .height('30%') .width('80%'); }.height('100%').width('100%'); } .backgroundColor('rgba(0,0,0,0.5)') .hideTitleBar(true) .mode(NavDestinationMode.DIALOG); } } @Component export struct DialogPage3 { @Consume('NavPathStack') pageStack: NavPathStack; build() { NavDestination() { Stack({ alignContent: Alignment.Center }) { Column() { Text('Dialog NavDestination3') .fontSize(20) .margin({ bottom: 100 }); Button('Close').onClick(() => { this.pageStack.pop(); }).width('30%'); } .justifyContent(FlexAlign.Center) .backgroundColor(Color.White) .borderRadius(10) .height('30%') .width('80%'); }.height('100%').width('100%'); } .backgroundColor('rgba(0,0,0,0.5)') .hideTitleBar(true) .mode(NavDestinationMode.DIALOG); } }