Travis Griggs
10/05/2023, 5:26 PM@Composable
private fun ManyBoxes(rects: List<Rect>, modifier: Modifier = Modifier) {
Box(modifier = modifier) {
rects.forEach { rect ->
Box(modifier = Modifier
.layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
layout(rect.width.roundToInt(), rect.height.roundToInt()) {
placeable.place(rect.left.roundToInt(), rect.top.roundToInt())
}
}
.background(Color._Random))
}
}
}
@Preview(showBackground = true)
@Composable
private fun Preview() {
ManyBoxes(rects = listOf(Rect(100f, 200f, 300f, 500f)), modifier = Modifier.size(800.dp))
}
romainguy
10/05/2023, 5:35 PMBox
for each Rect
but the .layout{}
modifier you use would layout children of the Box
, which you don’t haveTravis Griggs
10/05/2023, 5:46 PMBox
Text(Modifier.layout { ... })
Slider
and I use the layout modifier on the text to adjust where it goes relative to the slider's value. That isn't modifying it's children. I don't think the Text has children, does it? Is this a case where it's working, but not for the reasons I think it is working?romainguy
10/05/2023, 5:56 PMTravis Griggs
10/05/2023, 6:10 PMromainguy
10/05/2023, 6:13 PMTravis Griggs
10/05/2023, 6:15 PMAlbert Chang
10/06/2023, 4:10 AMlayout(width, height) {}
is the size of the layout itself, instead of the size of the placeable
. The placeable
is measured with a min size of zero (specified by constraints
), and since it's empty, its size will be zero. If you want to force a size from the parent, measure it with measurable.measure(Constraints.fixed(rect.width.roundToInt(), rect.height.roundToInt()))
.Canvas
will be better for this case.romainguy
10/06/2023, 4:41 AM