{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

BoxWithConstraintsKt

public final class BoxWithConstraintsKt


Summary

Public methods

static final void
@Composable
@UiComposable
BoxWithConstraints(
    @NonNull Modifier modifier,
    @NonNull Alignment contentAlignment,
    boolean propagateMinConstraints,
    @Composable @UiComposable @ExtensionFunctionType @NonNull Function1<@NonNull BoxWithConstraintsScopeUnit> content
)

A composable that defines its own content according to the available space, based on the incoming constraints or the current LayoutDirection.

Public methods

BoxWithConstraints

@Composable
@UiComposable
public static final void BoxWithConstraints(
    @NonNull Modifier modifier,
    @NonNull Alignment contentAlignment,
    boolean propagateMinConstraints,
    @Composable @UiComposable @ExtensionFunctionType @NonNull Function1<@NonNull BoxWithConstraintsScopeUnit> content
)

A composable that defines its own content according to the available space, based on the incoming constraints or the current LayoutDirection. Example usage:

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

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))
        }
    }
}

The composable will compose the given children, and will position the resulting layout elements in a parent layout which behaves similar to a Box. The layout will size itself to fit the content, subject to the incoming constraints. When children are smaller than the parent, by default they will be positioned inside the layout according to the contentAlignment. For individually specifying the alignments of the children layouts, use the BoxScope.align modifier. By default, the content will be measured without the Box's incoming min constraints, unless propagateMinConstraints is true. As an example, setting propagateMinConstraints to true can be useful when the BoxWithConstraints has content on which modifiers cannot be specified directly and setting a min size on the content of the BoxWithConstraints is needed. If propagateMinConstraints is set to true, the min size set on the BoxWithConstraints will also be applied to the content, whereas otherwise the min size will only apply to the BoxWithConstraints. When the content has more than one layout child the layout children will be stacked one on top of the other (positioned as explained above) in the composition order.

Parameters
@NonNull Modifier modifier

Modifier to be applied to the layout.

@NonNull Alignment contentAlignment

The default alignment inside the BoxWithConstraints.

boolean propagateMinConstraints

Whether the incoming min constraints should be passed to content.

@Composable @UiComposable @ExtensionFunctionType @NonNull Function1<@NonNull BoxWithConstraintsScopeUnit> content

The content of the BoxWithConstraints.