cart_provide.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_note/model/goods_detail_model.dart';
  4. import 'package:flutter_note/model/shopping_cart_model.dart';
  5. import 'package:flutter_note/utils/preference_utils.dart';
  6. class CartProvide with ChangeNotifier {
  7. bool _isAllChecked = false; // 是否全选
  8. int _allSelectedCount = 0; // 选中的物品数量
  9. int _allCartCount = 0; // 购物车内全部的物品数量
  10. double _allSelectedPrice = 0.0; // 选中物品的全部价格
  11. String _shopCartList = '[]'; // 用于持久化
  12. List<ShoppingCartModel> _shopCarts = []; // 购物车物品列表
  13. bool get allCheckedState => _isAllChecked;
  14. int get allCartCount => _allCartCount;
  15. int get allCheckedCount => _allSelectedCount;
  16. double get allCheckedPrice => _allSelectedPrice;
  17. List<ShoppingCartModel> get shopCarts => _shopCarts;
  18. CartProvide() {
  19. PreferenceUtils.instance.getString('shop_cart', '[]').then((value) {
  20. _shopCartList = value;
  21. _shopCarts.clear();
  22. _shopCarts.addAll(_shopCartList == '[]'
  23. ? []
  24. : ShoppingCartModel.fromJsonList(json.decode(_shopCartList)));
  25. _allInfoStateCheck();
  26. notifyListeners();
  27. });
  28. }
  29. /// 保存物品到购物车,可随意选择数量
  30. saveCarts(GoodInfoBean info, int count) {
  31. List<dynamic> carts =
  32. _shopCartList == '[]' ? [] : json.decode(_shopCartList);
  33. var included = false;
  34. if (carts.isNotEmpty) {
  35. carts.forEach((cart) {
  36. // 不是空列表的情况下,判断是否已经存在该物品,存在则添加,并设置状态位
  37. if (cart['goodsId'] == info.goodsId) {
  38. cart['count'] += count;
  39. included = true;
  40. }
  41. });
  42. }
  43. // 不存在该商品的时候则全部加入到列表
  44. if (!included) {
  45. carts.add({
  46. 'goodsName': info.goodsName,
  47. 'goodsId': info.goodsId,
  48. 'goodsImg': info.image1,
  49. 'orgPrice': info.oriPrice,
  50. 'price': info.presentPrice,
  51. 'count': count,
  52. 'isChecked': true,
  53. });
  54. }
  55. _notifyChanges(carts);
  56. }
  57. /// 增加/减少商品数量
  58. increaseOrReduceOperation(String goodsId, bool isIncrease) {
  59. List<dynamic> carts = json.decode(_shopCartList);
  60. // 已经存在的情况下才增加减少,修改数量值
  61. carts.forEach((cart) {
  62. if (cart['goodsId'] == goodsId) {
  63. if (isIncrease) {
  64. cart['count'] += 1;
  65. } else {
  66. cart['count'] -= 1;
  67. }
  68. }
  69. });
  70. _notifyChanges(carts);
  71. }
  72. /// 移除购物车内的某个商品
  73. removeCarts(String goodsId) {
  74. List<dynamic> carts =
  75. _shopCartList == '[]' ? [] : json.decode(_shopCartList);
  76. if (carts.isNotEmpty) {
  77. carts.removeWhere((e) => e['goodsId'] == goodsId);
  78. }
  79. _notifyChanges(carts);
  80. }
  81. /// 修改特定商品在购物车的选中状态
  82. changeCartState(String goodsId, bool checked) {
  83. List<dynamic> carts =
  84. _shopCartList == '[]' ? [] : json.decode(_shopCartList);
  85. if (carts.isNotEmpty) {
  86. carts.forEach((cart) {
  87. if (cart['goodsId'] == goodsId) {
  88. cart['isChecked'] = checked;
  89. }
  90. });
  91. }
  92. _notifyChanges(carts);
  93. }
  94. /// 全选状态修改
  95. allCheckStateChange(bool checkState) {
  96. List<dynamic> carts =
  97. _shopCartList == '[]' ? [] : json.decode(_shopCartList);
  98. if (carts.isNotEmpty) {
  99. carts.forEach((cart) {
  100. cart['isChecked'] = checkState; // 所有状态跟随全选修改
  101. });
  102. }
  103. _notifyChanges(carts);
  104. }
  105. /// 更新购物车状态封装方法
  106. _notifyChanges(List carts) {
  107. PreferenceUtils.instance.saveString('shop_cart', json.encode(carts));
  108. _shopCartList = json.encode(carts);
  109. _shopCarts.clear();
  110. _shopCarts
  111. .addAll(carts.isEmpty ? [] : ShoppingCartModel.fromJsonList(carts));
  112. _allInfoStateCheck();
  113. notifyListeners();
  114. }
  115. /// 数量,全选状态修改封装
  116. void _allInfoStateCheck() {
  117. _allCartCount = 0;
  118. _allSelectedCount = 0;
  119. _allSelectedPrice = 0.0;
  120. _isAllChecked = true;
  121. _shopCarts.forEach((e) {
  122. _allCartCount += e.count; // 全部数量
  123. if (!e.isChecked) {
  124. _isAllChecked = false; // 如果一个未选中,则全选为 false
  125. } else {
  126. _allSelectedCount += e.count;
  127. _allSelectedPrice += e.count * e.price;
  128. }
  129. });
  130. }
  131. }