HarmonyOS CustomDialog和 半模态 内部有页面跳转,push到新的页面,页面会被dialog遮挡

请问该如何解决 被遮挡的问题呢

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

请参考以下代码:

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

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

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

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

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

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

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

  public static pop(): void {
    AppRouters.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(() => {
          AppRouters.pop();
        })

        Column() {
          Text("dialogA")
            .fontColor(Color.White)
          Button("push pageC", { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouters.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(() => {
          AppRouters.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(() => {
            AppRouters.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(AppRouters.getInstance().getPathStack()) {
      Stack({ alignContent: Alignment.Center }) {
        Column() {
          Button('push PageA', { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouters.push('pageA')
            })
        }
      }
      .height("100%")
      .width("100%")
    }
    .hideTitleBar(true)
    .navDestination(this.PagesMap)
  }
}

// 封装路由工具类,并注册自定义弹窗组件
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();
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS CustomDialog跳转其他页面问题
47浏览 • 1回复 待解决
模态转场来实现弹框样式页面
931浏览 • 1回复 待解决
dialog跳转页面返回后dialog关闭
303浏览 • 1回复 待解决
HarmonyOS 模态会被软键盘顶起
18浏览 • 1回复 待解决