diff --git a/lib/store/store_order.dart b/lib/store/store_order.dart index 4dadfc0f..e2b0d76c 100644 --- a/lib/store/store_order.dart +++ b/lib/store/store_order.dart @@ -53,7 +53,6 @@ class _StoreOrderPage extends State double allPrice = 0; StoreOrderListPage storeOrderListPage; - List productLists = []; List> shopCarGoods = []; ScrollController controller = ScrollController(); @@ -310,6 +309,7 @@ class _StoreOrderPage extends State shopCarGoods, controller, _queryMiNiDetail, + _productListResult, _fc, ), @@ -450,19 +450,25 @@ class _StoreOrderPage extends State ); } + List appletProducts = []; + + _productListResult(List appletProducts) { + this.appletProducts = appletProducts; + } + ///购物车弹窗 showShoppingCart() { - + if (appletProducts == null || appletProducts.length == 0) return; List shopCar = []; - if (shopCarGoods != null && shopCarGoods.length > 0) { shopCarGoods.forEach((element) { - ProductListBean productListBean = productLists.firstWhere((element1) { - return element["id"] == element1.id; + appletProducts.forEach((element1) { + element1.productList.forEach((element2) { + if (element["id"] == element2.id) { + shopCar.add(element2); + } + }); }); - if (productListBean != null) { - shopCar.add(productListBean); - } }); } @@ -473,11 +479,8 @@ class _StoreOrderPage extends State return ShopCar( shopCar, shopCarGoods, + clearShopCar, (int count, String productId, allCount, allPrice) { - if (count == 0) { - int index = shopCarGoods.indexWhere((element) => element["id"] == productId); - shopCarGoods.removeAt(index); - } print("shopCarCount: $allCount allPrice: $allPrice "); setState(() { this.allCount = allCount; @@ -489,6 +492,14 @@ class _StoreOrderPage extends State ); } + ///清空购物车 + clearShopCar() { + if (shopCarGoods != null) + shopCarGoods.clear(); + calculatePrice(); + setState(() {}); + } + ///选规格 _queryMiNiDetail(String id) async { BaseData baseData = await minService.miNiDetail(id); @@ -529,7 +540,6 @@ class _StoreOrderPage extends State map = element; } }); - print("result000: $map"); if (map != null) { int index = shopCarGoods.indexWhere((element) => element["id"] == result["id"]); shopCarGoods.removeAt(index); @@ -540,11 +550,25 @@ class _StoreOrderPage extends State } calculatePrice(); } - print("result111: $shopCarGoods"); } calculatePrice() { + AiDecimalAccuracy allPriceDecimal = AiDecimalAccuracy.zero; + int allCount = 0; + shopCarGoods.forEach((element2) { + if (element2["price"] != null && element2["price"] != "") { + double singlePrice = double.tryParse(element2["price"]); + allCount += element2["count"]; + + AiDecimalAccuracy aiDecimalAccuracy = AiDecimalAccuracy.fromInt(element2["count"]); + AiDecimalAccuracy aiPrice = AiDecimalAccuracy.tryParse("$singlePrice"); + + allPriceDecimal += (aiPrice * aiDecimalAccuracy); + } + }); + this.allCount = allCount; + this.allPrice = allPriceDecimal.toDouble(); setState(() {}); } diff --git a/lib/store/store_view/shop_car.dart b/lib/store/store_view/shop_car.dart index b37b4cce..182821b8 100644 --- a/lib/store/store_view/shop_car.dart +++ b/lib/store/store_view/shop_car.dart @@ -1,7 +1,7 @@ +import 'package:ai_decimal_accuracy/ai_decimal_accuracy.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/findMiNiGroupList.dart'; -import 'package:huixiang/retrofit/data/order_info.dart'; import 'package:huixiang/store/store_view/shop_goods.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/round_button.dart'; @@ -10,6 +10,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; class ShopCar extends StatefulWidget { final List productLists; final Function(String id) queryMiNiDetail; + final Function() clearShopCar; final List> shopCarGoods; final Function(int count, String productId, int allCount, double allPrice) fc; @@ -17,11 +18,10 @@ class ShopCar extends StatefulWidget { ShopCar( this.productLists, this.shopCarGoods, - this.fc, - { - this.queryMiNiDetail, - } - ); + this.clearShopCar, + this.fc, { + this.queryMiNiDetail, + }); @override State createState() { @@ -30,213 +30,251 @@ class ShopCar extends StatefulWidget { } class _ShopCar extends State { - - int allCount; - double allPrice; + int allCount = 0; + double allPrice = 0.0; final List> shopCarGoods; _ShopCar(this.shopCarGoods); + @override + void initState() { + super.initState(); + + if (shopCarGoods != null) { + AiDecimalAccuracy allPriceDecimal = AiDecimalAccuracy.zero; + shopCarGoods.forEach((element) { + if (element["price"] != null && element["price"] != "") { + double singlePrice = double.tryParse(element["price"]); + allCount += element["count"]; + + AiDecimalAccuracy aiDecimalAccuracy = AiDecimalAccuracy.fromInt(element["count"]); + AiDecimalAccuracy aiPrice = AiDecimalAccuracy.tryParse("$singlePrice"); + + allPriceDecimal += (aiPrice * aiDecimalAccuracy); + } + }); + allPrice = allPriceDecimal.toDouble(); + } + + } + @override Widget build(BuildContext context) { - return StatefulBuilder(builder: (context1, state) { - return Container( - alignment: Alignment.topCenter, - width: double.infinity, - height: MediaQuery.of(context).size.height / 2, - decoration: BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.only( - topLeft: Radius.circular(8), - topRight: Radius.circular(8), - ), - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Container( - padding: EdgeInsets.only( - top: 16, - left: 16, - right: 16, - bottom: 8, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "已选商品", - style: TextStyle( - color: Colors.black, - fontSize: 14.sp, - fontWeight: MyFontWeight.medium, - ), - ), - InkWell( - onTap: () { - state(() {}); - }, - child: Image.asset( - "assets/image/delete.png", - width: 22, - height: 22, - ), - ), - ], - ), + return StatefulBuilder( + builder: (context1, state) { + return Container( + alignment: Alignment.topCenter, + width: double.infinity, + height: MediaQuery.of(context).size.height / 2, + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.only( + topLeft: Radius.circular(8), + topRight: Radius.circular(8), ), - Expanded( - child: Container( + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( padding: EdgeInsets.only( - top: 8, + top: 16, left: 16, right: 16, + bottom: 8, ), - child: ListView.builder( - itemCount: widget.productLists == null - ? 0 : widget.productLists.length, - physics: BouncingScrollPhysics(), - itemBuilder: (context, position) { - return GestureDetector( - onTap: () {}, - child: ShopGoods( - widget.productLists[position], - (int count, String productId) { - int index = shopCarGoods.indexWhere((element) => element["id"] == productId); - if (count == 0) { - shopCarGoods.removeAt(index); - } - Map map = shopCarGoods.firstWhere((element) => element["id"] == productId); - map["count"] = count; - shopCarGoods[index] = map; - - shopCarGoods.forEach((element) { - if (element["price"] != null && element["price"] != "") { - double singlePrice = double.tryParse(element["price"]); - allCount += element["count"]; - allPrice += (singlePrice * element["count"]); + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "已选商品", + style: TextStyle( + color: Colors.black, + fontSize: 14.sp, + fontWeight: MyFontWeight.medium, + ), + ), + InkWell( + onTap: () { + widget.clearShopCar(); + }, + child: Image.asset( + "assets/image/delete.png", + width: 22, + height: 22, + ), + ), + ], + ), + ), + Expanded( + child: Container( + padding: EdgeInsets.only( + top: 8, + left: 16, + right: 16, + ), + child: ListView.builder( + itemCount: widget.productLists == null + ? 0 + : widget.productLists.length, + physics: BouncingScrollPhysics(), + itemBuilder: (context, position) { + return GestureDetector( + onTap: () {}, + child: ShopGoods( + widget.productLists[position], + (int count, String productId) { + int index = shopCarGoods.indexWhere((element) => element["id"] == productId); + if (count == 0) { + shopCarGoods.removeAt(index); + int index1 = widget.productLists.indexWhere((element) => element.id == productId); + widget.productLists.removeAt(index1); + } else { + Map map; + print("productId: $productId"); + shopCarGoods.forEach((element) { + if (element["id"] == productId) { + map = element; + } + }); + map["count"] = count; + shopCarGoods[index] = map; } - }); - widget.fc(count, productId, allCount, allPrice); + allCount = 0; + allPrice = 0; + shopCarGoods.forEach((element) { + if (element["price"] != null && + element["price"] != "") { + double singlePrice = + double.tryParse(element["price"]); + allCount += element["count"]; + allPrice += (singlePrice * element["count"]); + } + }); - state((){}); - }, - shopCarGoods: _shopCarGoods(position), - queryMiNiDetail: widget.queryMiNiDetail, - ), - ); - }, + widget.fc(count, productId, allCount, allPrice); + + state(() {}); + }, + shopCarGoods: _shopCarGoods(position), + queryMiNiDetail: widget.queryMiNiDetail, + ), + ); + }, + ), ), ), - ), - Stack( - alignment: Alignment.bottomLeft, - children: [ - Container( - height: 54.h, - decoration: BoxDecoration( - color: Color(0xFFFAFAFA), - boxShadow: [ - BoxShadow( - color: Colors.black.withAlpha(8), - offset: Offset(0, -1), - blurRadius: 3, - spreadRadius: 0, - ), - ], - ), - child: Row( - children: [ - Spacer(), - Text( - S.of(context).heji, - style: TextStyle( - fontSize: 12.sp, - fontWeight: MyFontWeight.regular, - color: Colors.black, + Stack( + alignment: Alignment.bottomLeft, + children: [ + Container( + height: 54.h, + decoration: BoxDecoration( + color: Color(0xFFFAFAFA), + boxShadow: [ + BoxShadow( + color: Colors.black.withAlpha(8), + offset: Offset(0, -1), + blurRadius: 3, + spreadRadius: 0, ), - ), - Text( - "¥" + allPrice.toString(), - style: TextStyle( - fontSize: 20.sp, - fontWeight: MyFontWeight.medium, - color: Color(0xFF32A060), + ], + ), + child: Row( + children: [ + Spacer(), + Text( + S.of(context).heji, + style: TextStyle( + fontSize: 12.sp, + fontWeight: MyFontWeight.regular, + color: Colors.black, + ), ), - ), - Spacer(), - GestureDetector( + Text( + "¥" + allPrice.toString(), + style: TextStyle( + fontSize: 20.sp, + fontWeight: MyFontWeight.medium, + color: Color(0xFF32A060), + ), + ), + Spacer(), + GestureDetector( + onTap: () { + Navigator.of(context).pushNamed( + '/router/settlement', + arguments: {}, + ); + }, + child: RoundButton( + width: 103.w, + height: 54.h, + text: S.current.jiesuan, + textColor: Colors.white, + fontWeight: MyFontWeight.regular, + backgroup: Color(0xFF32A060), + radius: 0, + fontSize: 16.sp, + padding: EdgeInsets.symmetric(vertical: 5.h), + ), + ), + ], + ), + ), + Stack( + children: [ + InkWell( onTap: () { - Navigator.of(context).pushNamed( - '/router/settlement', - arguments: {}, - ); + Navigator.of(context).pop(); }, + child: Image.asset( + "assets/image/shopp.png", + width: 88, + height: 88, + ), + ), + Positioned( + right: 15, + top: 14, child: RoundButton( - width: 103.w, - height: 54.h, - text: S.current.jiesuan, + width: 17, + height: 17, + text: allCount.toString(), textColor: Colors.white, fontWeight: MyFontWeight.regular, backgroup: Color(0xFF32A060), - radius: 0, - fontSize: 16.sp, - padding: EdgeInsets.symmetric(vertical: 5.h), + fontSize: 12.sp, + radius: 100, + callback: () { + Navigator.of(context) + .pushNamed('/router/test_page'); + }, ), ), ], ), - ), - Stack( - children: [ - InkWell( - onTap: () { - Navigator.of(context).pop(); - }, - child: Image.asset( - "assets/image/shopp.png", - width: 88, - height: 88, - ), - ), - Positioned( - right: 15, - top: 14, - child: RoundButton( - width: 17, - height: 17, - text: allCount.toString(), - textColor: Colors.white, - fontWeight: MyFontWeight.regular, - backgroup: Color(0xFF32A060), - fontSize: 12.sp, - radius: 100, - callback: () { - Navigator.of(context).pushNamed('/router/test_page'); - }, - ), - ), - ], - ), - ], - ), - ], - ), - ); - }); + ], + ), + ], + ), + ); + }, + ); } Map _shopCarGoods(int position) { + Map map; widget.shopCarGoods.forEach((element) { if (widget.productLists[position].id == element["id"]) { - return element; + map = element; } }); - return null; + return map; } - } diff --git a/lib/store/store_view/shop_goods.dart b/lib/store/store_view/shop_goods.dart index 4ba3f944..446de7b0 100644 --- a/lib/store/store_view/shop_goods.dart +++ b/lib/store/store_view/shop_goods.dart @@ -7,13 +7,13 @@ import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/round_button.dart'; class ShopGoods extends StatefulWidget { - final ProductListBean productList; + final ProductListBean product; final Function(String id) queryMiNiDetail; final Function(int count, String productId) fc; final Map shopCarGoods; ShopGoods( - this.productList, + this.product, this.fc, { this.queryMiNiDetail, this.shopCarGoods, @@ -30,7 +30,6 @@ class _ShopGoods extends State { @override Widget build(BuildContext context) { int buyNum = _count(); - print("result:11 $buyNum"); return Container( color: Colors.white, padding: EdgeInsets.only( @@ -44,7 +43,7 @@ class _ShopGoods extends State { children: [ SizedBox(width: 12.w), MImage( - widget.productList != null ? widget.productList.imgPath : "", + widget.product != null ? widget.product.imgPath : "", width: 70, height: 70, fit: BoxFit.cover, @@ -59,7 +58,7 @@ class _ShopGoods extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - widget.productList.productName, + widget.product.productName, style: TextStyle( color: Colors.black, fontSize: 13.sp, @@ -73,7 +72,7 @@ class _ShopGoods extends State { children: [ Expanded( child: Text( - widget.productList.shortName, + widget.product.shortName, overflow: TextOverflow.ellipsis, maxLines: 2, style: TextStyle( @@ -110,7 +109,7 @@ class _ShopGoods extends State { width: 4, ), Text( - "¥${widget.productList.price}", + "¥${widget.product.price}", style: TextStyle( color: Color(0xFFFF7A1A), fontSize: 11.sp, @@ -133,7 +132,7 @@ class _ShopGoods extends State { width: 4, ), Text( - "¥${widget.productList.applyPrice}", + "¥${widget.product.applyPrice}", style: TextStyle( color: Color(0xFFA29E9E), fontSize: 10.sp, @@ -156,14 +155,14 @@ class _ShopGoods extends State { fontSize: 11.sp, padding: EdgeInsets.symmetric(vertical: 5.h), callback: () { - widget.queryMiNiDetail(widget.productList.id); + widget.queryMiNiDetail(widget.product.id); }, ), if (buyNum > 0) InkWell( onTap: () { reduceCount(); - widget.fc(widget.shopCarGoods["count"], widget.productList.id); + widget.fc(widget.shopCarGoods["count"], widget.product.id); setState(() {}); }, child: Image.asset( @@ -189,7 +188,7 @@ class _ShopGoods extends State { InkWell( onTap: () { addCount(); - widget.fc(widget.shopCarGoods["count"], widget.productList.id); + widget.fc(widget.shopCarGoods["count"], widget.product.id); setState(() {}); }, child: Image.asset( @@ -219,7 +218,7 @@ class _ShopGoods extends State { int _count() { if (widget.shopCarGoods == null) return 0; - if (widget.productList.id == widget.shopCarGoods["id"]) { + if (widget.product.id == widget.shopCarGoods["id"]) { return widget.shopCarGoods["count"]; } diff --git a/lib/store/store_view/store_order_list.dart b/lib/store/store_view/store_order_list.dart index e908b3d5..ce5813c5 100644 --- a/lib/store/store_view/store_order_list.dart +++ b/lib/store/store_view/store_order_list.dart @@ -19,6 +19,7 @@ class StoreOrderListPage extends StatefulWidget { final ScrollController controller; final Function(int count, String productId, int allCount, double allPrice) fc; final Function(String id) queryMiNiDetail; + final Function(List appletProducts) productListResult; List> shopCarGoods; StoreOrderListPage( @@ -28,6 +29,7 @@ class StoreOrderListPage extends StatefulWidget { this.shopCarGoods, this.controller, this.queryMiNiDetail, + this.productListResult, this.fc ); @@ -47,17 +49,6 @@ class _StoreOrderListPage extends State { List appletProducts; List productListBeans = []; - void notifyList(List product){ - setState(() { - product.forEach((e0) { - productListBeans.forEach((e1) { - if(e0.id == e1.id) - e1.buyNum = e0.buyNum; - }); - }); - }); - } - @override void initState() { super.initState(); @@ -87,6 +78,7 @@ class _StoreOrderListPage extends State { if (baseData != null && baseData.isSuccess) { setState(() { appletProducts = baseData.data; + widget.productListResult(appletProducts); productListBeans.clear(); if (appletProducts.length > 0) productListBeans.addAll(appletProducts[0].productList);