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