参考示例如下://Index.ets import { GlobalDialog } from './GlobalDialog' @Entry @Component export struct MainPage { @State message: string = 'Hello World'; build() { Row() { Column({ space: 20 }) { Button('Toast显示') .onClick(() => { new GlobalDialog().showDialog(undefined, 'test !!!') setTimeout(() => { new GlobalDialog().hide() }, 2000) }) } .width('100%') } .height('100%') } } // GlobalDialog.ets: @CustomDialog struct _GlobalDialog { controller: CustomDialogController close: () => void = () => { } image: ResourceStr | undefined = undefined @State msg: string = "" build() { Column() { Text(this.msg) .fontColor(Color.White) .width(px2vp(200)) .height(px2vp(200)) Image(this.image) .fitOriginalSize(true) .objectFit(ImageFit.None) } .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) .padding(12) .margin(30) .backgroundColor(Color.Black) .borderRadius(10) .width('100%') .transition( TransitionEffect.asymmetric( TransitionEffect.translate({ y: -100 }).animation({ duration: 100, curve: Curve.Sharp }) , TransitionEffect.IDENTITY )) } } let _dialogController: CustomDialogController | null let _cancelCallBack: (() => void) | undefined @Component export struct GlobalDialog { showDialog( image: Resource | undefined, msg: string, cancelCallBack?: () => void, alignment: DialogAlignment = DialogAlignment.Top, offset: Offset = { dx: 0, dy: 0 }, showInSubWindow: boolean = false, isModal: boolean = false, ): void { this.hide() _cancelCallBack = cancelCallBack let animate: AnimateParam = { duration: 90, delay: 0, curve: Curve.EaseInOut } _dialogController = new CustomDialogController({ builder: _GlobalDialog({ image: image, msg: msg }), autoCancel: false, cancel: () => { _dialogController = null if (_cancelCallBack) { _cancelCallBack() } }, customStyle: true, alignment: alignment, offset: offset, maskColor: 0x01000000, openAnimation: animate, closeAnimation: animate, showInSubWindow: showInSubWindow, isModal: isModal, }) _dialogController.open() } hide() { if (_dialogController) { _dialogController.close() _dialogController = null if (_cancelCallBack) { _cancelCallBack() } } } build() { } }
参考示例如下: