FlexBox
@Composable
public inline fun FlexBox(
modifier: Modifier = Modifier,
config: FlexBoxConfig = FlexBoxConfig,
content: @Composable FlexBoxScope.() -> Unit,
)
A layout that aligns its children in a single direction (the main axis) and allows them to wrap onto multiple lines. FlexBox provides a highly configurable layout system, serving as a flexible superset of Row, Column, FlowRow, and FlowColumn.
The layout behavior of the container is controlled by the config parameter, which dictates the flex direction, wrapping behavior, alignment, and spacing. Individual children can further control their own flexibility (grow, shrink, and base size) and alignment using the FlexBoxScope.flex modifier.
Understanding FlexBox requires familiarity with its axes:
- Main Axis: The primary direction along which items are laid out, determined by the
- For
FlexDirection.Row:main-startis the layout's start edge (left in LTR, right in - For
FlexDirection.RowReverse:main-startis the layout's end edge (right in LTR, - RTL) and
main-endis the start edge (left in LTR, right in RTL). - For
FlexDirection.Column:main-startis the top edge andmain-endis the bottom edge. - For
FlexDirection.ColumnReverse:main-startis the bottom edge andmain-endis - Cross Axis: The axis perpendicular to the main axis. Wrapped lines are added, and items are
- For horizontal directions (
FlexDirection.RowandFlexDirection.RowReverse): - For vertical directions (
FlexDirection.ColumnandFlexDirection.ColumnReverse):
FlexBoxConfigScope.direction. Items are placed starting from the main-start edge and flowing toward the main-end edge. Defaults to FlexDirection.Row.
RTL) and main-end is the end edge (right in LTR, left in RTL).
left in
the top edge.
aligned within their lines, starting from the cross-start edge and flowing toward the cross-end edge.
cross-start is the top edge and cross-end is the bottom edge.
cross-start is the layout's start edge and cross-end is the end edge.
Children can dictate how they share available space using the FlexBoxScope.flex modifier:
FlexConfigScope.grow: Defines how much of the remaining positive free space the item shouldFlexConfigScope.shrink: Defines how much the item should shrink when the combined sizes ofFlexConfigScope.basis: Sets the initial main axis size of the item before any free space
consume relative to its siblings. Defaults to 0f (no growth).
the items exceed the container's main axis size. Defaults to 1f.
distribution (grow or shrink) is calculated. Defaults to FlexBasis.Auto.
FlexBox provides granular control over the placement of items and lines:
FlexBoxConfigScope.wrap: Controls whether items are forced onto a single line or allowed toFlexBoxConfigScope.maxItemsInEachLine: Limits how many items can be placed in each line whenFlexBoxConfigScope.justifyContent: Distributes items along the main axis (for example,FlexBoxConfigScope.alignItems: Aligns items within a specific line along the cross axis (forFlexConfigScope.alignSelf: Allows an individual item to override the container'sFlexBoxConfigScope.alignContent:Distributes multiple wrapped lines along the cross axis. This
wrap onto multiple lines when they exceed the available space. Defaults to FlexWrap.NoWrap.
wrapping is enabled. Defaults to no limit.
spacing them evenly). Defaults to FlexJustifyContent.Start.
example, centering them vertically within a Row). Defaults to FlexAlignItems.Start.
FlexBoxConfigScope.alignItems. Defaults to FlexAlignSelf.Auto.
only applies when wrapping is enabled. Defaults to FlexAlignContent.Start.
By default, children are placed in a horizontal row without wrapping. If wrapping is disabled (FlexWrap.NoWrap), children will shrink to fit the container if they have a shrink factor > 0. If children cannot shrink enough due to their minimum intrinsic sizes, they will visually overflow the container's bounds along the main axis. You can explicitly apply Modifier.clipToBounds on the FlexBox if you wish to hide overflowing content.
Parameters
| modifier | The modifier to be applied to the FlexBox container. |
| config | A FlexBoxConfig that configures the container's layout properties. Defaults to a horizontal row layout without wrapping, with items aligned to the start on both axes and no gaps between items. |
| content | The content of the FlexBox, defined within a FlexBoxScope. |