• 使用鸿蒙HarmonyOs NEXT 开发 快速开发 简单的购物车页面


    目录

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

    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. }

  • 相关阅读:
    IEEE会议论文LaTeX模板中添加页码
    50天50个前端小项目(纯html+css+js)第八天(形成波浪动画结合登录表单)
    【附源码】Python计算机毕业设计特大城市地铁站卫生防疫系统
    L44.linux命令每日一练 -- 第七章 Linux用户管理及用户信息查询命令 -- su和visudo
    德鲁伊(Druid)后台监控配置详细操作,别再怕找不到疑难杂症
    Sobel算子详解及例程
    android Compose Text文本
    通俗的B树入门 及 查找、插入、删除操作
    异步方法、async/await逃离回调地狱(Callback Hell)
    Vue常见问题
  • 原文地址:https://blog.csdn.net/qq_69183322/article/details/139937149