自定义页面组件,简化代码结构,只适用当前页面
1@Builder 2searchBox() { 3 Column() { 4 Flex({ 5 justifyContent: FlexAlign.SpaceBetween 6 }) { 7 Image($r('app.media.startIcon')) 8 .width(28) 9 Image($r('app.media.startIcon')) 10 .width(28) 11 } 12 .height(30) 13 .width('100%') 14 .margin({ 15 bottom: 8 16 }) 17 18 // 搜索框设计 19 Row() { 20 Image($r('app.media.startIcon')) 21 .width(20) 22 .margin({ 23 left: 12 24 }) 25 } 26 .height(40) 27 .width('100%') 28 .backgroundColor(Color.White) 29 .borderRadius(20) 30 } 31} 32 33// 搜索模块 34this.searchBox()
单个页面可以有多个组件使用
1@Builder 2function searchBox() { 3 Column() { 4 Flex({ 5 justifyContent: FlexAlign.SpaceBetween 6 }) { 7 Image($r('app.media.startIcon')) 8 .width(28) 9 Image($r('app.media.startIcon')) 10 .width(28) 11 } 12 .height(30) 13 .width('100%') 14 .margin({ 15 bottom: 8 16 }) 17 18 // 搜索框设计 19 Row() { 20 Image($r('app.media.startIcon')) 21 .width(20) 22 .margin({ 23 left: 12 24 }) 25 } 26 .height(40) 27 .width('100%') 28 .backgroundColor(Color.White) 29 .borderRadius(20) 30 } 31} 32 33// 搜索模块 34searchBox()
没法动态刷新,能请求到数据
1import { EmptyView } from "../components/EmptyView" 2import { BreakPointConstants } from "../constants/BreakPointConstants" 3import { ActivityDataModel, ProductDataModel, SwiperDataModel } from '../viewModel/HomeModel' 4import { InsightIntentUIExtensionAbility } from "@kit.IntentsKit" 5import { myRequest, myRequest2, myRequest3, myRequestGet } from "../utils/HttpUtils" 6import { JSON } from "@kit.ArkTS" 7import { http } from "@kit.NetworkKit" 8import { findDataByGetApi } from "../apis/ProductApi" 9import { ParamsType } from "../utils/ParamsType" 10 11@Builder 12function searchBox() { 13 Column() { 14 Flex({ 15 justifyContent: FlexAlign.SpaceBetween 16 }) { 17 Image($r('app.media.startIcon')) 18 .width(28) 19 Image($r('app.media.startIcon')) 20 .width(28) 21 } 22 .height(30) 23 .width('100%') 24 .margin({ 25 bottom: 8 26 }) 27 28 // 搜索框设计 29 Row() { 30 Image($r('app.media.startIcon')) 31 .width(20) 32 .margin({ 33 left: 12 34 }) 35 } 36 .height(40) 37 .width('100%') 38 .backgroundColor(Color.White) 39 .borderRadius(20) 40 } 41} 42 43@Builder 44function swiperBox(currentBreakPoint: string, arr: Array<SwiperDataModel>) { 45 Swiper() { 46 ForEach(arr, (item: SwiperDataModel, index: number) => { 47 Image($r(item.image_src)) 48 .width('100%') 49 .height(160) 50 }, (item: SwiperDataModel) => item.id) 51 } 52 .indicator(true) 53 .itemSpace(currentBreakPoint == BreakPointConstants.BREAKPOINT_LG ? 10 : 0) 54 .width('100%') 55 .autoPlay(true) 56 .borderRadius(16) 57 .margin({ 58 top: 20 59 }) 60 .displayCount(currentBreakPoint == BreakPointConstants.BREAKPOINT_LG ? 2 : 1) 61} 62 63@Builder 64function navBox(activityTitle: Array<ActivityDataModel>) { 65 Grid() { 66 ForEach(activityTitle, (item: ActivityDataModel, index: number) => { 67 GridItem() { 68 Column() { 69 Image($r(item.image_src)) 70 .width(60) 71 Text(item.name) 72 } 73 } 74 }, (item: ActivityDataModel) => item.id) 75 } 76 .rowsTemplate('1fr 1fr') 77 .columnsTemplate('1fr 1fr 1fr 1fr') 78 .rowsGap(10) 79 .columnsGap(10) 80 .width('100%') 81 .height(180) 82 .backgroundColor(Color.White) 83 .borderRadius(10) 84 .margin({ 85 top: 10 86 }) 87} 88 89@Builder 90function productBox(productData: Array<ProductDataModel>) { 91 if (productData.length > 0) { 92 List({ space: 10 }) { 93 ForEach(productData, (item: ProductDataModel, index: number) => { 94 ListItem() { 95 Column() { 96 Image(item.good_big_logo != "" ? $r(item.good_big_logo) : $r('app.media.bird')) 97 .width('100%') 98 .height(130) 99 Column() { 100 Text(item.good_name) 101 .width('100%') 102 .height(30) 103 .fontSize(14) 104 Row() { 105 Text(`¥${item.good_price}`) 106 .width(60) 107 .fontSize(18) 108 .fontColor(Color.Brown) 109 Image($r('app.media.startIcon')) 110 .width(30) 111 } 112 .width('100%') 113 .justifyContent(FlexAlign.SpaceBetween) 114 } 115 .padding({ 116 left: 5, 117 right: 5 118 }) 119 } 120 .width('96%') 121 .height(218) 122 .backgroundColor(Color.Pink) 123 .borderRadius(10) 124 } 125 }, (item: ProductDataModel) => item.id) 126 } 127 .lanes(2) 128 .margin({ 129 top: 10 130 }) 131 .padding({ 132 bottom: 38 133 }) 134 } else { 135 EmptyView({ defaultHeight: '100vp' }) 136 } 137} 138 139 140@Component 141export struct Home { 142 @StorageProp(BreakPointConstants.CURRENT_BREAKPOINT) currentBreakPoint: string = BreakPointConstants.BREAKPOINT_SM 143 @State activityTitle: Array<string> = ['1', '2', '3', '4', '5', '6', '7', '8'] 144 @State activityData: Array<ActivityDataModel> = [ { id: '1', name: '旅行', image_src: 'app.media.startIcon', navigator_url: '', open_type: '' }, { id: '2', name: '美食', image_src: 'app.media.startIcon', navigator_url: '', open_type: '' }, { id: '3', name: '周边', image_src: 'app.media.startIcon', navigator_url: '', open_type: '' }, { id: '4', name: '电子', image_src: 'app.media.startIcon', navigator_url: '', open_type: '' }, { id: '5', name: '话费', image_src: 'app.media.startIcon', navigator_url: '', open_type: '' } ] 145 @State swiperDataList: Array<SwiperDataModel> = [ { id: '1', good_id: 'p1', image_src: 'app.media.bird', navigator_url: '', open_type: '' }, { id: '2', good_id: 'p2', image_src: 'app.media.animal', navigator_url: '', open_type: '' }, { id: '3', good_id: 'p3', image_src: 'app.media.bird', navigator_url: '', open_type: '' }, { id: '4', good_id: 'p4', image_src: 'app.media.animal', navigator_url: '', open_type: '' }, { id: '5', good_id: 'p5', image_src: 'app.media.bird', navigator_url: '', open_type: '' } ] 146 @State productData: Array<ProductDataModel> = [ { id: '1', add_time: '', cat_id: '', cat_one_id: '', cat_two_id: '', cat_three_id: '', good_small_logo: '', good_big_logo: '', good_name: '无此商品', good_number: '', good_price: '0', good_weight: '', hot_number: '', is_promote: '', upd_time: '' }, { id: '2', add_time: '', cat_id: '', cat_one_id: '', cat_two_id: '', cat_three_id: '', good_small_logo: '', good_big_logo: 'app.media.animal', good_name: '创维电视机', good_number: '', good_price: '5000', good_weight: '', hot_number: '', is_promote: '', upd_time: '' }, { id: '3', add_time: '', cat_id: '', cat_one_id: '', cat_two_id: '', cat_three_id: '', good_small_logo: '', good_big_logo: 'app.media.bird', good_name: 'VIVO手机', good_number: '', good_price: '3000', good_weight: '', hot_number: '', is_promote: '', upd_time: '' }, { id: '4', add_time: '', cat_id: '', cat_one_id: '', cat_two_id: '', cat_three_id: '', good_small_logo: '', good_big_logo: 'app.media.animal', good_name: 'OPPO手机', good_number: '', good_price: '1000', good_weight: '', hot_number: '', is_promote: '', upd_time: '' }] 147 148 aboutToAppear(): void { 149 this.fetchSwiperData() 150 this.fetchProductData() 151 this.fetchActivityData() 152 } 153 154 fetchSwiperData = async () => { 155 await findDataByGetApi<Array<SwiperDataModel>>(1).then(res => { 156 this.swiperDataList = res 157 }) 158 } 159 fetchProductData = async () => { 160 await findDataByGetApi<Array<ProductDataModel>>(2).then(res => { 161 this.productData = res 162 }) 163 } 164 fetchActivityData = async () => { 165 await findDataByGetApi<Array<ActivityDataModel>>(2).then(res => { 166 this.activityData = res 167 }) 168 } 169 170 build() { 171 Column() { 172 Stack({ 173 alignContent: Alignment.Top 174 }) { 175 // 背景图 176 Image($r('app.media.background')) 177 .width('100%') 178 .height(220) 179 .borderRadius({ 180 bottomLeft: 30, 181 bottomRight: 30 182 }) 183 .objectFit(ImageFit.Auto) 184 185 // 弹性盒子, 内容布局 186 Column() { 187 // 搜索模块 188 searchBox() 189 // 滚动区域 190 Scroll() { 191 Column() { 192 // 轮播图片 193 swiperBox(this.currentBreakPoint, this.swiperDataList) 194 // 楼层模块 195 navBox(this.activityData) 196 // 商品列表 197 productBox(this.productData) 198 } 199 } 200 .scrollBar(BarState.Off) 201 .height(580) 202 .margin({ 203 top: 10, 204 bottom: 20 205 }) 206 } 207 .padding({ 208 left: 12, 209 right: 12, 210 top: 10 211 }) 212 } 213 } 214 .backgroundColor('#f1f3f5') 215 .width('100%') 216 .height('100%') 217 .justifyContent(FlexAlign.Start) 218 .alignItems(HorizontalAlign.Start) 219 } 220}
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语