1# Creating a Simple Page 2 3In this section, we will develop an infographic food details page, by building custom components through the container components **\<Stack>** and **\<Flex>** as well as basic components **\<Image>** and **\<Text>**. 4 5 6## Building the Stack Layout 7 81. Create a food name. 9 10 Delete the code of the **build** method in the project template, create a **\<Stack>** component, and place the **\<Text>** component in the braces of the **\<Stack>** component. When the **\<Stack>** component contains multiple child components, the latter child component overwrites the former one. 11 12 ``` 13 @Entry 14 @Component 15 struct MyComponent { 16 build() { 17 Stack() { 18 Text('Tomato') 19 .fontSize(26) 20 .fontWeight(500) 21 } 22 } 23 } 24 ``` 25 26  27 282. Display food pictures. 29 30 Create an **\<Image>** component and specify a URL for it. To display the **\<Text>** component above the **\<Image>** component, declare the **\<Image>** component first. Image resources are stored in the **rawfile** folder in **resources**. When referencing the resources in the **rawfile** folder, use the `$rawfile('filename')` format, where **filename** indicates the relative path of the file in the **rawfile** folder. Currently,` $rawfile` only allows the **\<Image>** component to reference image resources. 31 32 ``` 33 @Entry 34 @Component 35 struct MyComponent { 36 build() { 37 Stack() { 38 Image($rawfile('Tomato.png')) 39 Text('Tomato') 40 .fontSize(26) 41 .fontWeight(500) 42 } 43 } 44 } 45 ``` 46 47 48 49 503. Access images through resources. 51 52 In addition to specifying the image path, you can also use the media resource symbol **$r** to reference resources based on the resource qualifier rules in the **resources** folder. Right-click the **resources** folder, choose **New** > **Resource Directory** from the shortcut menu, and set **Resource Type** to **Media** (image resource). 53 54 Place **Tomato.png** in the **media** folder. You can then reference the application resources in the `$r('app.type.name')` format, which is `$r('app.media.Tomato')` in this example. 55 56``` 57@Entry 58 @Component 59 struct MyComponent { 60 build() { 61 Stack() { 62 Image($r('app.media.Tomato')) 63 .objectFit(ImageFit.Contain) 64 .height(357) 65 Text('Tomato') 66 .fontSize(26) 67 .fontWeight(500) 68 } 69 } 70 } 71``` 72 73 744. Set the width and height of the image, and set the **objectFit** attribute of the image to **ImageFit.Contain**, which means to keep the aspect ratio of the image to ensure that the image is completely displayed within the boundary. 75 76 If the image fills the entire screen, the possible causes are as follows: 77 78 - The width and height of the image are not set. 79 - The default attribute of **objectFit** of the image is **ImageFit.Cover**, that is, the image is zoomed in or zoomed out to fill the entire display boundary with the aspect ratio locked. 80 81``` 82@Entry 83 @Component 84 struct MyComponent { 85 build() { 86 Stack() { 87 Image($r('app.media.Tomato')) 88 .objectFit(ImageFit.Contain) 89 .height(357) 90 Text('Tomato') 91 .fontSize(26) 92 .fontWeight(500) 93 } 94 } 95 } 96``` 97 98 99  100 1015. Set the food image and name layout. 102 103 Set **alignContent** of the **\<Stack>** component to **Alignment.BottomStart**. Similar to **FontWeight**, **Alignment** is a built-in enumeration type provided by the framework. 104 105``` 106@Entry 107 @Component 108 struct MyComponent { 109 build() { 110 Stack({ alignContent: Alignment.BottomStart }) { 111 Image($r('app.media.Tomato')) 112 .objectFit(ImageFit.Contain) 113 .height(357) 114 Text('Tomato') 115 .fontSize(26) 116 .fontWeight(500) 117 } 118 } 119 } 120``` 121 122 123  124 1256. You can change the background color of the food image by setting the background color of the **\<Stack>** component in any of the following ways: 126 1. Using a built-in enumeration value of **Color** provided by the framework. For example, **backgroundColor(Color.Red)** indicates that the background color is set to red. 127 2. Using a value of the string type. The supported color formats are rgb, rgba, and HEX. For example, you can set the background color to blue through **backgroundColor(??#0000FF??)** and set the background color to white through **backgroundColor(??rgb(255, 255, 255)??)**. 128 3. Using a value of the number type. Hexadecimal color values are supported. For example, **backgroundColor(0xFF0000)** indicates that the background color is red. 129 130 4. Using a value of the Resource type. For details about the Resource type, see [Resource Access](ts-resource-access.md). 131 132 133``` 134@Entry 135 @Component 136 struct MyComponent { 137 build() { 138 Stack({ alignContent: Alignment.BottomStart }) { 139 Image($r('app.media.Tomato')) 140 .objectFit(ImageFit.Contain) 141 .height(357) 142 Text('Tomato') 143 .fontSize(26) 144 .fontWeight(500) 145 } 146 .backgroundColor('#FFedf2f5') 147 } 148} 149``` 150 151 152  153 1547. Adjust the left and bottom margin of the **\<Text>** component. Margin is a shorthand attribute. You can specify the margins of the four edges in a unified manner or separately. 155 - To set the margins of the four edges in a unified manner, use the **Margin(Length)** format. For example, **margin(20)** indicates that the margins of the top, right, bottom, and left edges are all 20. 156 157 - To set the margins of the four edges separately, use the **{top?: Length, right?: Length, bottom?: Length, left?:Length}** format. For example, **margin({ left: 26, bottom: 17.4 })** indicates that the left margin is 26 and the bottom margin is 17.4. 158 159``` 160@Entry 161 @Component 162 struct MyComponent { 163 build() { 164 Stack({ alignContent: Alignment.BottomStart }) { 165 Image($r('app.media.Tomato')) 166 .objectFit(ImageFit.Contain) 167 .height(357) 168 Text('Tomato') 169 .fontSize(26) 170 .fontWeight(500) 171 .margin({left: 26, bottom: 17.4}) 172 } 173 .backgroundColor('#FFedf2f5') 174 } 175 } 176``` 177 178 179  180 1818. Adjust the structure between components and semanticize component names. Create the **FoodDetail** page entry component, create a **\<Column>** component in **FoodDetail**, and set the alignment to **alignItems(HorizontalAlign.Center)**. Change the name of the **MyComponent** component to **FoodImageDisplay**, which is a child component of the **FoodDetail** component. 182 183 The **\<Column>** component is a container whose child components are vertically arranged. It is in linear layout in essence. Therefore, only the alignment in the cross axis direction can be set. 184 185``` 186@Component 187 struct FoodImageDisplay { 188 build() { 189 Stack({ alignContent: Alignment.BottomStart }) { 190 Image($r('app.media.Tomato')) 191 .objectFit(ImageFit.Contain) 192 Text('Tomato') 193 .fontSize(26) 194 .fontWeight(500) 195 .margin({ left: 26, bottom: 17.4 }) 196 } 197 .height(357) 198 .backgroundColor('#FFedf2f5') 199 } 200 } 201 202 @Entry 203 @Component 204 struct FoodDetail { 205 build() { 206 Column() { 207 FoodImageDisplay() 208 } 209 .alignItems(HorizontalAlign.Center) 210 } 211 } 212``` 213 214 215 216## Building the Flex Layout 217 218Use the **Flex** layout to build a food composition table. In this way, cell sizes are flexibly set based on the proportion, eliminating the need for width and height calculation. 219 2201. Create a **ContentTable** component as a child component of the **FoodDetail** component. 221 222``` 223@Component 224 struct FoodImageDisplay { 225 build() { 226 Stack({ alignContent: Alignment.BottomStart }) { 227 Image($r('app.media.Tomato')) 228 .objectFit(ImageFit.Contain) 229 .height(357) 230 Text('Tomato') 231 .fontSize(26) 232 .fontWeight(500) 233 .margin({ left: 26, bottom: 17.4 }) 234 } 235 .backgroundColor('#FFedf2f5') 236 } 237 } 238 239 @Component 240 struct ContentTable { 241 build() {} 242 } 243 244 @Entry 245 @Component 246 struct FoodDetail { 247 build() { 248 Column() { 249 FoodImageDisplay() 250 ContentTable() 251 } 252 .alignItems(HorizontalAlign.Center) 253 } 254 } 255``` 256 257 2582. Create a **\<Flex>** component to display two food composition categories in the tomato: **Calories** and **Nutrition**. 259 260 **Calories** contains information about calories. **Nutrition** contains information about protein, fat, carbohydrates, and vitamin C. 261 262 Create the **Calories** class. Create a **\<Flex>** component and set its height to **280**, and the top, right, and left margins to **30**. The **Flex** component contains three **\<Text>** child components, which represent the category name (**Calories**), content name (**Calories**), and contain value (**17 kcal**), respectively. By default, child components in the **Flex** component are arranged horizontally. 263 264 In the following example, code of **FoodImageDisplay** is omitted, and only code of **ContentTable** is provided. 265 266``` 267@Component 268 struct ContentTable { 269 build() { 270 Flex() { 271 Text('Calories') 272 .fontSize(17.4) 273 .fontWeight(FontWeight.Bold) 274 Text('Calories') 275 .fontSize(17.4) 276 Text('17kcal') 277 .fontSize(17.4) 278 } 279 .height(280) 280 .padding({ top: 30, right: 30, left: 30 }) 281 } 282 } 283 284 @Entry 285 @Component 286 struct FoodDetail { 287 build() { 288 Column() { 289 FoodImageDisplay() 290 ContentTable() 291 } 292 .alignItems(HorizontalAlign.Center) 293 } 294 } 295``` 296 297 298  299 3003. Adjust the layout and set the proportion (**layoutWeight**) of each part. Set **layoutWeight** of the category name to **1**, and **layoutWeight** of the content name and content value to **2**. The content name and content value are in a same **Flex**, and the content name occupies all remaining space **flexGrow(1)**. 301 302``` 303@Component 304 struct FoodImageDisplay { 305 build() { 306 Stack({ alignContent: Alignment.BottomStart }) { 307 Image($m('Tomato.png')) 308 .objectFit(ImageFit.Contain) 309 .height(357) 310 Text('Tomato') 311 .fontSize(26) 312 .fontWeight(500) 313 .margin({ left: 26, bottom: 17.4 }) 314 } 315 .backgroundColor('#FFedf2f5') 316 } 317 } 318 319 @Component 320 struct ContentTable { 321 build() { 322 Flex() { 323 Text('Calories') 324 .fontSize(17.4) 325 .fontWeight(FontWeight.Bold) 326 .layoutWeight(1) 327 Flex() { 328 Text('Calories') 329 .fontSize(17.4) 330 .flexGrow(1) 331 Text('17kcal') 332 .fontSize(17.4) 333 } 334 .layoutWeight(2) 335 } 336 .height(280) 337 .padding({ top: 30, right: 30, left: 30 }) 338 } 339 } 340 341 @Entry 342 @Component 343 struct FoodDetail { 344 build() { 345 Column() { 346 FoodImageDisplay() 347 ContentTable() 348 } 349 .alignItems(HorizontalAlign.Center) 350 } 351 } 352``` 353 354 355  356 3574. Create the **Nutrient** class in a similar process. **Nutrition** consists of four parts: **Protein**, **Fat**, **Carbohydrates**, and **VitaminC**. The names of the last three parts are omitted in the table and represented by spaces. 358 359 Set **FlexDirection.Column**, **FlexAlign.SpaceBetween**, and **ItemAlign.Start**. 360 361``` 362@Component 363 struct ContentTable { 364 build() { 365 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 366 Flex() { 367 Text('Calories') 368 .fontSize(17.4) 369 .fontWeight(FontWeight.Bold) 370 .layoutWeight(1) 371 Flex() { 372 Text('Calories') 373 .fontSize(17.4) 374 .flexGrow(1) 375 Text('17kcal') 376 .fontSize(17.4) 377 } 378 .layoutWeight(2) 379 } 380 Flex() { 381 Text('Nutrition') 382 .fontSize(17.4) 383 .fontWeight(FontWeight.Bold) 384 .layoutWeight(1) 385 Flex() { 386 Text('Protein') 387 .fontSize(17.4) 388 .flexGrow(1) 389 Text('0.9g') 390 .fontSize(17.4) 391 } 392 .layoutWeight(2) 393 } 394 Flex() { 395 Text(' ') 396 .fontSize(17.4) 397 .fontWeight(FontWeight.Bold) 398 .layoutWeight(1) 399 Flex() { 400 Text('Fat') 401 .fontSize(17.4) 402 .flexGrow(1) 403 Text('0.2g') 404 .fontSize(17.4) 405 } 406 .layoutWeight(2) 407 } 408 Flex() { 409 Text(' ') 410 .fontSize(17.4) 411 .fontWeight(FontWeight.Bold) 412 .layoutWeight(1) 413 Flex() { 414 Text('Carbohydrates') 415 .fontSize(17.4) 416 .flexGrow(1) 417 Text('3.9g') 418 .fontSize(17.4) 419 } 420 .layoutWeight(2) 421 } 422 Flex() { 423 Text(' ') 424 .fontSize(17.4) 425 .fontWeight(FontWeight.Bold) 426 .layoutWeight(1) 427 Flex() { 428 Text('vitaminC') 429 .fontSize(17.4) 430 .flexGrow(1) 431 Text('17.8mg') 432 .fontSize(17.4) 433 } 434 .layoutWeight(2) 435 } 436 } 437 .height(280) 438 .padding({ top: 30, right: 30, left: 30 }) 439 } 440 } 441 442 @Entry 443 @Component 444 struct FoodDetail { 445 build() { 446 Column() { 447 FoodImageDisplay() 448 ContentTable() 449 } 450 .alignItems(HorizontalAlign.Center) 451 } 452 } 453``` 454 455 4565. Use the custom constructor **\@Builder** to simplify the code. It can be found that the food groups in each food composition table are actually of the same UI structure. 457 458  459 460 Currently, all food groups are declared, resulting in code duplication and redundancy. You can use **\@Builder** to build a custom method and abstract the same UI structure declaration. The **\@Builder** decorated method and the **build** method for the **@Component** decorated component are used to declare some UI rendering structures and comply with the same ArkTS syntax. You can define one or more methods decorated by **\@Builder**, but a component decorated by **@Component** can have only one **build** method. 461 462 Declare the **IngredientItem** method decorated by **\@Builder** in **ContentTable** to declare the UI descriptions for the category name, content name, and content value. 463 464``` 465 @Component 466 struct ContentTable { 467 @Builder IngredientItem(title:string, name: string, value: string) { 468 Flex() { 469 Text(title) 470 .fontSize(17.4) 471 .fontWeight(FontWeight.Bold) 472 .layoutWeight(1) 473 Flex({ alignItems: ItemAlign.Center }) { 474 Text(name) 475 .fontSize(17.4) 476 .flexGrow(1) 477 Text(value) 478 .fontSize(17.4) 479 } 480 .layoutWeight(2) 481 } 482 } 483 } 484``` 485 486 487When the **IngredientItem** API is called in the **build** method of **ContentTable**, **this** needs to be used to invoke the method in the scope of the component to distinguish the global method call. 488 489``` 490@Component 491 struct ContentTable { 492 ...... 493 build() { 494 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 495 this.IngredientItem('Calories', 'Calories', '17kcal') 496 this.IngredientItem('Nutrition', 'Protein', '0.9g') 497 this.IngredientItem('', 'Fat', '0.2g') 498 this.IngredientItem('', 'Carbohydrates', '3.9g') 499 this.IngredientItem('', 'VitaminC', '17.8mg') 500 } 501 .height(280) 502 .padding({ top: 30, right: 30, left: 30 }) 503 } 504 } 505``` 506 507 508The overall code of the **ContentTable** component is as follows: 509 510``` 511@Component 512 struct ContentTable { 513 @Builder IngredientItem(title:string, name: string, value: string) { 514 Flex() { 515 Text(title) 516 .fontSize(17.4) 517 .fontWeight(FontWeight.Bold) 518 .layoutWeight(1) 519 Flex() { 520 Text(name) 521 .fontSize(17.4) 522 .flexGrow(1) 523 Text(value) 524 .fontSize(17.4) 525 } 526 .layoutWeight(2) 527 } 528 } 529 530 build() { 531 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 532 this.IngredientItem('Calories', 'Calories', '17kcal') 533 this.IngredientItem('Nutrition', 'Protein', '0.9g') 534 this.IngredientItem('', 'Fat', '0.2g') 535 this.IngredientItem('', 'Carbohydrates', '3.9g') 536 this.IngredientItem('', 'VitaminC', '17.8mg') 537 } 538 .height(280) 539 .padding({ top: 30, right: 30, left: 30 }) 540 } 541 542 } 543 544 @Entry 545 @Component 546 struct FoodDetail { 547 build() { 548 Column() { 549 FoodImageDisplay() 550 ContentTable() 551 } 552 .alignItems(HorizontalAlign.Center) 553 } 554 } 555``` 556 557 558  559 560You've learned how to build a simple food details page. Read on to learn how to define the page layout and connection. 561