xref: /aosp_15_r20/external/accompanist/docs/flowlayout.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1*fa44fe6aSInna Palant# Jetpack Compose Flow Layouts
2*fa44fe6aSInna Palant
3*fa44fe6aSInna Palant[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-flowlayout)](https://search.maven.org/search?q=g:com.google.accompanist)
4*fa44fe6aSInna Palant
5*fa44fe6aSInna Palant!!! warning
6*fa44fe6aSInna Palant    **This library is deprecated, with official FlowLayout support in androidx.compose.foundation.** The migration guide and original documentation is below.
7*fa44fe6aSInna Palant
8*fa44fe6aSInna Palant## Migration Guide to the official FlowLayouts
9*fa44fe6aSInna Palant
10*fa44fe6aSInna PalantThe official `androidx.compose.foundation` FlowLayouts support is very similar to accompanist/flowlayouts, with a few changes.
11*fa44fe6aSInna Palant
12*fa44fe6aSInna Palant1. Replace import packages to point to Androidx.Compose
13*fa44fe6aSInna Palant``` kotlin
14*fa44fe6aSInna Palantimport androidx.compose.foundation.layout.FlowColumn
15*fa44fe6aSInna Palant```
16*fa44fe6aSInna Palant
17*fa44fe6aSInna Palant``` kotlin
18*fa44fe6aSInna Palantimport androidx.compose.foundation.layout.FlowRow
19*fa44fe6aSInna Palant```
20*fa44fe6aSInna Palant
21*fa44fe6aSInna PalantFor `FlowColumn`:
22*fa44fe6aSInna Palant
23*fa44fe6aSInna Palant2. Replace Modifier `mainAxisAlignment` with `verticalArrangement`
24*fa44fe6aSInna Palant
25*fa44fe6aSInna Palant3. Replace Modifier `crossAxisAlignment` with `horizontalArrangement`
26*fa44fe6aSInna Palant
27*fa44fe6aSInna Palant
28*fa44fe6aSInna PalantFor `FlowRow`
29*fa44fe6aSInna Palant
30*fa44fe6aSInna Palant4. `mainAxisAlignment` is now `horizontalArrangement`
31*fa44fe6aSInna Palant
32*fa44fe6aSInna Palant5. `crossAxisAlignment` is now `verticalArrangement`
33*fa44fe6aSInna Palant
34*fa44fe6aSInna Palant``` kotlin
35*fa44fe6aSInna PalantFlowColumn(
36*fa44fe6aSInna Palant    modifier = Modifier,
37*fa44fe6aSInna Palant    verticalArrangement = Arrangement.Top,
38*fa44fe6aSInna Palant    horizontalArrangement = Arrangement.Start,
39*fa44fe6aSInna Palant    content = { // columns }
40*fa44fe6aSInna Palant)
41*fa44fe6aSInna Palant```
42*fa44fe6aSInna Palant
43*fa44fe6aSInna Palant``` kotlin
44*fa44fe6aSInna PalantFlowRow(
45*fa44fe6aSInna Palant    modifier = Modifier,
46*fa44fe6aSInna Palant    horizontalArrangement = Arrangement.Start,
47*fa44fe6aSInna Palant    verticalArrangement = Arrangement.Top,
48*fa44fe6aSInna Palant    content = { // rows }
49*fa44fe6aSInna Palant)
50*fa44fe6aSInna Palant```
51*fa44fe6aSInna Palant
52*fa44fe6aSInna Palant6. Replace `mainAxisSpacing` with `HorizontalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowRow` and `VerticalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowColumn`.
53*fa44fe6aSInna Palant
54*fa44fe6aSInna Palant7. Replace `crossAxisSpacing` with `VerticalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowRow` and `HorizontalArrangement.spacedBy(50.dp)` in `FlowColumn`.
55*fa44fe6aSInna Palant
56*fa44fe6aSInna PalantHere `Alignment.*` is the Alignment you wish to use such as `Alignment.Start`, `Alignment.Top` etc
57*fa44fe6aSInna Palant
58*fa44fe6aSInna Palant
59*fa44fe6aSInna Palant``` kotlin
60*fa44fe6aSInna PalantFlowRow(
61*fa44fe6aSInna Palant    modifier = Modifier,
62*fa44fe6aSInna Palant    horizontalArrangement = Arrangement.spacedBy(50.dp, Alignment.Start),
63*fa44fe6aSInna Palant    verticalArrangement = Arrangement.spacedBy(50.dp, Alignment.Top),
64*fa44fe6aSInna Palant    content = { // rows }
65*fa44fe6aSInna Palant)
66*fa44fe6aSInna Palant```
67*fa44fe6aSInna Palant
68*fa44fe6aSInna Palant``` kotlin
69*fa44fe6aSInna PalantFlowColumn(
70*fa44fe6aSInna Palant    modifier = Modifier,
71*fa44fe6aSInna Palant    verticalArrangement = Arrangement.spacedBy(50.dp, Alignment.Top),
72*fa44fe6aSInna Palant    horizontalArrangement = Arrangement.spacedBy(50.dp, Alignment.Start),
73*fa44fe6aSInna Palant    content = { // columns }
74*fa44fe6aSInna Palant)
75*fa44fe6aSInna Palant```
76*fa44fe6aSInna Palant
77*fa44fe6aSInna Palant8. `lastLineMainAxisAlignment` is currently not supported in Compose Flow Layouts.
78*fa44fe6aSInna Palant
79*fa44fe6aSInna Palant### New Features:
80*fa44fe6aSInna Palant#### Add weights to each child
81*fa44fe6aSInna PalantTo scale an item based on the size of its parent and the space available, adding weights is helpful.
82*fa44fe6aSInna PalantAdding a weight in `FlowRow` and `FlowColumn` is different than in `Row` and `Column`.
83*fa44fe6aSInna Palant
84*fa44fe6aSInna PalantIn `FlowLayout`, it is based on the number of items placed on the row it falls on and their weights.
85*fa44fe6aSInna PalantFirst we check to see if an item can fit in the current row or column based on its intrinsic size.
86*fa44fe6aSInna PalantIf it fits and has a weight, its final size is expanded based on the available space and the number of items
87*fa44fe6aSInna Palantwith weights placed on the row or column it falls on.
88*fa44fe6aSInna Palant
89*fa44fe6aSInna PalantBecause of the nature of `FlowLayouts` an item only expands and does not shrink in size. Its width in `FlowRow`
90*fa44fe6aSInna Palantor height in `FlowColumn` determines it minimum width or height, and then expands based on its weight and
91*fa44fe6aSInna Palantthe available space remaining after row items' width/height have been determined.
92*fa44fe6aSInna Palant
93*fa44fe6aSInna Palant``` kotlin
94*fa44fe6aSInna PalantFlowRow() {
95*fa44fe6aSInna Palant     repeat(20) { Box(Modifier.size(20.dp).weight(1f, true) }
96*fa44fe6aSInna Palant}
97*fa44fe6aSInna Palant```
98*fa44fe6aSInna Palant
99*fa44fe6aSInna Palant#### Create a maximum number of items in row or column
100*fa44fe6aSInna PalantYou may choose to limit the number of items that appear in each row in `FlowRow` or column in `FlowColumn`
101*fa44fe6aSInna PalantThis can be configured using `maxItemsInEachRow` or `maxItemsInEachColumn`:
102*fa44fe6aSInna Palant``` kotlin
103*fa44fe6aSInna PalantFlowRow(maxItemsInEachRow = 3) {
104*fa44fe6aSInna Palant     repeat(10) { Box(Modifier.size(20.dp).weight(1f, true) }
105*fa44fe6aSInna Palant}
106*fa44fe6aSInna Palant```
107*fa44fe6aSInna Palant
108*fa44fe6aSInna Palant## Examples
109*fa44fe6aSInna Palant
110*fa44fe6aSInna PalantFor examples, refer to the [Flow Row samples](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/compose/foundation/foundation-layout/samples/src/main/java/androidx/compose/foundation/layout/samples/FlowRowSample.kt)
111*fa44fe6aSInna Palantand the [Flow Column samples](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/compose/foundation/foundation-layout/samples/src/main/java/androidx/compose/foundation/layout/samples/FlowColumnSample.kt).
112*fa44fe6aSInna Palant
113*fa44fe6aSInna Palant## Original Docs
114*fa44fe6aSInna Palant
115*fa44fe6aSInna PalantIt is most similar to `Row` and `Column` and shares similar modifiers and the scopes.
116*fa44fe6aSInna PalantUnlike the standard `Row` and `Column` composables, if it runs out of space on the current row,
117*fa44fe6aSInna Palantthe children are placed in the next line, and this repeats until the children are fully placed.
118*fa44fe6aSInna Palant
119*fa44fe6aSInna Palant## Usage
120*fa44fe6aSInna Palant
121*fa44fe6aSInna Palant``` kotlin
122*fa44fe6aSInna PalantFlowRow {
123*fa44fe6aSInna Palant    // row contents
124*fa44fe6aSInna Palant}
125*fa44fe6aSInna Palant
126*fa44fe6aSInna PalantFlowColumn {
127*fa44fe6aSInna Palant    // column contents
128*fa44fe6aSInna Palant}
129*fa44fe6aSInna Palant```
130*fa44fe6aSInna Palant
131*fa44fe6aSInna Palant## Download
132*fa44fe6aSInna Palant
133*fa44fe6aSInna Palant[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-flowlayout)](https://search.maven.org/search?q=g:com.google.accompanist)
134*fa44fe6aSInna Palant
135*fa44fe6aSInna Palant```groovy
136*fa44fe6aSInna Palantrepositories {
137*fa44fe6aSInna Palant    mavenCentral()
138*fa44fe6aSInna Palant}
139*fa44fe6aSInna Palant
140*fa44fe6aSInna Palantdependencies {
141*fa44fe6aSInna Palant    implementation "com.google.accompanist:accompanist-flowlayout:<version>"
142*fa44fe6aSInna Palant}
143*fa44fe6aSInna Palant```
144*fa44fe6aSInna Palant
145*fa44fe6aSInna PalantSnapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. These are updated on every commit.
146*fa44fe6aSInna Palant
147*fa44fe6aSInna Palant[compose]: https://developer.android.com/jetpack/compose
148*fa44fe6aSInna Palant[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/accompanist/accompanist-flowlayout/
149