androidx.compose.foundation.layout
Box
Composable function · from org.jetbrains.compose.foundation:foundation-layout:1.12.2
A Box stacks its children on top of each other. The first child is at the bottom, the last one on top.
Use it to put a badge on an image, text over a picture, or a button in a corner of an area.
Box(Modifier.size(120.dp)) {
Image(painter, contentDescription = null, Modifier.fillMaxSize())
Text("New", Modifier.align(Alignment.TopEnd).padding(8.dp))
}
A Box is as big as its biggest child, unless you give it a size.
Inside the content, children get extra modifiers from BoxScope:
Modifier.align(Alignment.BottomEnd)places one child in a corner or edge.Modifier.matchParentSize()makes a child as big as the box, without making the box bigger. Good for a background layer.
An empty Box(Modifier.size(48.dp).background(color)) is a simple colored area.
With content
Children are stacked, the last one on top.
@Composable
fun Box(
modifier: Modifier = Modifier,
contentAlignment: Alignment = Alignment.TopStart,
propagateMinConstraints: Boolean = false,
content: @Composable BoxScope.() -> Unit,
)
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position. See the Modifier guide.
contentAlignment: AlignmentOptional
contentAlignment: AlignmentOptionalDefault: Alignment.TopStart
Where children go when they are smaller than the box. Default: top start. Use Alignment.Center to center them all.
propagateMinConstraints: BooleanOptional
propagateMinConstraints: BooleanOptionalDefault: false
Set to true to make children at least as big as the minimum size of the box. Rarely needed.
content: @Composable BoxScope.() -> UnitRequired
content: @Composable BoxScope.() -> UnitRequiredThe children, from bottom to top.
Empty
A box with no children, for a colored area or a placeholder.
@Composable
fun Box(modifier: Modifier)
Same as above: modifier.