CustomToast.ets 1.24 KB
@CustomDialog
export struct CustomToast {
  public static LENGTH_LONG = 4000;
  public static LENGTH_SHORT = 2000;

  @State msg: ResourceStr = ""
  @State duration: number = CustomToast.LENGTH_SHORT
  @State bgBorderRadius: number = 10
  opacityValue: number = 0.7
  bgColor: ResourceColor = $r("app.color.black")
  fontSizeValue :number | string | Resource = 27
  lineHeightValue :number | string | Resource = 38
  controller: CustomDialogController
  marginTop :number | string | Resource = 0

  dismiss: () => void = () => {
  }

  aboutToAppear(): void {
    let intervalID = setInterval(() => {
      if (this.controller != undefined) {
        this.controller.close()
        this.dismiss()
        clearInterval(intervalID);
      }
    }, this.duration);
  }

  build() {
    Row() {
        Text(this.msg)
          .fontWeight(FontWeight.Regular)
          .fontColor($r('app.color.white'))
          .fontSize(this.fontSizeValue)
          .lineHeight(this.lineHeightValue)
          .textAlign(TextAlign.Center)
    }.borderRadius(`${this.bgBorderRadius}`)
    .constraintSize({maxWidth:"86%"})
    .padding({top:23,bottom:23,left:35,right:35})
    .backgroundColor(this.bgColor)
    .opacity(this.opacityValue)
    .margin({top:this.marginTop})
  }
}