Indu
04/08/2021, 3:19 PMIndu
04/08/2021, 3:21 PMIndu
04/08/2021, 3:23 PMIndu
04/08/2021, 3:24 PM@Composable
fun LayoutContainer(modifier: Modifier=Modifier, content: @Composable () -> Unit){
Layout(
modifier = modifier,
content = content
) { measurables, constraints ->
var rowWidths = mutableListOf<Int>()
var rowWidth = 0
var maxRowElements = 0
var layoutHeight = 0
var layoutWidth = 0
var index = 0
val placeables = measurables.map { measurable ->
val placeable = measurable.measure(constraints)
if((rowWidth + placeable.width) < constraints.maxWidth){
rowWidth += placeable.width
}else{
rowWidths.add(rowWidth)
++index
rowWidth = 0
rowWidth += placeable.width
}
placeable
}
rowWidths.add(rowWidth)
++index
layoutHeight = placeables[0].height * index
layoutWidth = rowWidths.maxOf { it }
maxRowElements = layoutWidth/placeables[0].width
var xPosition = 0
var yPosition = 0
var rowElements = 0
layout(layoutWidth,layoutHeight){
placeables.forEach {
if(rowElements >= maxRowElements){
xPosition = 0
yPosition += it.height
rowElements = 0
}
it.placeRelative(xPosition,yPosition)
xPosition = xPosition+it.width
++rowElements
}
}
}
}
Albert Chang
04/09/2021, 12:00 AMLayout
whenever you can. If you can implement the design using Layout
, then definitely no need to use BoxWithConstraints
.Indu
04/09/2021, 1:42 AM