使用CustomDialog实现多个弹窗链式调用时,当在第一个弹窗中打开第二个弹窗时,第二个弹窗未能正确显示,实际显示的还是第一个弹窗的内容。
错误代码如下:
@CustomDialog
struct MyDialog1 {
controller1: CustomDialogController
controller2: CustomDialogController
title: string=''
build() {
Row() {
Column({ space: 10 }) {
Text(this.title)
.fontSize(30)
.fontColor(Color.Blue)
Button('点击打开弹窗2')
.onClick(() => {
this.controller2.open()
}).backgroundColor(0xffffff).fontColor(Color.Red)
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller1.close()
}).backgroundColor(0xffffff).fontColor(Color.Black)
}.width('100%')
}.width('50%')
}.height('50%')
}
}
// 弹窗组件
@CustomDialog
struct MyDialog2 {
controller: CustomDialogController
title: string=''
build() {
Row() {
Column({ space: 10 }) {
Text(this.title)
.fontSize(30)
.fontColor(Color.Blue)
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller.close()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
this.controller.close()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.width('100%')
}.margin({ bottom: 10 })
}.height('50%')
}
}
@Entry
@Component
struct DialogTest {
@State dialogTitle: string = ''
@State dialogData: string = ''
dialogController2: CustomDialogController = new CustomDialogController({
builder: MyDialog2({
title: '弹窗2',
}),
customStyle: false
})
dialogController1: CustomDialogController = new CustomDialogController({
builder: MyDialog1({
controller2: this.dialogController2,
title: '弹窗1',
}),
customStyle: false
})
build() {
Row() {
Column({ space: 10 }) {
Button('点击打开弹窗1')
.onClick(() => {
this.dialogController1.open()
})
}.width('100%')
}.height('100%')
}
} CustomDialog的打开与关闭都与CustomDialog的CustomDialogController有关,当多个弹窗链式调用时会存在多个CustomDialogController,会出现上述问题的原因可能是由于使用了错误的CustomDialogController,或未将指向自己的controller置于最后导致打开了错误的弹窗。根据上述错误代码中:
controller1: CustomDialogController controller2: CustomDialogController
错误的弹窗是由于未将自身的controller放置于其它controller之后。
在CustomDialog中传入多个其他的controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面。
为了正确的实现弹窗的嵌套使用,可以通过以下方式实现:
import { PromptAction } from '@kit.ArkUI';
@Entry
@Component
struct Index {
private customDialogComponentId: number = 0;
private customDialogComponentTwoId: number = 0;
private promptAction: PromptAction = this.getUIContext().getPromptAction();
@Builder
customDialogComponentTwo() {
Column() {
Text('第二个弹窗');
Button('关闭弹窗').onClick(() => {
// 记录当前弹窗ID
this.promptAction.closeCustomDialog(this.customDialogComponentTwoId);
});
};
}
@Builder
customDialogComponent() {
Column() {
Text('第一个弹窗').fontSize(30);
Text('打开第二个弹窗')
.onClick(() => {
this.promptAction.openCustomDialog({
width: '70%',
builder: () => {
this.customDialogComponentTwo();
}
})
.then((dialogId: number) => {
// 记录当前弹窗ID
this.customDialogComponentTwoId = dialogId;
});
});
Row({ space: 50 }) {
Button('关闭弹窗').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentId);
});
};
}.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween);
}
build() {
Row() {
Column({ space: 20 }) {
Text('打开第一个弹窗')
.fontSize(30)
.onClick(() => {
this.promptAction.openCustomDialog({
builder: () => {
this.customDialogComponent();
},
}).then((dialogId: number) => {
this.customDialogComponentId = dialogId;
});
});
}
.width('100%');
}
.height('100%');
}
}