Composable Function

SubcomposeLayout

Analogue of Layout which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.

SubcomposeLayoutSample

@Sampled
@Composable
fun SubcomposeLayoutSample(
    mainContent: @Composable () -> Unit,
    dependentContent: @Composable (IntSize) -> Unit,
) {
    // enum class SlotsEnum { Main, Dependent }
    SubcomposeLayout { constraints ->
        val mainPlaceables = subcompose(SlotsEnum.Main, mainContent).map { it.measure(constraints) }
        val maxSize =
            mainPlaceables.fold(IntSize.Zero) { currentMax, placeable ->
                IntSize(
                    width = maxOf(currentMax.width, placeable.width),
                    height = maxOf(currentMax.height, placeable.height),
                )
            }
        layout(maxSize.width, maxSize.height) {
            mainPlaceables.forEach { it.placeRelative(0, 0) }
            subcompose(SlotsEnum.Dependent) { dependentContent(maxSize) }
                .forEach { it.measure(constraints).placeRelative(0, 0) }
        }
    }
}

enum class SlotsEnum {
    Main,
    Dependent,
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: