1# WaterFlow 2 3 4The **\<WaterFlow>** component is a container that consists of cells formed by rows and columns and arranges items of different sizes from top to bottom according to the preset rules. 5 6 7> **NOTE** 8> 9> This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. 10 11 12## Child Components 13 14 15The [\<FlowItem>](ts-container-flowitem.md) child component is supported. 16 17> **NOTE** 18> 19> When the **visibility** attribute of a child component in **\<WaterFlow >** is set to **None**, the child component is not displayed, but still takes up cells. 20 21## APIs 22 23 24WaterFlow(options?: {footer?: CustomBuilder, scroller?: Scroller}) 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| ---------- | ----------------------------------------------- | ------ | -------------------------------------------- | 30| footer | [CustomBuilder](ts-types.md#custombuilder8) | No | Footer of the **\<WaterFlow>** component. | 31| scroller | [Scroller](ts-container-scroll.md#scroller) | No | Controller, which can be bound to scrollable components.<br>The **\<WaterFlow>** component supports only the **scrollToIndex** API of the **\<Scroller>** component.| 32 33 34## Attributes 35 36 37In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 38 39| Name| Type| Description| 40| -------- | -------- | -------- | 41| columnsTemplate | string | Number of columns in the layout. If this attribute is not set, one column is used by default.<br>For example, **'1fr 1fr 2fr'** indicates three columns, with the first column taking up 1/4 of the parent component's full width, the second column 1/4, and the third column 2/4. This attribute supports [auto-fill](#auto-fill).<br>Default value: **'1fr'**| 42| rowsTemplate | string | Number of rows in the layout. If this attribute is not set, one row is used by default.<br>For example, **'1fr 1fr 2fr'** indicates three rows, with the first row taking up 1/4 of the parent component's full height, the second row 1/4, and the third row 2/4. This attribute supports [auto-fill](#auto-fill).<br>Default value: **'1fr'**| 43| itemConstraintSize | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraints of the child components during layout. | 44| columnsGap | Length |Gap between columns.<br>Default value: **0**| 45| rowsGap | Length |Gap between rows.<br> Default value: **0**| 46| layoutDirection | [FlexDirection](ts-appendix-enums.md#flexdirection) |Main axis direction of the layout.<br>Default value: **FlexDirection.Column**| 47 48The priority of **layoutDirection** is higher than that of **rowsTemplate** and **columnsTemplate**. Depending on the **layoutDirection** settings, there are three layout modes: 49 50- **layoutDirection** is set to **FlexDirection.Column** or **FlexDirection.ColumnReverse** 51 52 In this case, **columnsTemplate** is valid. If it is not set, the default value is used. For example, if **columnsTemplate** is set to **"1fr 1fr"** and **rowsTemplate** **"1fr 1fr 1fr"**, child components are arranged in vertical layout, with the cross axis equally divided into two columns. 53 54- **layoutDirection** set to **FlexDirection.Row** or **FlexDirection.RowReverse** 55 56 In this case, **rowsTemplate** is valid. If it is not set, the default value is used. For example, if **columnsTemplate** is set to **"1fr 1fr"** and **rowsTemplate** **"1fr 1fr 1fr"**, child components are arranged in horizontal layout, with the cross axis equally divided into three columns. 57 58- **layoutDirection** is not set 59 60 In this case, the default value of **layoutDirection** is used, which is **FlexDirection.Column**, and **columnsTemplate** is valid. For example, if **columnsTemplate** is set to **"1fr 1fr"** and **rowsTemplate** **"1fr 1fr 1fr"**, child components are arranged in vertical layout, with the cross axis equally divided into two columns. 61 62## Events 63 64 65In addition to the [universal events](ts-universal-events-click.md), the following events are supported. 66 67 68| Name| Description| 69| -------- | -------- | 70| onReachStart(event: () => void) | Triggered when the component reaches the start.| 71| onReachEnd(event: () => void) | Triggered when the component reaches the end.| 72 73 74## auto-fill 75 76The **columnsTemplate** and **rowsTemplate** attributes supports **auto-fill** in the following format: 77 78```css 79repeat(auto-fill, track-size) 80``` 81 82Where, **repeat** and **auto-fill** are keywords, and **track-size** indicates the row height or column width. The supported units include px, vp, %, and digits. The value of **track-size** must contain at least one valid row height or column width. 83 84 85## Example 86 87 88```ts 89// WaterFlowDataSource.ets 90 91// Object that implements the IDataSource API, which is used by the <WaterFlow> component to load data. 92export class WaterFlowDataSource implements IDataSource { 93 94 private dataArray: number[] = [] 95 private listeners: DataChangeListener[] = [] 96 97 constructor() { 98 for (let i = 0; i < 100; i++) { 99 this.dataArray.push(i) 100 } 101 } 102 103 // Obtain the data corresponding to the specified index. 104 public getData(index: number): any { 105 return this.dataArray[index] 106 } 107 108 // Notify the controller of data reloading. 109 notifyDataReload(): void { 110 this.listeners.forEach(listener => { 111 listener.onDataReloaded() 112 }) 113 } 114 115 // Notify the controller of data addition. 116 notifyDataAdd(index: number): void { 117 this.listeners.forEach(listener => { 118 listener.onDataAdded(index) 119 }) 120 } 121 122 // Notify the controller of data changes. 123 notifyDataChange(index: number): void { 124 this.listeners.forEach(listener => { 125 listener.onDataChanged(index) 126 }) 127 } 128 129 // Notify the controller of data deletion. 130 notifyDataDelete(index: number): void { 131 this.listeners.forEach(listener => { 132 listener.onDataDeleted(index) 133 }) 134 } 135 136 // Notify the controller of the data location change. 137 notifyDataMove(from: number, to: number): void { 138 this.listeners.forEach(listener => { 139 listener.onDataMoved(from, to) 140 }) 141 } 142 143 // Obtain the total number of data records. 144 public totalCount(): number { 145 return this.dataArray.length 146 } 147 148 // Register the data change listener. 149 registerDataChangeListener(listener: DataChangeListener): void { 150 if (this.listeners.indexOf(listener) < 0) { 151 this.listeners.push(listener) 152 } 153 } 154 155 // Unregister the data change listener. 156 unregisterDataChangeListener(listener: DataChangeListener): void { 157 const pos = this.listeners.indexOf(listener) 158 if (pos >= 0) { 159 this.listeners.splice(pos, 1) 160 } 161 } 162 163 // Add data. 164 public Add1stItem(): void { 165 this.dataArray.splice(0, 0, this.dataArray.length) 166 this.notifyDataAdd(0) 167 } 168 169 // Add an item to the end of the data. 170 public AddLastItem(): void { 171 this.dataArray.splice(this.dataArray.length, 0, this.dataArray.length) 172 this.notifyDataAdd(this.dataArray.length-1) 173 } 174 175 // Add an item to the position corresponding to the specified index. 176 public AddItem(index: number): void { 177 this.dataArray.splice(index, 0, this.dataArray.length) 178 this.notifyDataAdd(index) 179 } 180 181 // Delete the first item. 182 public Delete1stItem(): void { 183 this.dataArray.splice(0, 1) 184 this.notifyDataDelete(0) 185 } 186 187 // Delete the second item. 188 public Delete2ndItem(): void { 189 this.dataArray.splice(1, 1) 190 this.notifyDataDelete(1) 191 } 192 193 // Delete the last item. 194 public DeleteLastItem(): void { 195 this.dataArray.splice(-1, 1) 196 this.notifyDataDelete(this.dataArray.length) 197 } 198 199 // Reload data. 200 public Reload(): void { 201 this.dataArray.splice(1, 1) 202 this.dataArray.splice(3, 2) 203 this.notifyDataReload() 204 } 205} 206``` 207 208```ts 209// WaterflowDemo.ets 210import { WaterFlowDataSource } from './WaterFlowDataSource' 211 212@Entry 213@Component 214struct WaterflowDemo { 215 @State minSize: number = 50 216 @State maxSize: number = 100 217 @State fontSize: number = 24 218 @State colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F] 219 scroller: Scroller = new Scroller() 220 datasource: WaterFlowDataSource = new WaterFlowDataSource() 221 private itemWidthArray: number[] = [] 222 private itemHeightArray: number[] = [] 223 224 // Calculate the width and height of a flow item. 225 getSize() { 226 let ret = Math.floor(Math.random() * this.maxSize) 227 return (ret > this.minSize ? ret : this.minSize) 228 } 229 230 // Save the width and height of the flow item. 231 getItemSizeArray() { 232 for (let i = 0; i < 100; i++) { 233 this.itemWidthArray.push(this.getSize()) 234 this.itemHeightArray.push(this.getSize()) 235 } 236 } 237 238 aboutToAppear() { 239 this.getItemSizeArray() 240 } 241 242 @Builder itemFoot() { 243 Column() { 244 Text(`Footer`) 245 .fontSize(10) 246 .backgroundColor(Color.Red) 247 .width(50) 248 .height(50) 249 .align(Alignment.Center) 250 .margin({ top: 2 }) 251 } 252 } 253 254 build() { 255 Column({ space: 2 }) { 256 WaterFlow({ footer: this.itemFoot.bind(this), scroller: this.scroller }) { 257 LazyForEach(this.datasource, (item: number) => { 258 FlowItem() { 259 Column() { 260 Text("N" + item).fontSize(12).height('16') 261 Image('res/waterFlowTest(' + item % 5 + ').jpg') 262 .objectFit(ImageFit.Fill) 263 .width('100%') 264 .layoutWeight(1) 265 } 266 } 267 .width('100%') 268 .height(this.itemHeightArray[item]) 269 .backgroundColor(this.colors[item % 5]) 270 }, item => item) 271 } 272 .columnsTemplate("1fr 1fr 1fr 1fr") 273 .itemConstraintSize({ 274 minWidth: 0, 275 maxWidth: '100%', 276 minHeight: 0, 277 maxHeight: '100%' 278 }) 279 .columnsGap(10) 280 .rowsGap(5) 281 .onReachStart(() => { 282 console.info("onReachStart") 283 }) 284 .onReachEnd(() => { 285 console.info("onReachEnd") 286 }) 287 .backgroundColor(0xFAEEE0) 288 .width('100%') 289 .height('80%') 290 .layoutDirection(FlexDirection.Column) 291 } 292 } 293} 294``` 295 296 297