HarmonyOS 如何用promptAction实现推送通知效果?

类似推送一样,从顶部划下来一条信息,过个几秒又自己从顶部划上去消失

阅读 493
1 个回答

参考示例如下:

//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() {
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进