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 5Before creating a page, create an ArkTS project. For details, see [Creating an ArkTS Project in Stage Model](../quick-start/start-with-ets-stage.md#creating-an-arkts-project) or [Creating an ArkTS Project in FA Model](../quick-start/start-with-ets-fa.md#creating-an-arkts-project). 6 7 8## Building the Stack Layout 9 101. Create a food name. 11 12 In the **index.ets** file, 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. 13 14 ```ts 15 @Entry 16 @Component 17 struct MyComponent { 18 build() { 19 Stack() { 20 Text('Tomato') 21 .fontSize(26) 22 .fontWeight(500) 23 } 24 } 25 } 26 ``` 27 28  29 302. Display food pictures. 31 32 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. `$rawfile` only allows the **\<Image>** component to reference image resources. 33 34 ```ts 35 @Entry 36 @Component 37 struct MyComponent { 38 build() { 39 Stack() { 40 Image($rawfile('Tomato.png')) 41 Text('Tomato') 42 .fontSize(26) 43 .fontWeight(500) 44 } 45 } 46 } 47 ``` 48 49  50 513. Access images through resources. 52 53 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). 54 55 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. 56 57 ```ts 58 @Entry 59 @Component 60 struct MyComponent { 61 build() { 62 Stack() { 63 Image($r('app.media.Tomato')) 64 .objectFit(ImageFit.Contain) 65 .height(357) 66 Text('Tomato') 67 .fontSize(26) 68 .fontWeight(500) 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 1. The width and height of the image are not set. 79 80 2. 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. 81 82 ```ts 83 @Entry 84 @Component 85 struct MyComponent { 86 build() { 87 Stack() { 88 Image($r('app.media.Tomato')) 89 .objectFit(ImageFit.Contain) 90 .height(357) 91 Text('Tomato') 92 .fontSize(26) 93 .fontWeight(500) 94 } 95 } 96 } 97 ``` 98 99  100 1015. Set the food image and name layout. Set the alignment mode of the stack to bottom alignment. By default, the stack is center aligned. Set **alignContent** to **Alignment.BottomStart**. Similar to **FontWeight**, **Alignment** is a built-in enumeration type provided by the framework. 102 103 ```ts 104 @Entry 105 @Component 106 struct MyComponent { 107 build() { 108 Stack({ alignContent: Alignment.BottomStart }) { 109 Image($r('app.media.Tomato')) 110 .objectFit(ImageFit.Contain) 111 .height(357) 112 Text('Tomato') 113 .fontSize(26) 114 .fontWeight(500) 115 } 116 } 117 } 118 ``` 119 120  121 1226. 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. 123 124 1. 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. 125 2. 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. 126 127 ```ts 128 @Entry 129 @Component 130 struct MyComponent { 131 build() { 132 Stack({ alignContent: Alignment.BottomStart }) { 133 Image($r('app.media.Tomato')) 134 .objectFit(ImageFit.Contain) 135 .height(357) 136 Text('Tomato') 137 .fontSize(26) 138 .fontWeight(500) 139 .margin({left: 26, bottom: 17.4}) 140 } 141 } 142 } 143 ``` 144 145  146 1477. 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. 148 149 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. 150 151 ```ts 152 @Component 153 struct FoodImageDisplay { 154 build() { 155 Stack({ alignContent: Alignment.BottomStart }) { 156 Image($r('app.media.Tomato')) 157 .objectFit(ImageFit.Contain) 158 Text('Tomato') 159 .fontSize(26) 160 .fontWeight(500) 161 .margin({ left: 26, bottom: 17.4 }) 162 } 163 .height(357) 164 } 165 } 166 167 @Entry 168 @Component 169 struct FoodDetail { 170 build() { 171 Column() { 172 FoodImageDisplay() 173 } 174 .alignItems(HorizontalAlign.Center) 175 } 176 } 177 ``` 178 179## Building the Flex Layout 180 181Use 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. 182 1831. Create a **ContentTable** component as a child component of the **FoodDetail** component. 184 185 ```ts 186 @Component 187 struct FoodImageDisplay { 188 build() { 189 Stack({ alignContent: Alignment.BottomStart }) { 190 Image($r('app.media.Tomato')) 191 .objectFit(ImageFit.Contain) 192 .height(357) 193 Text('Tomato') 194 .fontSize(26) 195 .fontWeight(500) 196 .margin({ left: 26, bottom: 17.4 }) 197 } 198 } 199 } 200 201 @Component 202 struct ContentTable { 203 build() { 204 } 205 } 206 207 @Entry 208 @Component 209 struct FoodDetail { 210 build() { 211 Column() { 212 FoodImageDisplay() 213 ContentTable() 214 } 215 .alignItems(HorizontalAlign.Center) 216 } 217 } 218 ``` 219 2202. Create a **\<Flex>** component to display two food composition categories in the tomato: **Calories** and **Nutrition**. 221 222 **Calories** contains information about calories. **Nutrition** contains information about protein, fat, carbohydrates, and vitamin C. 223 224 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. 225 226 In the following example, code of **FoodImageDisplay** is omitted, and only code of **ContentTable** is provided. 227 228 ```ts 229 @Component 230 struct ContentTable { 231 build() { 232 Flex() { 233 Text('Calories') 234 .fontSize(17.4) 235 .fontWeight(FontWeight.Bold) 236 Text('Calories') 237 .fontSize(17.4) 238 Text('17kcal') 239 .fontSize(17.4) 240 } 241 .height(280) 242 .padding({ top: 30, right: 30, left: 30 }) 243 } 244 } 245 246 @Entry 247 @Component 248 struct FoodDetail { 249 build() { 250 Column() { 251 FoodImageDisplay() 252 ContentTable() 253 } 254 .alignItems(HorizontalAlign.Center) 255 } 256 } 257 ``` 258 259 260  261 2623. 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)**. 263 264 ```ts 265 @Component 266 struct FoodImageDisplay { 267 build() { 268 Stack({ alignContent: Alignment.BottomStart }) { 269 Image($r('app.media.Tomato')) 270 .objectFit(ImageFit.Contain) 271 .height(357) 272 Text('Tomato') 273 .fontSize(26) 274 .fontWeight(500) 275 .margin({ left: 26, bottom: 17.4 }) 276 } 277 } 278 } 279 280 @Component 281 struct ContentTable { 282 build() { 283 Flex() { 284 Text('Calories') 285 .fontSize(17.4) 286 .fontWeight(FontWeight.Bold) 287 .layoutWeight(1) 288 Flex() { 289 Text('Calories') 290 .fontSize(17.4) 291 .flexGrow(1) 292 Text('17kcal') 293 .fontSize(17.4) 294 } 295 .layoutWeight(2) 296 } 297 .height(280) 298 .padding({ top: 30, right: 30, left: 30 }) 299 } 300 } 301 302 @Entry 303 @Component 304 struct FoodDetail { 305 build() { 306 Column() { 307 FoodImageDisplay() 308 ContentTable() 309 } 310 .alignItems(HorizontalAlign.Center) 311 } 312 } 313 ``` 314 315  316 3174. 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. 318 319 Set **FlexDirection.Column**, **FlexAlign.SpaceBetween**, and **ItemAlign.Start**. 320 321 ```ts 322 @Component 323 struct ContentTable { 324 build() { 325 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 326 Flex() { 327 Text('Calories') 328 .fontSize(17.4) 329 .fontWeight(FontWeight.Bold) 330 .layoutWeight(1) 331 Flex() { 332 Text('Calories') 333 .fontSize(17.4) 334 .flexGrow(1) 335 Text('17kcal') 336 .fontSize(17.4) 337 } 338 .layoutWeight(2) 339 } 340 Flex() { 341 Text('Nutrition') 342 .fontSize(17.4) 343 .fontWeight(FontWeight.Bold) 344 .layoutWeight(1) 345 Flex() { 346 Text('Protein') 347 .fontSize(17.4) 348 .flexGrow(1) 349 Text('0.9g') 350 .fontSize(17.4) 351 } 352 .layoutWeight(2) 353 } 354 Flex() { 355 Text(' ') 356 .fontSize(17.4) 357 .fontWeight(FontWeight.Bold) 358 .layoutWeight(1) 359 Flex() { 360 Text('Fat') 361 .fontSize(17.4) 362 .flexGrow(1) 363 Text('0.2g') 364 .fontSize(17.4) 365 } 366 .layoutWeight(2) 367 } 368 Flex() { 369 Text(' ') 370 .fontSize(17.4) 371 .fontWeight(FontWeight.Bold) 372 .layoutWeight(1) 373 Flex() { 374 Text('Carbohydrates') 375 .fontSize(17.4) 376 .flexGrow(1) 377 Text('3.9g') 378 .fontSize(17.4) 379 } 380 .layoutWeight(2) 381 } 382 Flex() { 383 Text(' ') 384 .fontSize(17.4) 385 .fontWeight(FontWeight.Bold) 386 .layoutWeight(1) 387 Flex() { 388 Text('vitaminC') 389 .fontSize(17.4) 390 .flexGrow(1) 391 Text('17.8mg') 392 .fontSize(17.4) 393 } 394 .layoutWeight(2) 395 } 396 } 397 .height(280) 398 .padding({ top: 30, right: 30, left: 30 }) 399 } 400 } 401 402 @Entry 403 @Component 404 struct FoodDetail { 405 build() { 406 Column() { 407 FoodImageDisplay() 408 ContentTable() 409 } 410 .alignItems(HorizontalAlign.Center) 411 } 412 } 413 ``` 414 415  416 4175. 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. 418 419  420 421 422Currently, 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. 423 424Declare the **IngredientItem** method decorated by **\@Builder** in **ContentTable** to declare the UI descriptions for the category name, content name, and content value. 425 426 ```ts 427 @Component 428 struct ContentTable { 429 @Builder IngredientItem(title:string, name: string, value: string) { 430 Flex() { 431 Text(title) 432 .fontSize(17.4) 433 .fontWeight(FontWeight.Bold) 434 .layoutWeight(1) 435 Flex({ alignItems: ItemAlign.Center }) { 436 Text(name) 437 .fontSize(17.4) 438 .flexGrow(1) 439 Text(value) 440 .fontSize(17.4) 441 } 442 .layoutWeight(2) 443 } 444 } 445 } 446 ``` 447 448When 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. 449 450 ```ts 451 @Component 452 struct ContentTable { 453 ...... 454 build() { 455 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 456 this.IngredientItem('Calories', 'Calories', '17kcal') 457 this.IngredientItem('Nutrition', 'Protein', '0.9g') 458 this.IngredientItem('', 'Fat', '0.2g') 459 this.IngredientItem('', 'Carbohydrates', '3.9g') 460 this.IngredientItem('', 'VitaminC', '17.8mg') 461 } 462 .height(280) 463 .padding({ top: 30, right: 30, left: 30 }) 464 } 465 } 466 ``` 467 468The overall code of the **ContentTable** component is as follows: 469 470 ```ts 471 @Component 472 struct ContentTable { 473 @Builder IngredientItem(title:string, name: string, value: string) { 474 Flex() { 475 Text(title) 476 .fontSize(17.4) 477 .fontWeight(FontWeight.Bold) 478 .layoutWeight(1) 479 Flex() { 480 Text(name) 481 .fontSize(17.4) 482 .flexGrow(1) 483 Text(value) 484 .fontSize(17.4) 485 } 486 .layoutWeight(2) 487 } 488 } 489 490 build() { 491 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 492 this.IngredientItem('Calories', 'Calories', '17kcal') 493 this.IngredientItem('Nutrition', 'Protein', '0.9g') 494 this.IngredientItem('', 'Fat', '0.2g') 495 this.IngredientItem('', 'Carbohydrates', '3.9g') 496 this.IngredientItem('', 'VitaminC', '17.8mg') 497 } 498 .height(280) 499 .padding({ top: 30, right: 30, left: 30 }) 500 } 501 } 502 503 @Entry 504 @Component 505 struct FoodDetail { 506 build() { 507 Column() { 508 FoodImageDisplay() 509 ContentTable() 510 } 511 .alignItems(HorizontalAlign.Center) 512 } 513 } 514 ``` 515 516  517 518You've learned how to build a simple food details page. Read on to learn how to define the page layout and connection. 519