https://kotlinlang.org logo
#compose
Title
# compose
v

Vsevolod Ganin

01/14/2021, 5:50 PM
Is it ok that
onSizeChanged
reports size without padding?
Copy code
@Preview
@Composable
fun Test() {
    val density = AmbientDensity.current
    Box(
        modifier = Modifier
            .padding(with(density) { 50.toDp() })
            .size(with(density) { 50.toDp() })
            .background(Color.Red)
            .onSizeChanged { println(it) }
    )
}
I expect this to print
150 x 150
, but get
50 x 50
instead.
l

Lukas Sztefek

01/14/2021, 5:53 PM
What about this?
Copy code
modifier = Modifier
            .onSizeChanged { println(it) }
            .padding(with(density) { 50.toDp() })
            .size(with(density) { 50.toDp() })
            .background(Color.Red)
v

Vsevolod Ganin

01/14/2021, 5:55 PM
Tried that, still same output
Replacing
onSizeChanged { println(it) }
with
Copy code
layout { measurable, constraints ->
                val placeable = measurable.measure(constraints)
                println("${placeable.width} x ${placeable.height}")
                layout(placeable.width, placeable.height) {
                    placeable.place(0, 0)
                }
            }
before
padding
and
size
did the trick. Seems like a bug in
onSizeChanged
, I’ll file
4 Views