HarmonyOS onAreaChange回调方法问题?

Flex添加onAreaChange区域变化回调监听,监听高度变化,如果设置Flex的.height为newValue,那么onAreaChange回调方法中的newValue就无变化了,一直是0,这个怎么处理?

代码如下:目前看问题现象就是:组件设置onAreaChange,同时设置.height的高度为变化的高度,那么回调中的返回值就不对了。

@State historyHeight:Length = 0
Flex(
  {
    direction: FlexDirection.Row,
    wrap: FlexWrap.Wrap
  }
)
  .height(this.historyHeight)
  .onAreaChange((oldValue:Area, newValue:Area) => {
    this.historyHeight = newValue.height
  })
阅读 440
1 个回答

因为onAreaChange回调之后,高度的值没有变化了,所以不会走onAreaChange回调了,如果设置的值一直增加的话是可以的。

demo如下:

// xxx.ets
@Entry
@Component
struct AreaExample {
  @State value: string = 'Text'
  @State sizeValue: string = ''
  @State historyHeight:Length = 10

  build() {
    Column() {
      Text(this.value)
        .backgroundColor(Color.Green)
        .fontSize(20)
        .width(this.historyHeight)
        .onClick(() => {
          this.historyHeight=this.historyHeight as number + 1
        })
        .onAreaChange((oldValue: Area, newValue: Area) => {
          console.info(`tag Ace: on area change, newValue.width:`+newValue.width)
          this.sizeValue = JSON.stringify(newValue)
          this.historyHeight = newValue.width
        })
      Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 })
    }
    .width('100%').height('100%').margin({ top: 30 })
  }
}