HarmonyOS 基于Navigation和NavDestination的跳转方式的弹窗问题

项目全部跳转都是基于Navigation和NavDestination的跳转方式,但这种方式有个问题,当弹出Dialog或者Sheet的时候,再从Dialog里点击内容后,想要跳转到新的界面,这个时候新的界面就会出现在对话框之下,也就是被对话框给盖住了。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

请参考示例如下:

interface RouterParams {
  name?: string,
  onPop?: (data: PopInfo) => void
}

// 封装路由工具类,并注册自定义弹窗组件
class AppRouter {
  private static instance = new AppRouter();
  private pathStack: NavPathStack = new NavPathStack();

  public static getInstance(): AppRouter {
    return AppRouter.instance;
  }

  public getPathStack(): NavPathStack {
    return this.pathStack;
  }

  private pushPath(name: string): void {
    this.pathStack.pushPath({ name: name })
  }

  public static push(name: string): void {
    AppRouter.instance.pushPath(name);
  }

  public static openDialog(name: string, params?: RouterParams): void {
    AppRouter.instance.pathStack.pushPath({
      name: name, param: params, onPop: (data: PopInfo) => {
        if (params?.onPop) {
          params.onPop!(data);
        }
      }
    });
  }

  public static pop(): void {
    AppRouter.instance.pathStack.pop();
  }
}

@Component
  // NavDestinationMode.DIALOG
struct DefaultDialog {
  build() {
    NavDestination() {
      Stack({ alignContent: Alignment.Center }) {
        Column() {
        }
        .width("100%")
        .height("100%")
        .backgroundColor('rgba(0,0,0,0.5)')
        .transition(
          TransitionEffect.OPACITY.animation({
            duration: 300,
            curve: Curve.Friction
          })
        )
        .onClick(() => {
          AppRouter.pop();
        })

        Column() {
          Text("dialogA")
            .fontColor(Color.White)
          Button("push pageC", { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouter.push("pageC")
            })
        }
        .width("50%")
        .height("30%")
        .backgroundColor('#ffae2d2d')
        .transition(
          TransitionEffect.scale({ x: 0, y: 0 }).animation({
            duration: 300,
            curve: Curve.Friction
          })
        )
      }
      .width("100%")
      .height("100%")
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
  }
}

@Component
struct PageA {
  @Consume('pageInfos') pageInfos: NavPathStack;

  build() {
    NavDestination() {
      Button('push dialogA', { stateEffect: true, type: ButtonType.Capsule })
        .onClick(() => {
          AppRouter.openDialog("DefaultDialog");
        }).margin(10)
    }.title('PageA')
  }
}

@Component
struct PageC {
  @Consume('pageInfos') pageInfos: NavPathStack;

  build() {
    NavDestination() {
      Column() {
        Text('pageC')
          .margin(10)
        Button('返回', { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            AppRouter.pop();
          }).margin(10)
      }
    }.mode(NavDestinationMode.STANDARD)
  }
}

@Entry
@Component
struct pageRoot01 {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
  // 显隐控制设置为不占用
  @State visible: Visibility = Visibility.None

  @Builder
  PagesMap(name: string) {
    if (name == 'pageA') {
      PageA()
    } else if (name == 'pageC') {
      PageC()
    } else if (name == 'DefaultDialog') {
      DefaultDialog()
    }
  }

  build() {
    // 在根页面中注册NavPathStack
    Navigation(AppRouter.getInstance().getPathStack()) {
      Stack({ alignContent: Alignment.Center }) {
        Column() {
          Button('push PageA', { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouter.push('pageA')
            })
        }
      }
      .height("100%")
      .width("100%")
    }
    .hideTitleBar(true)
    .navDestination(this.PagesMap)
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
Navigation页面跳转问题
488浏览 • 1回复 待解决
HarmonyOS Navigation NavRouter NavDestination
62浏览 • 1回复 待解决
HarmonyOS dialog弹窗跳转问题
38浏览 • 1回复 待解决
HarmonyOS 跳转方式RouterNavigator
446浏览 • 1回复 待解决
基于bindSheet半模态弹窗
1054浏览 • 1回复 待解决
HarmonyOS Navigation动态跳转页面问题
33浏览 • 1回复 待解决
基于原生能力跨应用跳转
876浏览 • 1回复 待解决
Navigation实现动态路由方式
762浏览 • 1回复 待解决
HarmonyOS 自定义弹窗点击跳转问题
21浏览 • 1回复 待解决
HarmonyOS NavigationTab组件问题
963浏览 • 1回复 待解决