利用鸿蒙HarmonyOs NEXT 开发 快速开发 简朴的购物车页面 ...

鼠扑  论坛元老 | 2024-12-30 19:21:33 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1053|帖子 1053|积分 3159

目次

资源准备:需要准备三张照片:商品图、向下图标、款项图标
1.表现效果:
2.源码:



资源准备:需要准备三张照片:商品图、向下图标、款项图标

1.表现效果:




定义了一个购物车页面的结构,包括以下几个部分:
1. 每个商品都有一个复选框来表现选择状态,一个图片来展示商品,以及商品形貌、规格、标签和价格。
2. 用户可以通过点击减号或加号来调解商品数量。
3. 表现已选商品数量和总金额,以及一个结算按钮。
在每个商品行中,当用户点击复选框时,会更新商品的选择状态、总金额和总选中商品数量。当用户调解商品数量时,也会相应地更新总金额。
在结算行中,表现了用户已选择的商品数量和总金额。
请留意,这个代码示例是一个简化的版本,实际应用中可能需要更多的逻辑来处理全选功能、商品数量的限定、价格盘算等。此外,您可能还需要与后端服务交互来更新购物车的状态。
2.源码:

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State pantValue:number = 69.98 // 商品单价
  5.   @State amount:number = 0 // 总金额
  6.   @State count:number = 1 // 商品数量
  7.   @State selectPant:boolean = false // 商品是否选中
  8.   @State totalCount:number = 0 // 总选中商品数量
  9.   build() {
  10.     // 整个页面的垂直布局
  11.     Column(){
  12.       // 商品行的布局
  13.       Row({space:5}){
  14.         // 商品选择复选框
  15.         Checkbox()
  16.           .width(15)
  17.           .onClick(()=>{
  18.             // 当复选框点击时,更新金额和选中状态
  19.             if(!this.selectPant){
  20.               this.amount += this.count * this.pantValue
  21.               this.selectPant = true
  22.               this.totalCount +=1
  23.             }else{
  24.               this.amount -= this.count * this.pantValue
  25.               this.selectPant = false
  26.               this.totalCount -=1
  27.             }
  28.           })
  29.         // 商品图片的布局
  30.         Column(){
  31.           Image($r('app.media.shop_pant'))
  32.             .width(80)
  33.             .borderRadius(10)
  34.             .backgroundColor(Color.Brown)
  35.         }.height('100%')
  36.         // 商品描述的布局
  37.         Column(){
  38.           // 商品名称
  39.           Row(){
  40.             Text(){
  41.               Span('开学季 ')
  42.                 .fontColor(Color.Red)
  43.                 .fontSize(20)
  44.               Span('三条杠运动卫裤男春秋百搭宽松裤')
  45.             }
  46.             .textOverflow({overflow:TextOverflow.Clip})
  47.             .maxLines(1)
  48.           }
  49.           // 商品规格
  50.           Text('S668黑色,XL[码(对应32-33)]')
  51.             .textOverflow({overflow:TextOverflow.Clip})
  52.             .maxLines(1)
  53.           // 商品标签
  54.           Flex({
  55.             direction:FlexDirection.Row,
  56.             wrap:FlexWrap.Wrap
  57.           }){
  58.             Text('每200减20')
  59.               .margin({right:5})
  60.               .fontColor('#fff16706')
  61.             Text('假一赔四')
  62.               .fontColor('#fff16706')
  63.             Text('极速退款')
  64.               .fontColor('#fff16706')
  65.           }
  66.           .padding(3)
  67.           .width('100%')
  68.           // 商品价格
  69.           Row({space:5}){
  70.             Image($r('app.media.money'))
  71.               .width(16)
  72.               .fillColor('#e6f51905')
  73.             Text(){
  74.               Span(this.pantValue.toFixed(2).split('.')[0].toString())
  75.                 .fontSize(24)
  76.               Span('.')
  77.                 .fontSize(24)
  78.               Span(this.pantValue.toFixed(2).split('.')[1].toString())
  79.                 .fontSize(18)
  80.             }
  81.             .fontColor('#e6f51905')
  82.           }.width('100%')
  83.         }.layoutWeight(1)
  84.         .height('100%')
  85.         // 商品数量调整的布局
  86.         Column(){
  87.           Row({space:5}){
  88.             // 减少商品数量的按钮
  89.             Text('-')
  90.               .fontSize(25)
  91.               .border({
  92.                 width:{top:1,left:1,bottom:1},
  93.                 style:BorderStyle.Dotted
  94.               })
  95.               .borderRadius({
  96.                 topLeft:10,
  97.                 bottomLeft:10
  98.               }).padding({left:3,right:3})
  99.               .onClick(()=>{
  100.                 // 减少商品数量,并更新金额
  101.                 if(this.count >1){
  102.                   this.count -= 1
  103.                   if (this.selectPant) {
  104.                     this.amount -= this.pantValue
  105.                   }
  106.                 }else{
  107.                   AlertDialog.show({message:'商品数量至少为1哦!'})
  108.                 }
  109.               })
  110.             // 显示商品数量的文本
  111.             Text(this.count.toString())
  112.               .fontSize(25)
  113.               .border({
  114.                 width:1,
  115.                 style:BorderStyle.Dotted
  116.               }).padding({left:3,right:3})
  117.             // 增加商品数量的按钮
  118.             Text('+')
  119.               .fontSize(25)
  120.               .border({
  121.                 width:{top:1,right:1,bottom:1},
  122.                 style:BorderStyle.Dotted
  123.               })
  124.               .borderRadius({
  125.                 topRight:10,
  126.                 bottomRight:10
  127.               })
  128.               .padding({left:3,right:3})
  129.               .onClick(()=>{
  130.                 // 增加商品数量,并更新金额
  131.                 this.count += 1
  132.                 if (this.selectPant) {
  133.                   this.amount += this.pantValue
  134.                 }
  135.               })
  136.           }
  137.           // 下拉箭头图标
  138.           Image($r('app.media.ic_public_arrow_down_0'))
  139.             .width(20)
  140.         }
  141.         .height('100%')
  142.         .alignItems(HorizontalAlign.Start)
  143.       }
  144.       .height(130)
  145.       .padding(5)
  146.       .width('100%')
  147.       .backgroundColor(Color.White)
  148.       // 占位符,用于在布局中创建空间
  149.       Blank()
  150.       // 结算行的布局
  151.       Row(){
  152.         // 全选复选框和文本
  153.         Row({space:5}){
  154.           Checkbox()
  155.             .width(14)
  156.           Text('全选')
  157.             .fontSize(16)
  158.         }
  159.         // 占位符,用于在布局中创建空间
  160.         Blank()
  161.         // 结算信息布局
  162.         Row(){
  163.           // 显示已选商品数量和总金额
  164.           Text('已选'+this.totalCount+'件 ')
  165.             .fontColor(Color.Gray)
  166.             .fontSize(14)
  167.           Text('合计: ')
  168.             .fontSize(14)
  169.           Image($r('app.media.money'))
  170.             .width(12)
  171.             .fillColor('#e6f51905')
  172.           Text(){
  173.             Span(this.amount.toFixed(2).split('.')[0].toString())
  174.               .fontSize(26)
  175.             Span('.')
  176.               .fontSize(26)
  177.             Span(this.amount.toFixed(2).split('.')[1].toString())
  178.               .fontSize(18)
  179.           }
  180.           .fontColor('#e6f51905')
  181.         }.margin({left:10})
  182.         // 结算按钮
  183.         Button('结算')
  184.           .width(100)
  185.           .height(50)
  186.           .backgroundColor('#ffff4801')
  187.           .margin({left:10})
  188.       }
  189.       .padding(10)
  190.       .height(100)
  191.       .width('100%')
  192.       .backgroundColor(Color.White)
  193.       .borderRadius({
  194.         topLeft:25,
  195.         topRight:25
  196.       })
  197.     }
  198.     .height('100%')
  199.     .width('100%')
  200.     .backgroundColor('#fff3f1f1')
  201.   }
  202. }
复制代码




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表