androidx.compose.foundation.layout
FlexBoxSince 1.11Experimental
Composable function · from org.jetbrains.compose.foundation:foundation-layout:1.12.2
FlexBox lays out children in one direction and wraps them onto new lines, like FlowRow. In addition, each child can grow to fill free space or shrink when space is short, like CSS Flexbox. It can do everything Row, Column and the flow layouts do.
It is new and experimental: add @OptIn(ExperimentalFlexBoxApi::class). Its API may still change.
FlexBox(
config = FlexBoxConfig {
direction(FlexDirection.Row)
wrap(FlexWrap.Wrap)
gap(8.dp)
},
) {
Text("Grows", Modifier.flex { grow(1f) })
Text("Fixed")
Text("Shrinks first", Modifier.flex { shrink(2f) })
}
- The container settings go in
config:direction,wrap,justifyContent(along the line),alignItems(across it), andgap. - Each child sets its own behavior with
Modifier.flex { }:grow,shrink,basis(its starting size),alignSelfandorder.
If a Row with Modifier.weight or a FlowRow does what you need, use those. They are stable.
Read more
- FlexBox: get started (Android Developers)
- Layout containers (Android Developers): how FlexBox compares to the other layouts
- Meet FlexBox: the powerful new layout system for Compose (ProAndroidDev blog)
ParametersExperimental
@Composable
fun FlexBox(
modifier: Modifier = Modifier,
config: FlexBoxConfig = FlexBoxConfig,
content: @Composable FlexBoxScope.() -> Unit,
)
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position. See the Modifier guide.
config: FlexBoxConfigOptional
config: FlexBoxConfigOptionalDefault: FlexBoxConfig
Direction, wrapping, alignment and gaps of the container. Create it with FlexBoxConfig { ... }.
content: @Composable FlexBoxScope.() -> UnitRequired
content: @Composable FlexBoxScope.() -> UnitRequiredThe children. Each can use Modifier.flex { } to grow, shrink or align itself.