1# Flex 2 3The **\<Flex>** component allows for flexible layout of child components. 4 5> **NOTE** 6> 7> - This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8> - 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. 9> - 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. 10 11 12## Child Components 13 14Supported 15 16 17## APIs 18 19Flex(value?: FlexOptions) 20 21Creates a standard **\<Flex>** component. For details, see [Flex Layout](../../ui/arkts-layout-development-flex-layout.md). 22 23Since API version 9, this API is supported in ArkTS widgets. 24 25**Parameters** 26 27| Name | Type | Mandatory | Description | 28| -------------- | ---------------------------------------- | ---- | ---------------------------------------- | 29| value | [FlexOptions](#flexoptions) | No | Parameters of the child components in the **\<Flex>** component. | 30 31## FlexOptions 32| Name | Type | Mandatory | Default Value | Description | 33| -------------- | ---------------------------------------- | ---- | ----------------- | ---------------------------------------- | 34| direction | [FlexDirection](ts-appendix-enums.md#flexdirection) | No | FlexDirection.Row | Direction in which child components are arranged in the **\<Flex>** component, that is, the direction of the main axis. | 35| wrap | [FlexWrap](ts-appendix-enums.md#flexwrap) | No | FlexWrap.NoWrap | Whether the **\<Flex>** component has a single line or multiple lines.<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.| 36| justifyContent | [FlexAlign](ts-appendix-enums.md#flexalign) | No | FlexAlign.Start | Alignment mode of the child components in the **\<Flex>** component along the main axis. | 37| alignItems | [ItemAlign](ts-appendix-enums.md#itemalign) | No | ItemAlign.Start | Alignment mode of the child components in the **\<Flex>** component along the cross axis. | 38| alignContent | [FlexAlign](ts-appendix-enums.md#flexalign) | No | FlexAlign.Start | 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**.| 39 40## Example 41 42### Example 1 43 44```ts 45// xxx.ets 46@Entry 47@Component 48struct FlexExample1 { 49 build() { 50 Column() { 51 Column({ space: 5 }) { 52 Text('direction:Row').fontSize(9).fontColor(0xCCCCCC).width('90%') 53 Flex({ direction: FlexDirection.Row }) { // The child components are arranged in the same direction as the main axis runs along the rows. 54 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 55 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 56 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 57 Text('4').width('20%').height(50).backgroundColor(0xD2B48C) 58 } 59 .height(70) 60 .width('90%') 61 .padding(10) 62 .backgroundColor(0xAFEEEE) 63 64 Text('direction:RowReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 65 Flex({ direction: FlexDirection.RowReverse }) { // The child components are arranged opposite to the Row direction. 66 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 67 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 68 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 69 Text('4').width('20%').height(50).backgroundColor(0xD2B48C) 70 } 71 .height(70) 72 .width('90%') 73 .padding(10) 74 .backgroundColor(0xAFEEEE) 75 76 Text('direction:Column').fontSize(9).fontColor(0xCCCCCC).width('90%') 77 Flex({ direction: FlexDirection.Column }) { // The child components are arranged in the same direction as the main axis runs down the columns. 78 Text('1').width('100%').height(40).backgroundColor(0xF5DEB3) 79 Text('2').width('100%').height(40).backgroundColor(0xD2B48C) 80 Text('3').width('100%').height(40).backgroundColor(0xF5DEB3) 81 Text('4').width('100%').height(40).backgroundColor(0xD2B48C) 82 } 83 .height(160) 84 .width('90%') 85 .padding(10) 86 .backgroundColor(0xAFEEEE) 87 88 Text('direction:ColumnReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 89 Flex({ direction: FlexDirection.ColumnReverse }) { // The child components are arranged opposite to the Column direction. 90 Text('1').width('100%').height(40).backgroundColor(0xF5DEB3) 91 Text('2').width('100%').height(40).backgroundColor(0xD2B48C) 92 Text('3').width('100%').height(40).backgroundColor(0xF5DEB3) 93 Text('4').width('100%').height(40).backgroundColor(0xD2B48C) 94 } 95 .height(160) 96 .width('90%') 97 .padding(10) 98 .backgroundColor(0xAFEEEE) 99 }.width('100%').margin({ top: 5 }) 100 }.width('100%') 101 } 102} 103``` 104 105 106 107### Example 2 108 109```ts 110// xxx.ets 111@Entry 112@Component 113struct FlexExample2 { 114 build() { 115 Column() { 116 Column({ space: 5 }) { 117 Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 118 Flex({ wrap: FlexWrap.Wrap }) { // The child components are arranged in multiple lines, and they may overflow. 119 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 120 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 121 Text('3').width('50%').height(50).backgroundColor(0xD2B48C) 122 } 123 .width('90%') 124 .padding(10) 125 .backgroundColor(0xAFEEEE) 126 127 Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%') 128 Flex({ wrap: FlexWrap.NoWrap }) { // The child components are arranged in a single line, and they cannot overflow. 129 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 130 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 131 Text('3').width('50%').height(50).backgroundColor(0xF5DEB3) 132 } 133 .width('90%') 134 .padding(10) 135 .backgroundColor(0xAFEEEE) 136 137 Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%') 138 Flex({ wrap: FlexWrap.WrapReverse , direction:FlexDirection.Row }) { // The child components are reversely arranged in multiple lines, and they may overflow. 139 Text('1').width('50%').height(50).backgroundColor(0xF5DEB3) 140 Text('2').width('50%').height(50).backgroundColor(0xD2B48C) 141 Text('3').width('50%').height(50).backgroundColor(0xD2B48C) 142 } 143 .width('90%') 144 .height(120) 145 .padding(10) 146 .backgroundColor(0xAFEEEE) 147 }.width('100%').margin({ top: 5 }) 148 }.width('100%') 149 } 150} 151``` 152 153 154 155### Example 3 156 157```ts 158// xxx.ets 159@Component 160struct JustifyContentFlex { 161 justifyContent : number = 0; 162 163 build() { 164 Flex({ justifyContent: this.justifyContent }) { 165 Text('1').width('20%').height(50).backgroundColor(0xF5DEB3) 166 Text('2').width('20%').height(50).backgroundColor(0xD2B48C) 167 Text('3').width('20%').height(50).backgroundColor(0xF5DEB3) 168 } 169 .width('90%') 170 .padding(10) 171 .backgroundColor(0xAFEEEE) 172 } 173} 174 175@Entry 176@Component 177struct FlexExample3 { 178 build() { 179 Column() { 180 Column({ space: 5 }) { 181 Text('justifyContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 182 JustifyContentFlex({ justifyContent: FlexAlign.Start }) // The child components are aligned with the start edge of the main axis. 183 184 Text('justifyContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 185 JustifyContentFlex({ justifyContent: FlexAlign.Center }) // The child components are aligned in the center of the main axis. 186 187 Text('justifyContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 188 JustifyContentFlex({ justifyContent: FlexAlign.End }) // The child components are aligned with the end edge of the main axis. 189 190 Text('justifyContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%') 191 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. 192 193 Text('justifyContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%') 194 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. 195 196 Text('justifyContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%') 197 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. 198 }.width('100%').margin({ top: 5 }) 199 }.width('100%') 200 } 201} 202``` 203 204 205 206### Example 4 207 208```ts 209// xxx.ets 210@Component 211struct AlignItemsFlex { 212 alignItems : number = 0; 213 214 build() { 215 Flex({ alignItems: this.alignItems }) { 216 Text('1').width('33%').height(30).backgroundColor(0xF5DEB3) 217 Text('2').width('33%').height(40).backgroundColor(0xD2B48C) 218 Text('3').width('33%').height(50).backgroundColor(0xF5DEB3) 219 } 220 .size({width: '90%', height: 80}) 221 .padding(10) 222 .backgroundColor(0xAFEEEE) 223 } 224} 225 226@Entry 227@Component 228struct FlexExample4 { 229 build() { 230 Column() { 231 Column({ space: 5 }) { 232 Text('alignItems:Auto').fontSize(9).fontColor(0xCCCCCC).width('90%') 233 AlignItemsFlex({ alignItems: ItemAlign.Auto }) // The items in the container are aligned with the cross-start edge. 234 235 Text('alignItems:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 236 AlignItemsFlex({ alignItems: ItemAlign.Start }) // The items in the container are aligned with the cross-start edge. 237 238 Text('alignItems:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 239 AlignItemsFlex({alignItems: ItemAlign.Center}) // The items in the container are centered along the cross axis. 240 241 Text('alignItems:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 242 AlignItemsFlex({ alignItems: ItemAlign.End }) // The items in the container are aligned with the cross-end edge. 243 244 Text('alignItems:Stretch').fontSize(9).fontColor(0xCCCCCC).width('90%') 245 AlignItemsFlex({ alignItems: ItemAlign.Stretch }) // The items in the container are stretched and padded along the cross axis. 246 247 Text('alignItems:Baseline').fontSize(9).fontColor(0xCCCCCC).width('90%') 248 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. 249 }.width('100%').margin({ top: 5 }) 250 }.width('100%') 251 } 252} 253``` 254 255 256 257### Example 5 258 259```ts 260// xxx.ets 261@Component 262struct AlignContentFlex { 263 alignContent: number = 0; 264 265 build() { 266 Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) { 267 Text('1').width('50%').height(20).backgroundColor(0xF5DEB3) 268 Text('2').width('50%').height(20).backgroundColor(0xD2B48C) 269 Text('3').width('50%').height(20).backgroundColor(0xD2B48C) 270 } 271 .size({ width: '90%', height: 90 }) 272 .padding(10) 273 .backgroundColor(0xAFEEEE) 274 } 275} 276 277@Entry 278@Component 279struct FlexExample5 { 280 build() { 281 Column() { 282 Column({ space: 5 }) { 283 Text('alignContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%') 284 AlignContentFlex({ alignContent: FlexAlign.Start }) // The child components are aligned with the start edge in the multi-row layout. 285 286 Text('alignContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%') 287 AlignContentFlex({ alignContent: FlexAlign.Center }) // The child components are aligned in the center in the multi-row layout. 288 289 Text('alignContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%') 290 AlignContentFlex({ alignContent: FlexAlign.End }) // The child components are aligned with the end edge in the multi-row layout. 291 292 Text('alignContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%') 293 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. 294 295 Text('alignContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%') 296 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. 297 298 Text('alignContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%') 299 Flex({ 300 wrap: FlexWrap.Wrap, 301 alignContent: FlexAlign.SpaceEvenly 302 }) {// 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. 303 Text('1').width('50%').height(20).backgroundColor(0xF5DEB3) 304 Text('2').width('50%').height(20).backgroundColor(0xD2B48C) 305 Text('3').width('50%').height(20).backgroundColor(0xF5DEB3) 306 Text('4').width('50%').height(20).backgroundColor(0xD2B48C) 307 Text('5').width('50%').height(20).backgroundColor(0xF5DEB3) 308 } 309 .size({ width: '90%', height: 100 }) 310 .padding({ left: 10, right: 10 }) 311 .backgroundColor(0xAFEEEE) 312 }.width('100%').margin({ top: 5 }) 313 }.width('100%') 314 } 315} 316``` 317 318 319