1# Flex 2 3The **Flex** component is a container that lays out its children using a flexible box model, providing an effective way to arrange, align, and distribute remaining space among child elements. 4 5For details, see [Flex Layout](../../../ui/arkts-layout-development-flex-layout.md). 6 7> **NOTE** 8> 9> - This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 10> - The **Flex** component adapts the layout of flex items during rendering. This may affect the performance. Therefore, you are advised to use [Column](ts-container-column.md) or [Row](ts-container-row.md) instead under scenarios where consistently high performance is required. 11> - If the main axis of the **Flex** component is not set, it follows the size of the parent container. On the contrary, if the main axis of the [Column](ts-container-column.md) or [Row](ts-container-row.md) component is not set, it follows the size of their child component. 12> - You can set the main axis length of a **Flex** component to **auto** to make it adapt to the layout of its child components. This way, the **Flex** component's length is subject to the **constraintSize** attribute and the maximum and minimum lengths passed from the parent container, with **constraintSize** taking precedence. 13 14 15## Child Components 16 17Supported 18 19 20## APIs 21 22Flex(value?: FlexOptions) 23 24Creates a **Flex** component. 25 26**Widget capability**: This API can be used in ArkTS widgets since API version 9. 27 28**Atomic service API**: This API can be used in atomic services since API version 11. 29 30**System capability**: SystemCapability.ArkUI.ArkUI.Full 31 32**Parameters** 33 34| Name | Type | Mandatory | Description | 35| -------------- | ---------------------------------------- | ---- | ---------------------------------------- | 36| value | [FlexOptions](#flexoptions) | No | Parameters of the child components in the **Flex** component. | 37 38## FlexOptions 39 40Describes the layout and alignment of child components within the **Flex** component. 41 42**Atomic service API**: This API can be used in atomic services since API version 11. 43 44**System capability**: SystemCapability.ArkUI.ArkUI.Full 45 46| Name | Type | Mandatory | Description | 47| -------------- | ---------------------------------------- | ---- | ---------------------------------------- | 48| direction | [FlexDirection](ts-appendix-enums.md#flexdirection) | No | Direction in which child components are arranged in the **Flex** component, that is, the direction of the main axis.<br>Default value: **FlexDirection.Row**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9. | 49| wrap | [FlexWrap](ts-appendix-enums.md#flexwrap) | No | Whether the **Flex** component has a single line or multiple lines.<br>Default value: **FlexWrap.NoWrap**<br>**NOTE**<br>When wrapped onto multiple lines, the child elements on the new line are stacked in the direction based on the cross axis direction.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 50| justifyContent | [FlexAlign](ts-appendix-enums.md#flexalign) | No | Alignment mode of the child components in the **Flex** component along the main axis.<br>Default value: **FlexAlign.Start**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9. | 51| alignItems | [ItemAlign](ts-appendix-enums.md#itemalign) | No | Alignment mode of the child components in the **Flex** component along the cross axis.<br>Default value: **ItemAlign.Start**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9. | 52| alignContent | [FlexAlign](ts-appendix-enums.md#flexalign) | No | Alignment mode of the child components in a multi-row **Flex** component along the cross axis. This parameter is valid only when **wrap** is set to **Wrap** or **WrapReverse**.<br>Default value: **FlexAlign.Start**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9. | 53| space<sup>12+</sup> | [FlexSpaceOptions<sup>12+</sup>](ts-container-flex.md#flexspaceoptions12) | No | Spacing between child components along the main axis or cross axis of the **Flex** component.<br>Default value: **{main:LengthMetrics.px(0), cross:LengthMetrics.px(0)}**<br>This parameter does not take effect if the value specified is a negative number or percentage, or if **justifyContent** is set to **FlexAlign.SpaceBetween**, **FlexAlign.SpaceAround**, or **FlexAlign.SpaceEvenly**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 54 55## FlexSpaceOptions<sup>12+</sup> 56 57**Atomic service API**: This API can be used in atomic services since API version 12. 58 59**System capability**: SystemCapability.ArkUI.ArkUI.Full 60 61| Name | Type | Read Only | Optional | Description | 62| ----------- | --------- | ----------- | --------- |----------- | 63| main | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Yes| Space on the main axis of the **Flex** component.<br> space: {main: LengthMetrics.unit(value)} | 64| cross | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Yes| Space on the cross axis of the **Flex** component.<br> space: {cross: LengthMetrics.unit(value)} | 65 66## Example 67 68### Example 1: Setting the Child Component Layout Direction 69This example demonstrates different layout directions for child components by setting the **direction** property. 70```ts 71// xxx.ets 72@Entry 73@Component 74struct FlexExample1 { 75 build() { 76 Column() { 77 Column({ space: 5 }) { 78 Text('direction:Row').fontSize(9).fontColor(0xCCCCCC).width('90%') 79 Flex({ direction: FlexDirection.Row }) { // The child components are arranged in the same direction as the main axis runs along the rows. 80 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 81 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 82 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 83 Text('4').width('20%').height(50).backgroundColor(0xD2B48C) 84 } 85 .height(70) 86 .width('90%') 87 .padding(10) 88 .backgroundColor(0xAFEEEE) 89 90 Text('direction:RowReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 91 Flex({ direction: FlexDirection.RowReverse }) { // The child components are arranged opposite to the Row direction. 92 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 93 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 94 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 95 Text('4').width('20%').height(50).backgroundColor(0xD2B48C) 96 } 97 .height(70) 98 .width('90%') 99 .padding(10) 100 .backgroundColor(0xAFEEEE) 101 102 Text('direction:Column').fontSize(9).fontColor(0xCCCCCC).width('90%') 103 Flex({ direction: FlexDirection.Column }) { // The child components are arranged in the same direction as the main axis runs down the columns. 104 Text('1').width('100%').height(40).backgroundColor(0xF5DEB3) 105 Text('2').width('100%').height(40).backgroundColor(0xD2B48C) 106 Text('3').width('100%').height(40).backgroundColor(0xF5DEB3) 107 Text('4').width('100%').height(40).backgroundColor(0xD2B48C) 108 } 109 .height(160) 110 .width('90%') 111 .padding(10) 112 .backgroundColor(0xAFEEEE) 113 114 Text('direction:ColumnReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 115 Flex({ direction: FlexDirection.ColumnReverse }) { // The child components are arranged opposite to the Column direction. 116 Text('1').width('100%').height(40).backgroundColor(0xF5DEB3) 117 Text('2').width('100%').height(40).backgroundColor(0xD2B48C) 118 Text('3').width('100%').height(40).backgroundColor(0xF5DEB3) 119 Text('4').width('100%').height(40).backgroundColor(0xD2B48C) 120 } 121 .height(160) 122 .width('90%') 123 .padding(10) 124 .backgroundColor(0xAFEEEE) 125 }.width('100%').margin({ top: 5 }) 126 }.width('100%') 127 } 128} 129``` 130 131 132 133### Example 2: Implementing Single- and Multi-Line Layouts 134This example demonstrates single-line and multi-line layouts for child components by setting the **wrap** property. 135```ts 136// xxx.ets 137@Entry 138@Component 139struct FlexExample2 { 140 build() { 141 Column() { 142 Column({ space: 5 }) { 143 Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 144 Flex({ wrap: FlexWrap.Wrap }) { // The child components are arranged in multiple lines. 145 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 146 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 147 Text('3').width('50%').height(50).backgroundColor(0xD2B48C) 148 } 149 .width('90%') 150 .padding(10) 151 .backgroundColor(0xAFEEEE) 152 153 Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 154 Flex({ wrap: FlexWrap.NoWrap }) { // The child components are arranged in a single line. 155 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 156 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 157 Text('3').width('50%').height(50).backgroundColor(0xF5DEB3) 158 } 159 .width('90%') 160 .padding(10) 161 .backgroundColor(0xAFEEEE) 162 163 Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 164 Flex({ wrap: FlexWrap.WrapReverse , direction:FlexDirection.Row }) { // The child components are reversely arranged in multiple lines, and they may overflow. 165 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 166 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 167 Text('3').width('50%').height(50).backgroundColor(0xD2B48C) 168 } 169 .width('90%') 170 .height(120) 171 .padding(10) 172 .backgroundColor(0xAFEEEE) 173 }.width('100%').margin({ top: 5 }) 174 }.width('100%') 175 } 176} 177``` 178 179 180 181### Example 3: Setting Alignment Along the Main Axis 182This example demonstrates different alignment effects for child components along the main axis by setting the **justifyContent** property. 183```ts 184// xxx.ets 185@Component 186struct JustifyContentFlex { 187 justifyContent : number = 0; 188 189 build() { 190 Flex({ justifyContent: this.justifyContent }) { 191 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 192 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 193 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 194 } 195 .width('90%') 196 .padding(10) 197 .backgroundColor(0xAFEEEE) 198 } 199} 200 201@Entry 202@Component 203struct FlexExample3 { 204 build() { 205 Column() { 206 Column({ space: 5 }) { 207 Text('justifyContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 208 JustifyContentFlex({ justifyContent: FlexAlign.Start }) // The child components are aligned with the start edge of the main axis. 209 210 Text('justifyContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 211 JustifyContentFlex({ justifyContent: FlexAlign.Center }) // The child components are aligned in the center of the main axis. 212 213 Text('justifyContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 214 JustifyContentFlex({ justifyContent: FlexAlign.End }) // The child components are aligned with the end edge of the main axis. 215 216 Text('justifyContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%') 217 JustifyContentFlex({ justifyContent: FlexAlign.SpaceBetween }) // The child components are evenly distributed along the main axis. The first component is aligned with the main-start, the last component is aligned with the main-end. 218 219 Text('justifyContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%') 220 JustifyContentFlex({ justifyContent: FlexAlign.SpaceAround }) // The child components are evenly distributed along the main axis. The space between the first component and main-start, and that between the last component and cross-main are both half the size of the space between two adjacent components. 221 222 Text('justifyContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%') 223 JustifyContentFlex({ justifyContent: FlexAlign.SpaceEvenly }) // The child components are evenly distributed along the main axis. The space between the first component and main-start, the space between the last component and main-end, and the space between any two adjacent components are the same. 224 }.width('100%').margin({ top: 5 }) 225 }.width('100%') 226 } 227} 228``` 229 230 231 232### Example 4: Setting Alignment Along the Cross Axis 233This example demonstrates different alignment effects for child components along the cross axis by setting the **alignItems** property. 234```ts 235// xxx.ets 236@Component 237struct AlignItemsFlex { 238 alignItems : number = 0; 239 240 build() { 241 Flex({ alignItems: this.alignItems }) { 242 Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 243 Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 244 Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 245 } 246 .size({width: '90%', height: 80}) 247 .padding(10) 248 .backgroundColor(0xAFEEEE) 249 } 250} 251 252@Entry 253@Component 254struct FlexExample4 { 255 build() { 256 Column() { 257 Column({ space: 5 }) { 258 Text('alignItems:Auto').fontSize(9).fontColor(0xCCCCCC).width('90%') 259 AlignItemsFlex({ alignItems: ItemAlign.Auto }) // The items in the container are aligned with the cross-start edge. 260 261 Text('alignItems:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 262 AlignItemsFlex({ alignItems: ItemAlign.Start }) // The items in the container are aligned with the cross-start edge. 263 264 Text('alignItems:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 265 AlignItemsFlex({alignItems: ItemAlign.Center}) // The items in the container are centered along the cross axis. 266 267 Text('alignItems:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 268 AlignItemsFlex({ alignItems: ItemAlign.End }) // The items in the container are aligned with the cross-end edge. 269 270 Text('alignItems:Stretch').fontSize(9).fontColor(0xCCCCCC).width('90%') 271 AlignItemsFlex({ alignItems: ItemAlign.Stretch }) // The items in the container are stretched and padded along the cross axis. 272 273 Text('alignItems:Baseline').fontSize(9).fontColor(0xCCCCCC).width('90%') 274 AlignItemsFlex({ alignItems: ItemAlign.Baseline }) // The items in the container are aligned in such a manner that their text baselines are aligned along the cross axis. 275 }.width('100%').margin({ top: 5 }) 276 }.width('100%') 277 } 278} 279``` 280 281 282 283### Example 5: Setting Alignment of Multiple Lines 284This example demonstrates different alignment effects for multiple lines of content by setting the **alignContent** property. 285```ts 286// xxx.ets 287@Component 288struct AlignContentFlex { 289 alignContent: number = 0; 290 291 build() { 292 Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) { 293 Text('1').width('50%').height(20).backgroundColor(0xF5DEB3) 294 Text('2').width('50%').height(20).backgroundColor(0xD2B48C) 295 Text('3').width('50%').height(20).backgroundColor(0xD2B48C) 296 } 297 .size({ width: '90%', height: 90 }) 298 .padding(10) 299 .backgroundColor(0xAFEEEE) 300 } 301} 302 303@Entry 304@Component 305struct FlexExample5 { 306 build() { 307 Column() { 308 Column({ space: 5 }) { 309 Text('alignContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 310 AlignContentFlex({ alignContent: FlexAlign.Start }) // The child components are aligned with the start edge in the multi-row layout. 311 312 Text('alignContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 313 AlignContentFlex({ alignContent: FlexAlign.Center }) // The child components are aligned in the center in the multi-row layout. 314 315 Text('alignContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 316 AlignContentFlex({ alignContent: FlexAlign.End }) // The child components are aligned with the end edge in the multi-row layout. 317 318 Text('alignContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%') 319 AlignContentFlex({ alignContent: FlexAlign.SpaceBetween }) // In the multi-row layout, the child component in the first row is aligned with the start edge of the column, and the child component in the last row is aligned with the end edge of the column. 320 321 Text('alignContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%') 322 AlignContentFlex({ alignContent: FlexAlign.SpaceAround }) // In the multi-row layout, the space between the child component in the first row and the start edge of the column, and that between the child component in the last row and the end edge of the column are both half the size of the space between two adjacent rows. 323 324 Text('alignContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%') 325 Flex({ 326 wrap: FlexWrap.Wrap, 327 alignContent: FlexAlign.SpaceEvenly 328 }) {// In the multi-row layout, the space between the child component in the first row and the start edge of the column, the space between the child component in the last row and the end edge of the column, and the space between any two adjacent rows are the same. 329 Text('1').width('50%').height(20).backgroundColor(0xF5DEB3) 330 Text('2').width('50%').height(20).backgroundColor(0xD2B48C) 331 Text('3').width('50%').height(20).backgroundColor(0xF5DEB3) 332 Text('4').width('50%').height(20).backgroundColor(0xD2B48C) 333 Text('5').width('50%').height(20).backgroundColor(0xF5DEB3) 334 } 335 .size({ width: '90%', height: 100 }) 336 .padding({ left: 10, right: 10 }) 337 .backgroundColor(0xAFEEEE) 338 }.width('100%').margin({ top: 5 }) 339 }.width('100%') 340 } 341} 342``` 343 344 345 346### Example 6: Setting the Spacing Between Child Components Along the Main Axis or Cross Axis 347This example shows how to set the spacing between child components along the main axis or cross axis using the **space** property. 348```ts 349import {LengthMetrics} from '@kit.ArkUI'; 350 351@Entry 352@Component 353struct FlexExample2 { 354 build() { 355 Column() { 356 Column({ space: 5 }) { 357 Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 358 Flex({ wrap: FlexWrap.Wrap, space: {main: LengthMetrics.px(50), cross: LengthMetrics.px(50)} }) { // The child components are arranged in multiple lines. 359 Text('1').width('40%').height(50).backgroundColor(0xF5DEB3) 360 Text('2').width('40%').height(50).backgroundColor(0xD2B48C) 361 Text('3').width('40%').height(50).backgroundColor(0xD2B48C) 362 } 363 .width('90%') 364 .padding(10) 365 .backgroundColor(0xAFEEEE) 366 367 Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 368 Flex({ wrap: FlexWrap.NoWrap, space: {main: LengthMetrics.px(50), cross: LengthMetrics.px(50)} }) { // The child components are arranged in a single line. 369 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 370 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 371 Text('3').width('50%').height(50).backgroundColor(0xF5DEB3) 372 } 373 .width('90%') 374 .padding(10) 375 .backgroundColor(0xAFEEEE) 376 377 Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 378 Flex({ wrap: FlexWrap.WrapReverse, direction:FlexDirection.Row, space: {main: LengthMetrics.px(50), cross: LengthMetrics.px(50)} }) { // The child components are reversely arranged in multiple lines. 379 Text('1').width('40%').height(50).backgroundColor(0xF5DEB3) 380 Text('2').width('40%').height(50).backgroundColor(0xD2B48C) 381 Text('3').width('40%').height(50).backgroundColor(0xD2B48C) 382 } 383 .width('90%') 384 .height(120) 385 .padding(10) 386 .backgroundColor(0xAFEEEE) 387 }.width('100%').margin({ top: 5 }) 388 }.width('100%') 389 } 390} 391``` 392 393 394 395### Example 7: Implementing a Flex Component with Adaptive Width 396This example shows how the **Flex** component can automatically adjust to fit the layout of child components when the width is set to **auto**. 397```ts 398@Component 399struct Demo { 400 @Require @Prop text: string 401 402 build() { 403 Button() { 404 Flex() { 405 Image($r('sys.media.ohos_ic_public_voice')) 406 .width(16) 407 .height(16) 408 409 Row() { 410 Text(this.text) 411 .margin({ 412 left: 6, 413 right: 6 414 }) 415 .fontSize(14) 416 .maxLines(1) 417 .textOverflow({ overflow: TextOverflow.Ellipsis }) 418 } 419 420 Image($r('sys.media.ohos_ic_public_sound')) 421 .width(16) 422 .height(16) 423 }.width("auto") 424 } 425 .backgroundColor(0xAFEEEE) 426 .height(36) 427 .padding({ left: 16, right: 16 }) 428 .constraintSize({ maxWidth: 156 }) 429 .width("auto") 430 } 431} 432 433@Entry 434@Component 435struct Index { 436 build() { 437 Column({ space: 12 }) { 438 Text("Width does not reach max length").fontSize(11).fontColor(0XCCCCCC).width("50%") 439 Demo({ text: "123" }) 440 Text("Width reaches max length").fontSize(11).fontColor(0XCCCCCC).width("50%") 441 Demo({ text: "1234567890-1234567890-1234567890-1234567890" }) 442 } 443 } 444} 445``` 446 447 448