加载中...

下厨房


界面展示图片

*核心步骤:*

  1. 首页:

    绑定点击事件

​ 点击时跳转页面,并携带数据

​ 携带的内容结合 【详情页】确认

  1. 详情页:

​ 接收数据

​ 渲染对应页面结构

利用router可实现页面间的跳转

界面展示图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// -----------传递参数------------
// 普通跳转 并 传递参数
router.pushUrl({
url:'地址',
params:{
// 以对象的形式传递参数
}
})

// 返回并传递参数
router.back(
url:'地址',
params:{
// 以对象的形式传递参数
}
)

// -------------页面 B接收并解析参数------------
// aboutToAppear一会展开 (生命周期函数)
aboutToAppear(): void {
// 1.确认内容
console.log(JSON.stringify(router.getParams()))
// 2.通过 as 类型断言 转为具体的类型
const params = router.getParams() as 类型
// 3. 通过点语法即可取值
params.xxx
}
界面展示图片

下厨房首页

界面展示图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Category, CategoryContainer } from '..//data/CategoryData'
import { router } from '@kit.ArkUI'


@Entry
@Component
struct XCFDemo {
@State list: CategoryContainer[] = CategoryContainer.getCategoryContainerList()

build() {
Column() {
// 顶部
TopCom()
// 内容
ContentCom({list:this.list})
.layoutWeight(1)

// 底部
BottomCom()

}
.width('100%')
.height('100%')
}
}

// 顶部组件:图片代替
@Component
struct TopCom {
build() {
Image($r('app.media.ic_public_XCF_TOP'))
.width('100%')
}
}

// 内容组件:渲染列表
@Component
struct ContentCom {
@Prop list:CategoryContainer[]
build() {
Scroll() {
Column({ space: 30 }) {
ForEach(this.list, (item: CategoryContainer) => {
Column({ space: 15 }) {
Text(item.title)
.fontSize(20)
.alignSelf(ItemAlign.Start)
.fontWeight(FontWeight.Medium)
Grid() {
ForEach(item.categorizes, (cateItem: Category) => {
GridItem() {
Stack({ alignContent: Alignment.Bottom }) {
Image(cateItem.cover)
Text(cateItem.title)
.fontColor(Color.White)
.fontSize(14)
.padding({ bottom: 10 })
}
.borderRadius(10)
.clip(true)
.onClick(()=>{
router.pushUrl({
//写/有提示
url:'pages/XCFDetailPage',
params:{
title:cateItem.title,
id:cateItem.id
}
})
})
}
})
}
.columnsTemplate('1fr 1fr 1fr')
.columnsGap(10)
.rowsGap(10)
}
.height(240)
})
}
.padding(20)
}
.scrollBar(BarState.Off)
.edgeEffect(EdgeEffect.Spring)

}
}

// 底部组件:图片代替
@Component
struct BottomCom {
build() {
Image($r('app.media.ic_public_XCF_Bottom'))
.width('100%')
}
}

下厨房详情页

首页点击跳转至对应详情页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Food, FoodList } from '../data/FoodData';
import { router } from '@kit.ArkUI';

interface ParamType {
title: string
id: string
}

@Entry
@Component
struct CategoryPage {
// 渲染食物列表
foods: Food[] = []
@State cover: string = '' //图片素材
@State cateTitle: string = ''
@State arr: Food[] = [] //筛选出来的食物清单

aboutToAppear(): void {
// 接收路由传递而来的数据
// console.log(JSON.stringify(router.getParams()))
const Params = router.getParams() as ParamType
const arr = FoodList.filter((item: Food) => {
if (item.categoryId == Params.id) {
return true
} else {
return false
}
})
//console.log('',JSON.stringify(arr))
this.cover = arr[0].cover // 找到素材中第一个图片
//console.log(this.cover)
this.cateTitle = Params.title

this.arr = arr
}

build() {
Column() {
// 顶部
TopCom({ cover: this.cover, cateTitle: this.cateTitle })

// 列表
List({ space: 20 }) {
ForEach(this.arr, (food: Food) => {
ListItem() {
Row() {
Image(food.cover)
.width(150)
.height(110)
.borderRadius(10)
Column() {
Text(food.title)
Text() {
Span(`评分:${food.score} `)
Span(`${food.count} 人做过`)
}
.fontSize(12)

Row({ space: 10 }) {
// 作者头像
Image(food.author.avatar)
.width(20)
.height(20)
.borderRadius(10)
// 作者昵称
Text(food.author.nickname)
.fontSize(10)
.fontColor(Color.Gray)
}
}
.alignItems(HorizontalAlign.Start)
.justifyContent(FlexAlign.SpaceBetween)
.layoutWeight(1)
.padding({ top: 5, bottom: 5, left: 10 })
.height(110)
}
}
})
}
.padding(20)
.layoutWeight(1)
}
.height('100%')
}
}

@Component
struct TopCom {
@Prop cover: string
@Prop cateTitle: string

build() {
Stack() {
// 顶部封面
Image(this.cover)
.height('100%')
.width('100%')
Column() {
// 返回按钮
Image($r('app.media.ic_public_arrow_left'))
.fillColor(Color.White)
.width(30)
.onClick(() => {
router.back()
})

// 顶部标题
Text(this.cateTitle)
.fontColor(Color.White)
.fontSize(30)
}
.justifyContent(FlexAlign.SpaceAround)
.alignItems(HorizontalAlign.Start)
.padding(20)
.backgroundColor('rgba(0,0,0,0.4)')
.width('100%')
.height('100%')
}
.height(220)
}
}

文章作者: 太阳神小赖
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 太阳神小赖 !
  目录