SubcomposeLayout
Common
Component in Compose Ui
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.
Last updated:
Installation
dependencies {
implementation("androidx.compose.ui:ui:1.8.0-alpha04")
}
Overloads
@Composable
fun SubcomposeLayout(
modifier: Modifier = Modifier,
measurePolicy: SubcomposeMeasureScope.(Constraints) -> MeasureResult
)
Parameters
name | description |
---|---|
modifier | [Modifier] to apply for the layout. |
measurePolicy | Measure policy which provides ability to subcompose during the measuring. |
@Composable
@UiComposable
fun SubcomposeLayout(
state: SubcomposeLayoutState,
modifier: Modifier = Modifier,
measurePolicy: SubcomposeMeasureScope.(Constraints) -> MeasureResult
)
Parameters
name | description |
---|---|
state | the state object to be used by the layout. |
modifier | [Modifier] to apply for the layout. |
measurePolicy | Measure policy which provides ability to subcompose during the measuring. |
Code Example
SubcomposeLayoutSample
@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) }
}
}
}