androidx.compose.ui.layout

Layout

Composable function · from org.jetbrains.compose.ui:ui:1.12.2

Layout lets you measure and place children with your own rules. Row, Column and Box are built on it.

@Composable
fun Stagger(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    Layout(content, modifier) { measurables, constraints ->
        // 1. Measure each child.
        val placeables = measurables.map { it.measure(constraints) }
        // 2. Decide your own size.
        val width = placeables.maxOf { it.width }
        val height = placeables.sumOf { it.height }
        // 3. Place the children.
        layout(width, height) {
            var y = 0
            placeables.forEachIndexed { i, p ->
                p.place(x = if (i % 2 == 0) 0 else width - p.width, y = y)
                y += p.height
            }
        }
    }
}

The rules:

Try Row, Column, Box, FlowRow or Modifier.layout { } first. You need Layout only for arrangements none of them can do.

With content

Measures and places children.

@Composable
fun Layout(
    content: @Composable @UiComposable () -> Unit,
    modifier: Modifier = Modifier,
    measurePolicy: MeasurePolicy,
)

content: @Composable @UiComposable () -> UnitRequired

The children to measure and place.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

measurePolicy: MeasurePolicyRequired

Your measuring and placing code. A lambda with measurables and constraints that ends with layout(width, height) { place... }.

Without content

A leaf that only takes up space.

@Composable
fun Layout(
    modifier: Modifier = Modifier,
    measurePolicy: MeasurePolicy,
)

Same as above: modifier, measurePolicy.

With several content slots

Each slot is a separate list of children.

@Composable
fun Layout(
    contents: List<@Composable @UiComposable () -> Unit>,
    modifier: Modifier = Modifier,
    measurePolicy: MultiContentMeasurePolicy,
)

contents: List<@Composable @UiComposable () -> Unit>Required

Several lists of children. The measure policy gets one list of measurables per slot.

measurePolicy: MultiContentMeasurePolicyRequired

Your measuring and placing code. A lambda with measurables and constraints that ends with layout(width, height) { place... }.

Same as above: modifier.