目录

如何避免Badge在数量显示切换时的Image闪烁问题

通过@State装饰器修饰变量,动态设置badgeSize以控制Badge状态,当值设为0时Badge自动隐藏。

@Entry
@Component
struct BadgeDemo {
  @State message: string = 'Hello World';
  @State badgeSize: number = 15;

  build() {
    Row() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          // change the Badge size
          this.badgeSize = this.badgeSize === 0 ? 15 : 0;
        })
      Badge({
        value: '1',
        position: {
          x: 40,
          y: 0
        },
        style: {
          badgeSize: this.badgeSize,
          badgeColor: Color.Red
        }
      }) {
        Image($r('app.media.startIcon'))
          .width(50)
          .height(50)
      }
    }
    .height('100%')
  }
}