xref: /aosp_15_r20/external/accompanist/docs/flowlayout.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1# Jetpack Compose Flow Layouts
2
3[![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
5!!! warning
6    **This library is deprecated, with official FlowLayout support in androidx.compose.foundation.** The migration guide and original documentation is below.
7
8## Migration Guide to the official FlowLayouts
9
10The official `androidx.compose.foundation` FlowLayouts support is very similar to accompanist/flowlayouts, with a few changes.
11
121. Replace import packages to point to Androidx.Compose
13``` kotlin
14import androidx.compose.foundation.layout.FlowColumn
15```
16
17``` kotlin
18import androidx.compose.foundation.layout.FlowRow
19```
20
21For `FlowColumn`:
22
232. Replace Modifier `mainAxisAlignment` with `verticalArrangement`
24
253. Replace Modifier `crossAxisAlignment` with `horizontalArrangement`
26
27
28For `FlowRow`
29
304. `mainAxisAlignment` is now `horizontalArrangement`
31
325. `crossAxisAlignment` is now `verticalArrangement`
33
34``` kotlin
35FlowColumn(
36    modifier = Modifier,
37    verticalArrangement = Arrangement.Top,
38    horizontalArrangement = Arrangement.Start,
39    content = { // columns }
40)
41```
42
43``` kotlin
44FlowRow(
45    modifier = Modifier,
46    horizontalArrangement = Arrangement.Start,
47    verticalArrangement = Arrangement.Top,
48    content = { // rows }
49)
50```
51
526. Replace `mainAxisSpacing` with `HorizontalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowRow` and `VerticalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowColumn`.
53
547. Replace `crossAxisSpacing` with `VerticalArrangement.spacedBy(50.dp, Alignment.*)` in `FlowRow` and `HorizontalArrangement.spacedBy(50.dp)` in `FlowColumn`.
55
56Here `Alignment.*` is the Alignment you wish to use such as `Alignment.Start`, `Alignment.Top` etc
57
58
59``` kotlin
60FlowRow(
61    modifier = Modifier,
62    horizontalArrangement = Arrangement.spacedBy(50.dp, Alignment.Start),
63    verticalArrangement = Arrangement.spacedBy(50.dp, Alignment.Top),
64    content = { // rows }
65)
66```
67
68``` kotlin
69FlowColumn(
70    modifier = Modifier,
71    verticalArrangement = Arrangement.spacedBy(50.dp, Alignment.Top),
72    horizontalArrangement = Arrangement.spacedBy(50.dp, Alignment.Start),
73    content = { // columns }
74)
75```
76
778. `lastLineMainAxisAlignment` is currently not supported in Compose Flow Layouts.
78
79### New Features:
80#### Add weights to each child
81To scale an item based on the size of its parent and the space available, adding weights is helpful.
82Adding a weight in `FlowRow` and `FlowColumn` is different than in `Row` and `Column`.
83
84In `FlowLayout`, it is based on the number of items placed on the row it falls on and their weights.
85First we check to see if an item can fit in the current row or column based on its intrinsic size.
86If it fits and has a weight, its final size is expanded based on the available space and the number of items
87with weights placed on the row or column it falls on.
88
89Because of the nature of `FlowLayouts` an item only expands and does not shrink in size. Its width in `FlowRow`
90or height in `FlowColumn` determines it minimum width or height, and then expands based on its weight and
91the available space remaining after row items' width/height have been determined.
92
93``` kotlin
94FlowRow() {
95     repeat(20) { Box(Modifier.size(20.dp).weight(1f, true) }
96}
97```
98
99#### Create a maximum number of items in row or column
100You may choose to limit the number of items that appear in each row in `FlowRow` or column in `FlowColumn`
101This can be configured using `maxItemsInEachRow` or `maxItemsInEachColumn`:
102``` kotlin
103FlowRow(maxItemsInEachRow = 3) {
104     repeat(10) { Box(Modifier.size(20.dp).weight(1f, true) }
105}
106```
107
108## Examples
109
110For 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)
111and 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
113## Original Docs
114
115It is most similar to `Row` and `Column` and shares similar modifiers and the scopes.
116Unlike the standard `Row` and `Column` composables, if it runs out of space on the current row,
117the children are placed in the next line, and this repeats until the children are fully placed.
118
119## Usage
120
121``` kotlin
122FlowRow {
123    // row contents
124}
125
126FlowColumn {
127    // column contents
128}
129```
130
131## Download
132
133[![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
135```groovy
136repositories {
137    mavenCentral()
138}
139
140dependencies {
141    implementation "com.google.accompanist:accompanist-flowlayout:<version>"
142}
143```
144
145Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. These are updated on every commit.
146
147[compose]: https://developer.android.com/jetpack/compose
148[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/accompanist/accompanist-flowlayout/
149