目录

弹窗在跳转新页面后未消失,覆盖在新页面上方

问题现象

弹窗在跳转新页面之后没有自动消失,显示在新页面上方,对新页面造成遮挡。

背景知识

问题定位

排查代码中使用的是模态弹窗还是非模态弹窗。如果使用了bindSheet或bindContentCover作为弹窗,则属于模态弹窗。如果使用CustomDialog等Dialog作为弹窗,需要检查弹窗配置中的isModal配置项,如果是false则是非模态弹窗。

分析结论

修改建议

完整示例代码如下:
@CustomDialog
@Component
struct CustomDialogExample {
  controller?: CustomDialogController;
  confirm: () => void = () => {
  };

  build() {
    Column() {
      Button('跳转页面')
        .onClick(() => {
          this.confirm();
        })
        .margin(20);
    }.borderRadius(10);
  }
}

@Entry
@Component
struct CustomDialogUser {
  @State isShow: boolean = false;
  pageInfo: NavPathStack = new NavPathStack();

  @Builder
  PagesMap(name: string) {
    if (name === 'PageOne') {
      PageOne();
    }
  }

  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      confirm: () => {
        // 跳转页面前隐藏弹窗
        this.dialogController?.close();
        // 跳转页面
        this.pageInfo.pushPath({ name: 'PageOne' });
      }
    }),
    autoCancel: true,
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
      console.info(`reason= ${dismissDialogAction.reason}`);
      console.info('dialog onWillDismiss');
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss();
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss();
      }
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
  });



  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null; // 将dialogController置空
  }

  @Builder
  myBuilder() {
    Scroll() {
      Column() {
        Button('跳转页面')
          .margin(10)
          .fontSize(20)
          .onClick(() => {
            // 跳转页面前隐藏弹窗
            this.isShow = false;
            // 跳转页面
            this.pageInfo.pushPath({ name: 'PageOne' });
          });
      };
    }
    .width('100%')
    .height('100%');
  }


  build() {
    Navigation(this.pageInfo) {
      Column({ space: 20 }) {
        Button('bindSheet', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.isShow = true;
          })
          .bindSheet($$this.isShow, this.myBuilder(), {
            height: '30%',
            backgroundColor: Color.Green,
            onWillAppear: () => {
            },
            onAppear: () => {
            },
            onWillDisappear: () => {
            },
            onDisappear: () => {
            }
          });
        Button('CustomDialog', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            if (this.dialogController != null) {
              this.dialogController.open();
            }
          });
      };
    }.title('NavIndex')
    .navDestination(this.PagesMap);
  }
}

@Builder
export function PageOneBuilder() {
  PageOne();
}

@Component
export struct PageOne {
  pageInfo: NavPathStack = new NavPathStack();
  message: string = 'Hello World';

  build() {
    NavDestination() {
      Column() {
        Text(this.message)
          .width('80%')
          .height(50)
          .margin(10);
      }.width('100%').height('100%');
    }.title('PageOne')
    .onBackPressed(() => {
      this.pageInfo.pop({ number: 1 }); // 弹出路由栈栈顶元素。
      return true;
    }).onReady((context: NavDestinationContext) => {
      this.pageInfo = context.pathStack;
    });
  }
}