shopping_cart_list.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_note/model/shopping_cart_model.dart';
  4. import 'package:flutter_note/views/cartpage/cart_count.dart';
  5. import 'package:flutter_note/provide/cart_provide.dart';
  6. class ShoppingCartList extends StatelessWidget {
  7. final CartProvide cartProvide;
  8. ShoppingCartList({Key key, this.cartProvide}) : super(key: key);
  9. /// 列表小计
  10. Widget _cartSummaryItem() {
  11. return Container(
  12. color: Colors.white,
  13. padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
  14. child: Row(
  15. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  16. children: [
  17. Text(
  18. '共${cartProvide.allCheckedCount}件商品',
  19. style: TextStyle(color: Colors.black),
  20. ),
  21. Text(
  22. '小计:¥${cartProvide.allCheckedPrice}',
  23. style: TextStyle(color: Colors.red[700]),
  24. )
  25. ],
  26. ),
  27. );
  28. }
  29. /// 前置部分,checkbox image
  30. Widget _leadingPart(ShoppingCartModel entity) {
  31. return Row(
  32. children: [
  33. Checkbox(
  34. value: entity.isChecked,
  35. onChanged: (checkState) {
  36. // 修改商品选择状态
  37. cartProvide.changeCartState(entity.goodsId, checkState);
  38. },
  39. activeColor: Colors.pink),
  40. // 商品图标
  41. DecoratedBox(
  42. decoration: ShapeDecoration(
  43. shape: RoundedRectangleBorder(), color: Colors.black12),
  44. child: Padding(
  45. padding: const EdgeInsets.all(1.0),
  46. child: Image.network(entity.goodsImg, height: 80.0, width: 80.0),
  47. ),
  48. )
  49. ],
  50. );
  51. }
  52. /// 中间部分,商品名 计数器
  53. Widget _middlePart(ShoppingCartModel entity) {
  54. return Expanded(
  55. child: Container(
  56. height: 80.0, // 该部件同图片同高
  57. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  58. child: Column(
  59. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  60. crossAxisAlignment: CrossAxisAlignment.start,
  61. children: [
  62. Text(
  63. entity.goodsName,
  64. style: TextStyle(color: Colors.black, fontSize: 16.0),
  65. maxLines: 1,
  66. overflow: TextOverflow.ellipsis,
  67. ),
  68. // 数量管理器
  69. CartCountWidget(
  70. count: entity.count,
  71. goodsId: entity.goodsId,
  72. cartProvide: cartProvide,
  73. )
  74. ],
  75. ),
  76. ),
  77. );
  78. }
  79. /// 尾部价格部分
  80. Widget _trailingPart(ShoppingCartModel entity) {
  81. return Column(
  82. crossAxisAlignment: CrossAxisAlignment.end,
  83. children: [
  84. Text(
  85. '¥${(entity.count * entity.price).toStringAsFixed(2)}',
  86. style: TextStyle(color: Colors.black, fontSize: 16.0),
  87. ),
  88. Text(
  89. '¥${(entity.count * entity.orgPrice).toStringAsFixed(2)}',
  90. style: TextStyle(
  91. color: Colors.black54,
  92. fontSize: 14.0,
  93. decoration: TextDecoration.lineThrough),
  94. ),
  95. InkWell(
  96. child: Icon(CupertinoIcons.delete, size: 32.0),
  97. onTap: () {
  98. cartProvide.removeCarts(entity.goodsId);
  99. })
  100. ],
  101. );
  102. }
  103. @override
  104. Widget build(BuildContext context) {
  105. return ListView.separated(
  106. itemBuilder: (_, index) => index == cartProvide.shopCarts.length
  107. ? _cartSummaryItem()
  108. : Container(
  109. color: Colors.white,
  110. padding: const EdgeInsets.all(12.0),
  111. child: Row(
  112. children: [
  113. _leadingPart(cartProvide.shopCarts[index]),
  114. _middlePart(cartProvide.shopCarts[index]),
  115. _trailingPart(cartProvide.shopCarts[index])
  116. ],
  117. ),
  118. ),
  119. separatorBuilder: (_, index) =>
  120. Divider(height: 1.0, color: Colors.black26),
  121. itemCount: cartProvide.shopCarts.length + 1,
  122. );
  123. }
  124. }