I can’t wrap my head about this one issue. Why wou...
# compose
m
I can’t wrap my head about this one issue. Why would this code do not run the layout inner lambda ?
👀 2
Copy code
fun Modifier.visibility(visible: Boolean) = this.composed {
    layout { measurable, constraints ->
        val placeable = measurable.measure(constraints)
        layout(placeable.width, placeable.height) {
            if (visible) placeable.place(0, 0)
        }
    }
}
somehow, it doesn’t recompose
z
Why are you using
composed
there, it looks unnecessary
m
True. I think that i used it because layout is not a composavlble function and that was my hope for it not to recompose
z
I’m not sure what you mean. I’m also not sure what the issue is exactly. Are you passing a different value in for
visible
over time and not seeing the layout modifier capture the new value?
m
Yes. When the value changes, the layout modifier does not run but it is measured
nvm. I asked chatGPT to fix that modifier, and it gave me this
Copy code
fun Modifier.visibility(visible: Boolean) = this.layout { measurable, constraints ->
    val placeable = measurable.measure(constraints)
    if (visible) {
        layout(placeable.width, placeable.height) {
            placeable.place(0, 0)
        }
    } else {
        layout(0, 0) {}
    }
}
no idea where he got that from. It seems do work
why would this work over my code OMG
z
i’m guessing it’s because the measure result is the same so it thinks it can skip placement
l
Why not this:
Copy code
fun Modifier.measureWithoutPlacing(isVisible: Boolean): Modifier =
    if (isVisible.not()) {
        layout { measurable, constraints ->
            val placeable = measurable.measure(constraints)
            layout(placeable.measuredWidth, placeable.measuredHeight) {}
        }
    } else {
        this
    }