很想获取可组合组件的 MeasuredWitdh 和 MeasuredHeight?

Hot to get MeasuredWitdh and MeasuredHeight of a Composable component?

假设我有以下可组合项:

Column(Modifier.fillMaxWidth()) {
        
    Text()
    Text()
    Text()

}

我想知道如何捕获 Column measuredWidthmeasuredHeight 的值。 使用 Android 视图框架样式,我可以利用视图中的方法 onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

您可以使用 DrawCacheModifier 找到可组合项的大小:

Column(
    Modifier
    .fillMaxWidth()
    .then(object : DrawCacheModifier {
        override fun onBuildCache(size: Size, density: Density) {
          println("Size is $size")
       }
        override fun ContentDrawScope.draw() {
          drawContent()
    }

 })
)
var width = 0
var hegiht = 0
Column(modifier = Modifier.onGloballyPositioned { layoutCoordinates ->
    width = layoutCoordinates.size.width
    hegiht = layoutCoordinates.size.height
}) {
    // Column body
}