CustomToast.ets 1.19 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 = "27lpx"
  lineHeightValue :number | string | Resource = "38lpx"
  controller: CustomDialogController

  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}lpx`)
    .constraintSize({maxWidth:"86%"})
    .padding({top:"23lpx",bottom:'23lpx',left:"35lpx",right:"35lpx"})
    .backgroundColor(this.bgColor)
    .opacity(this.opacityValue)
  }
}