Is it possible to change the height of a composabl...
# compose
l
Is it possible to change the height of a composable with out causing a recomposition? I do not want to scale the height, I want to be able to feed it a
Dp
height.
r
Yep, do it on
Layout
phase Use the
Modifier.layout{ }
and defer the read inside that lambda, then convert
dp
to
px
and voila, skipping the recomposition when animating the height or any other frequently-changing value for the layout phase! This is what I did for the
width
in the past:
Copy code
fun Modifier.width(widthProvider: () -> Dp): Modifier {
    return this.layout { measurable, constraints ->
        val width = widthProvider().toPx().toInt()
        val placeable = measurable.measure(constraints.copy(minWidth = width, maxWidth = width))
        layout(placeable.width, placeable.height) {
            placeable.placeRelative(0, 0)
        }
    }
}
l
Quick on the draw, Roberto. You the man
r
Amazing! I wasn't able to find a cleaner solution, hopefully there's one, if not at least that's what we got for now 😄
l
That is pretty damned clean, my friend
If you wanted you may be able to abstract it all away behind a custom layout which was I was going to do.