New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Compose Foundation Layout

BoxWithConstraints

Common

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.7.0-beta04")
}

Overloads

@UiComposable
@Composable
fun BoxWithConstraints(
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.TopStart,
    propagateMinConstraints: Boolean = false,
    content:
        @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
)

Parameters

namedescription
modifierModifier to be applied to the layout.
contentAlignmentThe default alignment inside the [BoxWithConstraints].
propagateMinConstraintsWhether the incoming min constraints should be passed to content.
contentThe content of the [BoxWithConstraints].

Code Example

BoxWithConstraintsSample

@Composable
@Sampled
fun BoxWithConstraintsSample() {
    BoxWithConstraints {
        val rectangleHeight = 100.dp
        if (maxHeight < rectangleHeight * 2) {
            Box(Modifier.size(50.dp, rectangleHeight).background(Color.Blue))
        } else {
            Column {
                Box(Modifier.size(50.dp, rectangleHeight).background(Color.Blue))
                Box(Modifier.size(50.dp, rectangleHeight).background(Color.Gray))
            }
        }
    }
}