diff --git a/lib/main.dart b/lib/main.dart index c8cf3cd4..9dd5c659 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -54,7 +54,7 @@ import 'package:huixiang/setting/permission_setting_page.dart'; import 'package:huixiang/setting/setting_page.dart'; import 'package:huixiang/setting/treaty_page.dart'; import 'package:huixiang/store/store_order.dart'; -import 'package:huixiang/store/store_view/settlement.dart'; +import 'package:huixiang/settlement/settlement.dart'; import 'package:huixiang/test_page.dart'; import 'package:huixiang/union/location_map_page.dart'; diff --git a/lib/retrofit/data/settlement_bean.dart b/lib/retrofit/data/settlement_bean.dart index 5cee84b3..d612b14d 100644 --- a/lib/retrofit/data/settlement_bean.dart +++ b/lib/retrofit/data/settlement_bean.dart @@ -7,6 +7,7 @@ /// promotionInfoDTO : {"couponId":0,"promotionId":0} class SettlementBean { + SettlementBean({ int addressId, int buyNum, diff --git a/lib/retrofit/data/shoppingCart.dart b/lib/retrofit/data/shoppingCart.dart index 78a1794a..9f5b9bc7 100644 --- a/lib/retrofit/data/shoppingCart.dart +++ b/lib/retrofit/data/shoppingCart.dart @@ -30,7 +30,7 @@ class ShoppingCart { return shoppingCartBean; } - Map toJson() => { + Map toJson() => { "cartSum": cartSum, "numberOfPeople": numberOfPeople, "selected": selected, @@ -71,7 +71,7 @@ class ShoppingCartSkuItemListBean { String skuPrice; int skuStock; String storeId; - int tableId; + String tableId; static ShoppingCartSkuItemListBean fromJson(Map map) { if (map == null) return null; diff --git a/lib/retrofit/min_api.dart b/lib/retrofit/min_api.dart index a470375f..59c1a1e8 100644 --- a/lib/retrofit/min_api.dart +++ b/lib/retrofit/min_api.dart @@ -7,6 +7,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; +import 'package:huixiang/retrofit/data/address.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/placeOrderFirst.dart'; import 'package:huixiang/retrofit/data/settlement_bean.dart'; @@ -164,9 +165,11 @@ abstract class MinApiService { ///订单结算信息 @POST("order/getOrderInfo") - Future> getOrderInfo( - @Body() Map param); + Future> getOrderInfo(@Body() Map param); + ///查询用户所有收货地址 + @GET("address/queryMemberAddress") + Future>> queryAddress(); ///小程序下单 @POST("order/placeOrderFirst") @@ -175,4 +178,5 @@ abstract class MinApiService { /// 结算 @POST("order/settlement") Future settlementApi(@Body() Map param); + } diff --git a/lib/retrofit/min_api.g.dart b/lib/retrofit/min_api.g.dart index efe149f7..1d33c8eb 100644 --- a/lib/retrofit/min_api.g.dart +++ b/lib/retrofit/min_api.g.dart @@ -203,6 +203,28 @@ class _MinApiService implements MinApiService { return value; } + @override + Future>> queryAddress() async { + const _extra = {}; + final queryParameters = {}; + final _data = {}; + final _result = await _dio.request>( + 'address/queryMemberAddress', + queryParameters: queryParameters, + options: RequestOptions( + method: 'GET', + headers: {}, + extra: _extra, + baseUrl: baseUrl), + data: _data); + final value = BaseData>.fromJson( + _result.data, + (json) => (json as List) + .map
((i) => Address.fromJson(i as Map)) + .toList()); + return value; + } + @override Future> placeOrderFirst(param) async { ArgumentError.checkNotNull(param, 'param'); diff --git a/lib/store/store_view/settlement.dart b/lib/settlement/settlement.dart similarity index 74% rename from lib/store/store_view/settlement.dart rename to lib/settlement/settlement.dart index 7b145c5c..89cb8947 100644 --- a/lib/store/store_view/settlement.dart +++ b/lib/settlement/settlement.dart @@ -2,11 +2,12 @@ import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; +import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:huixiang/generated/l10n.dart'; -import 'package:huixiang/order/order_view/order_commodity.dart'; -import 'package:huixiang/order/order_view/order_info.dart'; +import 'package:huixiang/retrofit/data/address.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/findMiNiGroupList.dart'; import 'package:huixiang/retrofit/data/placeOrderFirst.dart'; @@ -15,14 +16,13 @@ import 'package:huixiang/retrofit/data/settlement_bean.dart'; import 'package:huixiang/retrofit/data/shoppingCart.dart'; import 'package:huixiang/retrofit/data/store_info.dart'; import 'package:huixiang/retrofit/min_api.dart'; -import 'package:huixiang/store/sku.dart'; -import 'package:huixiang/store/store_view/settlement_order_commodity.dart'; +import 'package:huixiang/settlement/settlement_view/settlement_order_commodity.dart'; import 'package:huixiang/utils/font_weight.dart'; +import 'package:huixiang/utils/location.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; -import 'package:huixiang/view_widget/rename_dialog.dart'; -import 'package:huixiang/view_widget/rename_dialog_content.dart'; import 'package:huixiang/view_widget/round_button.dart'; import 'package:huixiang/view_widget/separator.dart'; +import 'package:intl/intl.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -45,9 +45,6 @@ class _Settlement extends State { SettleOrderInfo settleOrderInfo; int selectedCouponIndex = -1; - List shopCar = []; - List shopCarGoods = []; - TextEditingController _vc; List addressBgs = [ "assets/svg/dingdan_ziqu.svg", @@ -59,8 +56,6 @@ class _Settlement extends State { void initState() { super.initState(); storeInfo = widget.arguments["storeInfo"]; - shopCar = widget.arguments["shopProduct"]; - shopCarGoods = widget.arguments["shopGoods"]; _vc = TextEditingController( text: storeInfo != null ? storeInfo.headMobile : ""); @@ -68,24 +63,69 @@ class _Settlement extends State { SharedPreferences.getInstance().then((value) { String minToken = value.getString("minToken"); String tenant = value.getString("tenant"); + String storeId = value.getString("storeId"); minService = MinApiService( Dio(), context: context, token: minToken, tenant: tenant, + storeId: storeId, ); - queryOrderInfo(); + queryOrderInfo(null, selectedBtn, null, 0, null); + }); + + startLocation(); + } + + BMFCoordinate myLatLng; + String distance = "0"; + + ///定位获取当前的位置 + void startLocation() async { + Location.getInstance() + .aMapFlutterLocation + .onResultCallback() + .listen((event) { + if (event != null && + event["latitude"] != null && + event["longitude"] != null) { + print("location: $event"); + if (event["latitude"] is String && event["longitude"] is String) { + myLatLng = BMFCoordinate(double.tryParse(event["latitude"]), + double.tryParse(event["longitude"])); + } else { + myLatLng = BMFCoordinate(event["latitude"], event["longitude"]); + } + calculateDistance(); + } }); + Location.getInstance().prepareLoc(); + Location.getInstance().startLocation(context); + } + + ///计算距离(当前位置到店位置直线距离) + calculateDistance() async { + BMFCoordinate bmfCoordinate = BMFCoordinate( + double.tryParse(storeInfo.latitude), + double.tryParse(storeInfo.longitude)); + double mi = + await BMFCalculateUtils.getLocationDistance(bmfCoordinate, myLatLng); + NumberFormat numberFormat = NumberFormat("#.#"); + distance = "${numberFormat.format(mi / 1000)}"; + print("distance: $distance"); + setState(() {}); + Location.getInstance().stopLocation(); } ///订单结算信息 - queryOrderInfo() async { + queryOrderInfo( + addressId, isTake, memberCouponId, orderId, promotionId) async { BaseData baseData = await minService.getOrderInfo({ - "addressId": null, - "isTake": 0, - "memberCouponId": null, - "orderId": 0, - "promotionId": null + "addressId": addressId, + "isTake": isTake, + "memberCouponId": memberCouponId, + "orderId": orderId, + "promotionId": promotionId }); if (baseData != null && baseData.isSuccess) { setState(() { @@ -94,6 +134,55 @@ class _Settlement extends State { } } + Address address; + + queryAddress() async { + if (address != null) { + queryOrderInfo(address.id, selectedBtn, null, 0, null); + return; + } + BaseData> baseDate = + await minService.queryAddress().catchError((error) {}); + + BMFCoordinate bmfCoordinate = BMFCoordinate( + double.tryParse(storeInfo.latitude), + double.tryParse(storeInfo.longitude), + ); + if (baseDate != null && baseDate.isSuccess) { + address = baseDate.data[0]; + for (int i = 1; i < baseDate.data.length; i++) { + Address address1 = baseDate.data[i]; + + BMFCoordinate coordinate = await BMFCalculateUtils.coordConvert( + coordinate: BMFCoordinate( + double.tryParse(address.longitude), + double.tryParse(address.latitude), + ), + fromType: BMF_COORD_TYPE.COMMON, + toType: BMF_COORD_TYPE.BD09LL); + BMFCoordinate coordinate1 = await BMFCalculateUtils.coordConvert( + coordinate: BMFCoordinate( + double.tryParse(address1.longitude), + double.tryParse(address1.latitude), + ), + fromType: BMF_COORD_TYPE.COMMON, + toType: BMF_COORD_TYPE.BD09LL); + + double mi = await BMFCalculateUtils.getLocationDistance( + bmfCoordinate, coordinate); + double mi1 = await BMFCalculateUtils.getLocationDistance( + bmfCoordinate, coordinate1); + print("address 1111: $mi"); + print("address 11: $mi1"); + if (mi1 < mi) { + address = address1; + } + } + + queryOrderInfo(address.id, selectedBtn, null, 0, null); + } + } + ///小程序下单 queryPlaceOrderFirst() async { PlaceOrderFirst placeOrderFirst = PlaceOrderFirst(); @@ -109,34 +198,21 @@ class _Settlement extends State { placeOrderFirst.parentId = 0; placeOrderFirst.payChannel = 1; placeOrderFirst.promotionInfoDTO = PlaceOrderFirstPromotionInfoDTO(); - placeOrderFirst.promotionInfoDTO.couponId = selectedCouponIndex == -1?null : settleOrderInfo.couponList[selectedCouponIndex].couponId; + placeOrderFirst.promotionInfoDTO.couponId = selectedCouponIndex == -1 + ? null + : settleOrderInfo.couponList[selectedCouponIndex].couponId; placeOrderFirst.promotionInfoDTO.promotionId = "0"; placeOrderFirst.recMobile = storeInfo.headMobile; placeOrderFirst.shoppingCartSkuItemList = []; placeOrderFirst.skuItemDTOList = []; - shopCarGoods.forEach((e0) { - SkuItemDTOList skuItemDTOListBean = SkuItemDTOList(); - skuItemDTOListBean.skuId = e0.skuId; - skuItemDTOListBean.buyNum = e0.count; - PlatterList platterListBean = PlatterList(); - platterListBean.skuId = e0.skuId; - platterListBean.productId = e0.id; - skuItemDTOListBean.platterList = []; - skuItemDTOListBean.platterList.add(platterListBean); - placeOrderFirst.skuItemDTOList.add(skuItemDTOListBean); - ShoppingCartSkuItemListBean shoppingCartSkuItemListBean = ShoppingCartSkuItemListBean(); - shoppingCartSkuItemListBean.buyNum = e0.count; - shoppingCartSkuItemListBean.productId = e0.id; - shoppingCartSkuItemListBean.productName = e0.name; - shoppingCartSkuItemListBean.skuId = e0.skuId; - placeOrderFirst.shoppingCartSkuItemList.add(shoppingCartSkuItemListBean); - }); + placeOrderFirst.source = 1; placeOrderFirst.storeId = storeInfo.id; placeOrderFirst.subcribeTime = null; placeOrderFirst.tableId = 0; - BaseData baseData = await minService.placeOrderFirst( - placeOrderFirst.toJson()).catchError((error) {}); + BaseData baseData = await minService + .placeOrderFirst(placeOrderFirst.toJson()) + .catchError((error) {}); if (baseData != null && baseData.isSuccess) { querySettlement(); } @@ -150,97 +226,18 @@ class _Settlement extends State { settlementBean.id = 0; settlementBean.isLogistics = true; settlementBean.orderProductVOList = []; - shopCarGoods.forEach((e0) { - OrderProductVOList orderProductVOListBean = OrderProductVOList(); - orderProductVOListBean.skuId = e0.skuId; - orderProductVOListBean.buyNum = e0.count; - ActInfo actInfoBean = ActInfo(); - actInfoBean.couponDiscountAmount = 0; - actInfoBean.couponDiscountRate = 0; - actInfoBean.couponId = 0; - actInfoBean.couponType = 0; - actInfoBean.discountAmount = 0; - actInfoBean.discountRate = 0; - actInfoBean.promotionId = 0; - actInfoBean.promotionType = 0; - AdditionalComment additionalCommentBean = AdditionalComment(); - additionalCommentBean.bizType = 0; - additionalCommentBean.commentImgs = ""; - additionalCommentBean.commentImgsFlag = false; - additionalCommentBean.commentStar = 0; - additionalCommentBean.commentText = ""; - additionalCommentBean.descStar = 0; - additionalCommentBean.hideFlag = true; - additionalCommentBean.id = 0; - additionalCommentBean.isDelete = true; - additionalCommentBean.likeNum = 0; - additionalCommentBean.logisticsStar = 0; - additionalCommentBean.mid = 0; - additionalCommentBean.orderProductId = 0; - additionalCommentBean.parentId = 0; - additionalCommentBean.productId = 0; - additionalCommentBean.sensitiveFlag = true; - additionalCommentBean.serviceStar = 0; - additionalCommentBean.storeId = 0; - settlementBean.buyNum = 0; - Comment commentBean = Comment(); - commentBean.bizType = 0; - commentBean.commentImgs = ""; - commentBean.commentImgsFlag = true; - commentBean.commentStar = 0; - commentBean.commentText = ""; - commentBean.descStar = 0; - commentBean.hideFlag = true; - commentBean.id = 0; - commentBean.isDelete = true; - commentBean.logisticsStar = 0; - commentBean.mid = 0; - commentBean.orderProductId = 0; - commentBean.parentId = 0; - commentBean.productId = 0; - commentBean.sensitiveFlag = true; - commentBean.serviceStar = 0; - commentBean.storeId = 0; - orderProductVOListBean.commentStatus = 0; - orderProductVOListBean.discountAmount = 0; - orderProductVOListBean.id = 0; - orderProductVOListBean.productId = 0; - orderProductVOListBean.productName = ""; - orderProductVOListBean.returnCode = ""; - orderProductVOListBean.returnStatus = 0; - orderProductVOListBean.returnType = 0; - orderProductVOListBean.sellPrice = 0; - orderProductVOListBean.skuId = "0"; - orderProductVOListBean.skuImg = ""; - orderProductVOListBean.skuNameStr = ""; - settlementBean.orderProductVOList.add(orderProductVOListBean); - }); + settlementBean.payChannel = 0; settlementBean.promotionInfoDTO = PromotionInfoDTO(); settlementBean.promotionInfoDTO.couponId = 0; settlementBean.promotionInfoDTO.promotionId = 0; - BaseData baseData = await minService.settlementApi({ - }).catchError((error) {}); - if (baseData != null && baseData.isSuccess) { - - } + BaseData baseData = + await minService.settlementApi({}).catchError((error) {}); + if (baseData != null && baseData.isSuccess) {} } bool isEdit = false; - String totalPrice(bool discountFlag) { - if (shopCarGoods == null) return ""; - double totalPrice = 0; - shopCarGoods.forEach((element) { - totalPrice += double.tryParse(element.price) * element.count; - }); - if(selectedCouponIndex != -1 && discountFlag){ - double discountA = double.parse(settleOrderInfo.couponList[selectedCouponIndex].discountAmount); - totalPrice -= discountA; - } - return totalPrice.toStringAsFixed(2); - } - @override Widget build(BuildContext context) { return Stack( @@ -267,32 +264,86 @@ class _Settlement extends State { titleSize: 18.sp, ), body: Container( - child: SingleChildScrollView( - physics: BouncingScrollPhysics(), - child: Container( - child: Column( - children: [ - distributionMode(), - SizedBox( - height: 16, - ), + child: Column( + children: [ + Expanded( + child: Container( + child: SingleChildScrollView( + physics: BouncingScrollPhysics(), + child: Container( + child: Column( + children: [ + distributionMode(), + SizedBox( + height: 16, + ), - /// 预留的手机号, 可修改 - phoneWidget(), + /// 预留的手机号, 可修改 + phoneWidget(), - ///订单商品 - SettlementOrderCommodity(shopCar, 0, shopCarGoods, settleOrderInfo), - // if (payStatus == 0) couponRemarks(), - /*(payStatus == 0) ? paySelector() : */ + ///订单商品 + SettlementOrderCommodity( + 0, + settleOrderInfo, + ), + // if (payStatus == 0) couponRemarks(), + /*(payStatus == 0) ? paySelector() : */ - ///优惠券/备注 - activityRemarks(), + ///优惠券/备注 + activityRemarks(), - ///支付方式 - payMethod(), - ], + ///支付方式 + payMethod(), + ], + ), + ), + ), + ), + flex: 1, ), - ), + Container( + height: 54.h, + color: Colors.white, + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Spacer(), + Text( + S.of(context).heji, + style: TextStyle( + fontSize: 12.sp, + fontWeight: MyFontWeight.regular, + color: Colors.black, + ), + ), + Text( + "¥${settleOrderInfo.orderSum}", + style: TextStyle( + fontSize: 20.sp, + fontWeight: MyFontWeight.semi_bold, + color: Color(0xFF32A060), + ), + ), + SizedBox( + width: 28, + ), + RoundButton( + width: 103.w, + height: 54.h, + text: S.current.jiesuan, + textColor: Colors.white, + fontWeight: MyFontWeight.regular, + backgroup: Color(0xFF32A060), + fontSize: 16.sp, + padding: EdgeInsets.symmetric( + vertical: 5.h, + ), + callback: () {}, + ), + ], + ), + ), + ], ), ), ), @@ -301,51 +352,6 @@ class _Settlement extends State { top: 0, bottom: 0, ), - Positioned( - bottom: 0, - left: 0, - right: 0, - child: Container( - height: 54.h, - color: Colors.white, - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Spacer(), - Text( - S.of(context).heji, - style: TextStyle( - fontSize: 12.sp, - fontWeight: MyFontWeight.regular, - color: Colors.black, - ), - ), - Text( - "¥19.00", - style: TextStyle( - fontSize: 20.sp, - fontWeight: MyFontWeight.semi_bold, - color: Color(0xFF32A060), - ), - ), - SizedBox( - width: 28, - ), - RoundButton( - width: 103.w, - height: 54.h, - text: S.current.jiesuan, - textColor: Colors.white, - fontWeight: MyFontWeight.regular, - backgroup: Color(0xFF32A060), - fontSize: 16.sp, - padding: EdgeInsets.symmetric(vertical: 5.h,), - callback: () {}, - ), - ], - ), - ), - ) ], ); } @@ -504,9 +510,8 @@ class _Settlement extends State { flex: 1, child: GestureDetector( onTap: () { - setState(() { - selectedBtn = 0; - }); + selectedBtn = 0; + queryOrderInfo(null, selectedBtn, null, 0, null); }, child: Container( height: 50.h, @@ -526,9 +531,8 @@ class _Settlement extends State { flex: 1, child: GestureDetector( onTap: () { - setState(() { - selectedBtn = 1; - }); + selectedBtn = 1; + queryAddress(); }, child: Container( height: 50.h, @@ -579,8 +583,9 @@ class _Settlement extends State { Expanded( child: Column( mainAxisAlignment: - MainAxisAlignment.spaceBetween, + MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.max, children: [ Text( storeInfo != null @@ -594,7 +599,7 @@ class _Settlement extends State { ), ), SizedBox( - height: 4, + height: 6, ), Text( storeInfo != null @@ -608,9 +613,6 @@ class _Settlement extends State { color: Color(0xFF727272), ), ), - SizedBox( - height: 12, - ), ], ), ), @@ -634,7 +636,9 @@ class _Settlement extends State { RoundButton( width: 41.w, height: 13, - text: "距离您11m", + text: distance.length > 3 + ? "${distance}km" + : "距离您${distance}km", textColor: Color(0xFF34A262), fontWeight: MyFontWeight.semi_bold, radius: 7.5, @@ -649,8 +653,11 @@ class _Settlement extends State { ], ), ], - ) + ), ], + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisSize: MainAxisSize.max, ), ], ), @@ -692,13 +699,15 @@ class _Settlement extends State { color: Color(0xFF4C4C4C), ), ), - ), InkWell( + ), + InkWell( onTap: () { setState(() { - Navigator.of(context).popAndPushNamed('/router/manage_address_page'); + Navigator.of(context).popAndPushNamed( + '/router/manage_address_page'); }); }, - child:Icon( + child: Icon( Icons.keyboard_arrow_right, size: 24, ), @@ -772,10 +781,13 @@ class _Settlement extends State { ), ), Text( - (settleOrderInfo?.couponList == null || settleOrderInfo.couponList.length == 0) - ? "暂无可用优惠券" : (selectedCouponIndex == -1 - ? "拥有${settleOrderInfo.couponList.length}张优惠券": - settleOrderInfo.couponList[selectedCouponIndex].promotionName), + (settleOrderInfo?.couponList == null || + settleOrderInfo.couponList.length == 0) + ? "暂无可用优惠券" + : (selectedCouponIndex == -1 + ? "拥有${settleOrderInfo.couponList.length}张优惠券" + : settleOrderInfo + .couponList[selectedCouponIndex].promotionName), textAlign: TextAlign.end, style: TextStyle( fontSize: 14.sp, @@ -956,117 +968,105 @@ class _Settlement extends State { ); } - ///手机号编辑 - // showAlertDialog() { - // //显示对话框 - // showDialog( - // barrierDismissible: false, - // context: context, - // builder: (context) { - // return RenameDialog( - // contentWidget: RenameDialogContent( - // title: "请输入新的手机号码", - // okBtnTap: () { - // // print( - // // "输入框中的文字为:${_vc.text}", - // // ); - // }, - // vc: _vc, - // cancelBtnTap: () {}, - // ), - // ); - // }); - // } - ///优惠券弹窗 couponCart() { showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (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), + 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: [ - Expanded( - child: Text( - (settleOrderInfo?.couponList == null || settleOrderInfo.couponList.length == 0) - ? "暂无可用优惠券" : (selectedCouponIndex == -1 - ? "拥有${settleOrderInfo.couponList.length}张优惠券": - settleOrderInfo.couponList[selectedCouponIndex].promotionName), - style: TextStyle( - color: Color(0xFF000000), - fontSize: 14.sp, - fontWeight: MyFontWeight.medium, + 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: [ + Expanded( + child: Text( + (settleOrderInfo?.couponList == null || + settleOrderInfo.couponList.length == 0) + ? "暂无可用优惠券" + : (selectedCouponIndex == -1 + ? "拥有${settleOrderInfo.couponList.length}张优惠券" + : settleOrderInfo + .couponList[selectedCouponIndex] + .promotionName), + style: TextStyle( + color: Color(0xFF000000), + fontSize: 14.sp, + fontWeight: MyFontWeight.medium, + ), ), ), + InkWell( + onTap: () { + Navigator.of(context).pop(); + }, + child: Image.asset( + "assets/image/cancel.png", + width: 24, + height: 24, + ), + ), + ], + ), + ), + Expanded( + child: Container( + padding: EdgeInsets.only( + top: 8, + left: 16, + right: 16, ), - InkWell( - onTap: () { - Navigator.of(context).pop(); + child: ListView.builder( + itemCount: settleOrderInfo == null + ? 0 + : settleOrderInfo.couponList.length, + physics: BouncingScrollPhysics(), + itemBuilder: (context, position) { + return GestureDetector( + onTap: () {}, + child: couponItem( + settleOrderInfo.couponList[position], + position, + ), + ); }, - child: Image.asset( - "assets/image/cancel.png", - width: 24, - height: 24, - ), ), - ], - ), - ), - Expanded( - child: Container( - padding: EdgeInsets.only( - top: 8, - left: 16, - right: 16, - ), - child: ListView.builder( - itemCount:settleOrderInfo == null ? 0 : settleOrderInfo.couponList.length, - physics: BouncingScrollPhysics(), - itemBuilder: (context, position) { - // return shoppGoodsItem(); - return GestureDetector( - onTap: () {}, - child: couponItem(settleOrderInfo.couponList[position],position), - ); - }, ), ), - ), - ], - ), - ); - }); + ], + ), + ); + }, + ); }, ); } ///优惠券列表 - Widget couponItem(CouponListBean couponList,int index) { + Widget couponItem(CouponListBean couponList, int index) { return StatefulBuilder(builder: (context1, state) { return Container( height: 170, @@ -1212,16 +1212,20 @@ class _Settlement extends State { // SmartDialog.showToast("当前优惠券不可用", alignment: Alignment.center); // return; // } - double toPrice = double.parse(totalPrice(false)); - double amount = double.parse(couponList.fullAmount); - if(toPrice <= amount){ - SmartDialog.showToast("当前金额不够使用优惠券", alignment: Alignment.center); + double toPrice = double.parse("0"); + double amount = + double.parse(couponList.fullAmount); + if (toPrice <= amount) { + SmartDialog.showToast("当前金额不够使用优惠券", + alignment: Alignment.center); return; } state(() { - if(selectedCouponIndex == index) + if (selectedCouponIndex == index) selectedCouponIndex = -1; - else {selectedCouponIndex = index;} + else { + selectedCouponIndex = index; + } }); Navigator.of(context).pop(); }); @@ -1243,7 +1247,7 @@ class _Settlement extends State { ], ), ), - flex: (couponList?.isEx??false) ? 97 : 97, + flex: (couponList?.isEx ?? false) ? 97 : 97, ), Container( padding: EdgeInsets.symmetric(horizontal: 23.w), @@ -1254,7 +1258,7 @@ class _Settlement extends State { ), ), Expanded( - flex: (couponList?.isEx??false) ? 56 : 42, + flex: (couponList?.isEx ?? false) ? 56 : 42, child: Container( margin: EdgeInsets.symmetric(horizontal: 23.w, vertical: 8.h), child: Column( @@ -1273,23 +1277,28 @@ class _Settlement extends State { ), GestureDetector( child: Icon( - (!(couponList?.isEx??false)) + (!(couponList?.isEx ?? false)) ? Icons.keyboard_arrow_down : Icons.keyboard_arrow_up, color: Colors.black, size: 18, ), //点击按钮布局 - onTap: (){ + onTap: () { setState(() { - state((){ - this.settleOrderInfo.couponList[index].isEx = !(this.settleOrderInfo.couponList[index].isEx??false); + state(() { + this.settleOrderInfo.couponList[index].isEx = + !(this + .settleOrderInfo + .couponList[index] + .isEx ?? + false); }); }); }, ), ], ), - if (couponList?.isEx??false) + if (couponList?.isEx ?? false) Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ @@ -1302,7 +1311,7 @@ class _Settlement extends State { ), Text( (couponList.useStartTime == null && - couponList.useEndTime == null) + couponList.useEndTime == null) ? S.of(context).quantian : "${couponList.useStartTime.replaceAll("-", ".").split(" ")[0]} - ${couponList.useEndTime.replaceAll("-", ".").split(" ")[0]}", style: TextStyle( diff --git a/lib/store/store_view/settlement_order_commodity.dart b/lib/settlement/settlement_view/settlement_order_commodity.dart similarity index 89% rename from lib/store/store_view/settlement_order_commodity.dart rename to lib/settlement/settlement_view/settlement_order_commodity.dart index 4a8d66c1..51286a0b 100644 --- a/lib/store/store_view/settlement_order_commodity.dart +++ b/lib/settlement/settlement_view/settlement_order_commodity.dart @@ -3,18 +3,16 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/findMiNiGroupList.dart'; import 'package:huixiang/retrofit/data/settleOrderInfo.dart'; -import 'package:huixiang/store/sku.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/separator.dart'; class SettlementOrderCommodity extends StatefulWidget { final int isTakeOut; - final List orderInfo; - final List shopCarGood; + final SettleOrderInfo settleOrderInfo; - SettlementOrderCommodity(this.orderInfo, this.isTakeOut,this.shopCarGood,this.settleOrderInfo); + SettlementOrderCommodity(this.isTakeOut,this.settleOrderInfo); @override State createState() { @@ -67,11 +65,11 @@ class _SettlementOrderCommodity extends State { } List commodityList() { - if (widget.orderInfo == null) return []; + if (widget.settleOrderInfo == null) return []; List widgets = []; - if (widget.orderInfo != null) { + if (widget.settleOrderInfo != null) { widgets.addAll( - widget.orderInfo.map((e) => commodityItem(e)).toList()); + widget.settleOrderInfo.orderProductList.map((e) => commodityItem(e)).toList()); } widgets.add(SizedBox(height: 20.h)); @@ -102,20 +100,14 @@ class _SettlementOrderCommodity extends State { return widgets; } - Widget commodityItem(ProductListBean productList) { - Sku goodsBuyInfo; - widget.shopCarGood.forEach((element) { - if(element.id == productList.id){ - goodsBuyInfo = element; - return; - } - }); + Widget commodityItem(OrderProductListBean productList) { + return Container( margin: EdgeInsets.only(top: 8.h, bottom: 8.h), child: Row( children: [ MImage( - productList.imgPath, + productList.skuImg, width: 44.w, height: 44, fit: BoxFit.cover, @@ -146,7 +138,7 @@ class _SettlementOrderCommodity extends State { ), ), Text( - "x${goodsBuyInfo.count}", + "x${productList.buyNum}", style: TextStyle( fontSize: 12.sp, color: Color(0xFF727272), @@ -172,7 +164,7 @@ class _SettlementOrderCommodity extends State { ), ), Text( - S.of(context).yuan_(productList.price), + S.of(context).yuan_(productList.sellPrice), style: TextStyle( fontWeight: FontWeight.bold, fontSize: 12.sp, @@ -319,9 +311,8 @@ class _SettlementOrderCommodity extends State { children: [ Expanded( child: Text( - S.of(context).gongjijianshangpin((widget.orderInfo != null && - widget.orderInfo != null) - ? widget.orderInfo.length + S.of(context).gongjijianshangpin((widget.settleOrderInfo != null) + ? widget.settleOrderInfo.orderProductList.length : "0"), style: TextStyle( fontSize: 10.sp, @@ -356,11 +347,7 @@ class _SettlementOrderCommodity extends State { } String totalPrice() { - if (widget.shopCarGood == null) return ""; - double totalPrice = 0; - widget.shopCarGood.forEach((element) { - totalPrice += double.tryParse(element.price) * element.count; - }); - return "$totalPrice"; + if (widget.settleOrderInfo.orderProductList == null) return ""; + return "${widget.settleOrderInfo.price}"; } } diff --git a/lib/store/sku.dart b/lib/store/sku.dart deleted file mode 100644 index 997b7360..00000000 --- a/lib/store/sku.dart +++ /dev/null @@ -1,71 +0,0 @@ -class Sku { - Sku( - this.id, - this.count, - this.price, - ); - - String id; - int count; - String price; - String name; - String skuId; - String skuValue; - - List attr = []; - - bool compareTo(List attr) { - bool attrY = true; - for (int i = 0; i < this.attr.length; i++) { - if (!this.attr[i].compareTo(attr[i])) { - attrY = false; - return attrY; - } - } - return attrY; - } - - Map toJson() => { - "id": id, - "count": count, - "price": price, - "skuId": skuId, - "name": name, - "value": skuValue, - "attr": attr.map((e) => e.toJson()).toList(), - }; - - Sku copy() { - return Sku( - this.id, - this.count, - this.price, - ) - ..name = this.name - ..skuValue = this.skuValue - ..attr = attr.map((e) => e.copy()).toList(); - } -} - -class Attr { - Attr(this.attrId, this.attrValue); - - String attrId; - String attrValue; - - bool compareTo(Attr attr) { - return attrId == attr.attrId && attrValue == attr.attrValue; - } - - Map toJson() => { - "attrId": attrId, - "attrValue": attrValue, - }; - - Attr copy() { - return Attr( - this.attrId, - this.attrValue, - ); - } -} diff --git a/lib/store/store_order.dart b/lib/store/store_order.dart index 3cffa6e2..015a3936 100644 --- a/lib/store/store_order.dart +++ b/lib/store/store_order.dart @@ -9,13 +9,11 @@ import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/activity.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; -import 'package:huixiang/retrofit/data/findMiNiGroupList.dart'; import 'package:huixiang/retrofit/data/miNiDetail.dart'; import 'package:huixiang/retrofit/data/shoppingCart.dart'; import 'package:huixiang/retrofit/data/store_info.dart'; import 'package:huixiang/retrofit/min_api.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; -import 'package:huixiang/store/sku.dart'; import 'package:huixiang/store/store_view/product_sku.dart'; import 'package:huixiang/store/store_view/shop_car.dart'; import 'package:huixiang/store/store_view/store_activity.dart'; @@ -53,8 +51,7 @@ class _StoreOrderPage extends State StoreOrderListPage storeOrderListPage; ScrollController controller = ScrollController(); - - List shopCarGoods; + ShoppingCart shopCarGoods; @override void initState() { @@ -112,6 +109,10 @@ class _StoreOrderPage extends State tenant: tenant, storeId: storeId, ); + queryShopCar().then((value) { + this.shopCarGoods = value; + setState(() {}); + }); } }); } @@ -314,7 +315,7 @@ class _StoreOrderPage extends State shopCarGoods, controller, _queryMiNiDetail, - _productListResult, + // _productListResult, _fc, ), @@ -352,7 +353,7 @@ class _StoreOrderPage extends State ), ), Text( - "¥$allPrice", + "¥${shopCarGoods != null ? shopCarGoods.cartSum : "0.0"}", style: TextStyle( fontSize: 20.sp, fontWeight: MyFontWeight.medium, @@ -398,7 +399,7 @@ class _StoreOrderPage extends State child: RoundButton( width: 17, height: 17, - text: "${allCount}", + text: "${count()}", textColor: Colors.white, fontWeight: MyFontWeight.regular, backgroup: Color(0xFF32A060), @@ -415,9 +416,24 @@ class _StoreOrderPage extends State ); } + ///计算个数 + int count() { + if (shopCarGoods == null || + shopCarGoods.shoppingCartSkuItemList == null || + shopCarGoods.shoppingCartSkuItemList.length == 0) return 0; + int count = 0; + shopCarGoods.shoppingCartSkuItemList.forEach((element) { + count += element.buyNum; + }); + return count; + } + + ///去下单结算页面 toDownOrder() { - if (shopCarGoods == null || shopCarGoods.length == 0) { - SmartDialog.showToast("请选择要购买的商品~"); + int num = count(); + + if (num == 0) { + SmartDialog.showToast("请先选择您要购买的商品!~"); return; } @@ -425,7 +441,6 @@ class _StoreOrderPage extends State '/router/settlement', arguments: { "storeInfo": storeInfo, - "shopGoods": shopCarGoods, }, ); } @@ -467,25 +482,26 @@ class _StoreOrderPage extends State ); } + GlobalKey shopCartKey = GlobalKey(); + ///购物车弹窗 showShoppingCart() { queryShopCar().then((value) { - + this.shopCarGoods = value; showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return ShopCar( - value, + shopCartKey, + this.shopCarGoods, clearShopCar, toDownOrder, - (int count, String productId, allCount, allPrice) { - // print("shopCarCount: $allCount allPrice: $allPrice "); - }, + shopCartAdd, + shopCartReduce, ); }, ); - }); } @@ -493,7 +509,7 @@ class _StoreOrderPage extends State clearShopCar() async { BaseData baseData = await minService.clearShoppingCart(); if (baseData.isSuccess) { - + shopCarGoods = null; setState(() {}); } } @@ -506,58 +522,198 @@ class _StoreOrderPage extends State } } + ///选规格弹窗 + showStoreSelector(MiNiDetail miNiDetail, String id) async { + showModalBottomSheet( + context: context, + backgroundColor: Colors.transparent, + builder: (context) { + return ProductSku( + miNiDetail, + shopCarGoods, + id, + _addShopCar, + add, + reduce, + ); + }, + ); + } + ///添加购物车 - _addShopCar(Sku sku) async { - if (sku != null) { + Future _addShopCar(MiNiDetail miNiDetail, selectSkus, int count) async { + ProductSkuVOListBean productSku = miNiDetail.productSkuVOList.firstWhere((element) { + bool gg = true; + selectSkus.forEach((element1) { + if (element.skuNameStr.indexOf(element1) < 0) { + gg = false; + return gg; + } + }); + return gg; + }); + if (productSku == null) return; + String skuId = productSku.id; + String skuValue = selectSkus.toString().replaceAll("[", "").replaceAll("]", "").replaceAll(",", ""); + + if (miNiDetail != null) { BaseData> baseDate = await minService.addShoppingCart({ "storeId": storeInfo.id, "storeName": storeInfo.storeName ?? "", "tableId": 0, "shoppingCartSkuItemList": [ { - "buyNum": sku.count, - "id": sku.skuId, - "platterList": [ - { - "skuId": sku.skuId - } - ], - "productId": sku.id, - "productName": sku.name, - "skuName": "", + "buyNum": count, + "id": skuId, + "productId": miNiDetail.id, + "productName": miNiDetail.productName, + "skuName": skuValue, "storeId": storeInfo.id, - } + "skuPrice": productSku.applyPrice, + "skuStock": productSku.skuStock, + "tableId": 0, + }, ], }); if (baseDate != null && baseDate.isSuccess) { - queryShopCar(); + queryShopCar().then((value) { + this.shopCarGoods = value; + setState(() {}); + }); } } } ///查询购物车 - Future> queryShopCar() async { + Future queryShopCar() async { BaseData> baseDate = await minService.getShoppingCart(0); - if (baseDate != null && baseDate.isSuccess) { - return baseDate.data; + if (baseDate != null && + baseDate.isSuccess && + baseDate.data != null && + baseDate.data.length > 0) { + return baseDate.data[0]; } else { - return []; + return null; } } - ///选规格弹窗 - showStoreSelector(MiNiDetail miNiDetail, String id) async { - showModalBottomSheet( - context: context, - backgroundColor: Colors.transparent, - builder: (context) { - return ProductSku(miNiDetail, id, _addShopCar); - }, - ); + ///购物车➕1 + shopCartAdd(ShoppingCartSkuItemListBean cartSkuItem) async { + Map shopCarTemp = shopCarGoods.toJson(); + cartSkuItem.buyNum += 1; + shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()]; + BaseData> baseDate = await minService.shoppingCartSingle(shopCarTemp); + if (baseDate.isSuccess) { + queryShopCar().then((value) { + this.shopCarGoods = value; + if (shopCartKey != null) { + shopCartKey.currentState.setState(() {}); + } + setState(() {}); + }); + } + } + + ///购物车➖1 + shopCartReduce(ShoppingCartSkuItemListBean cartSkuItem) async { + Map shopCarTemp = shopCarGoods.toJson(); + cartSkuItem.buyNum -= 1; + shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()]; + BaseData> baseDate = await minService.shoppingCartSingle(shopCarTemp); + if (baseDate.isSuccess) { + queryShopCar().then((value) { + this.shopCarGoods = value; + if (shopCartKey != null) { + shopCartKey.currentState.setState(() {}); + } + setState(() {}); + }); + } + } + + ///商品➕1 + add(MiNiDetail miNiDetail, selectSkus) async { + ProductSkuVOListBean productSku = miNiDetail.productSkuVOList.firstWhere((element) { + return skuY(element, selectSkus); + }); + + if (productSku == null) return; + String skuId = productSku.id; + if (shopCarGoods == null) { + await _addShopCar(miNiDetail, selectSkus, 2); + return; + } + int shopSkuIndex = shopCarGoods.shoppingCartSkuItemList.indexWhere((element) => skuId == element.id); + Map shopCarTemp = shopCarGoods.toJson(); + shopCarGoods.tableId = "0"; + + if (shopSkuIndex >= 0) { + shopCarGoods.shoppingCartSkuItemList[shopSkuIndex].buyNum += 1; + ShoppingCartSkuItemListBean cartSkuItem = shopCarGoods.shoppingCartSkuItemList.firstWhere((element) => skuId == element.id); + shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()]; + } else { + await _addShopCar(miNiDetail, selectSkus, 2); + return; + } + + BaseData> baseDate = await minService.shoppingCartSingle(shopCarTemp); + if (baseDate.isSuccess) { + queryShopCar().then((value) { + this.shopCarGoods = value; + setState(() {}); + }); + } + } + + ///商品➖1 + reduce(MiNiDetail miNiDetail, selectSkus) async { + ProductSkuVOListBean productSku = miNiDetail.productSkuVOList.firstWhere((element) { + return skuY(element, selectSkus); + }); + + if (productSku == null) return; + String skuId = productSku.id; + if (shopCarGoods == null) { + await _addShopCar(miNiDetail, selectSkus, 2); + return; + } + ShoppingCartSkuItemListBean shopSkuItem = shopCarGoods.shoppingCartSkuItemList.firstWhere((element) => skuId == element.skuId); + int shopSkuIndex = shopCarGoods.shoppingCartSkuItemList.indexWhere((element) => skuId == element.skuId); + + if (shopSkuItem != null) { + if (shopSkuItem.buyNum > 1) { + shopCarGoods.shoppingCartSkuItemList[shopSkuIndex].buyNum -= 1; + } + } else { + await _addShopCar(miNiDetail, selectSkus, 2); + return; + } + shopCarGoods.tableId = "0"; + + Map shopCarTemp = shopCarGoods.toJson(); + ShoppingCartSkuItemListBean cartSkuItem = shopCarGoods.shoppingCartSkuItemList.firstWhere((element) => skuId == element.id); + shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()]; + + BaseData> baseDate = await minService.shoppingCartSingle(shopCarTemp); + if (baseDate.isSuccess) { + queryShopCar().then((value) { + this.shopCarGoods = value; + setState(() {}); + }); + } + } + + bool skuY(ProductSkuVOListBean productSku, selectSkus) { + bool gg = true; + selectSkus.forEach((element1) { + if (productSku.skuNameStr.indexOf(element1) < 0) { + gg = false; + return gg; + } + }); + return gg; } -// @override -// bool get wantKeepAlive => true; } class Title extends StatefulWidget { diff --git a/lib/store/store_view/product_sku.dart b/lib/store/store_view/product_sku.dart index efcec472..7d8a6a59 100644 --- a/lib/store/store_view/product_sku.dart +++ b/lib/store/store_view/product_sku.dart @@ -2,16 +2,28 @@ import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/retrofit/data/miNiDetail.dart'; -import 'package:huixiang/store/sku.dart'; +import 'package:huixiang/retrofit/data/shoppingCart.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/round_button.dart'; class ProductSku extends StatefulWidget { + final MiNiDetail miNiDetail; final String productId; - final Function(Sku sku) addShopCar; - ProductSku(this.miNiDetail, this.productId, this.addShopCar); + final Function(MiNiDetail miNiDetail, List selectSkus, int count) addShopCar; + final Function(MiNiDetail miNiDetail, List selectSkus) add; + final Function(MiNiDetail miNiDetail, List selectSkus) reduce; + final ShoppingCart shopCarGoods; + + ProductSku( + this.miNiDetail, + this.shopCarGoods, + this.productId, + this.addShopCar, + this.add, + this.reduce, + ); @override State createState() { @@ -21,29 +33,47 @@ class ProductSku extends StatefulWidget { class _ProductSku extends State { List selectSkus = []; - int selectCount = 1; - Sku tempSelected; + int count = 1; + @override void initState() { super.initState(); - if (widget.miNiDetail != null) { - tempSelected = Sku(widget.productId, 1, widget.miNiDetail.price); - tempSelected.name = widget.miNiDetail.productName; + widget.miNiDetail.attrList.forEach((element) { + selectSkus.add(element.attrValueList[0].attrValue); + }); - widget.miNiDetail.attrList.forEach((element) { - selectSkus.add(element.attrValueList[0].attrValue); - tempSelected.attr.add( - Attr( - "${element.id}", - "${element.attrValueList[0].id}", - ), - ); - }); + buildCount(); + } + + buildCount() { + count = 1; + ProductSkuVOListBean productSku = widget.miNiDetail.productSkuVOList.firstWhere((element) { + return skuY(element, selectSkus); + }); + if (productSku == null) return; + String skuId = productSku.id; + if (widget.shopCarGoods == null || widget.shopCarGoods.shoppingCartSkuItemList == null) return; + int shopSkuIndex = widget.shopCarGoods.shoppingCartSkuItemList.indexWhere((element) => skuId == element.id); + if (shopSkuIndex >= 0) { + count = widget.shopCarGoods.shoppingCartSkuItemList[shopSkuIndex].buyNum; } + + } + + bool skuY(ProductSkuVOListBean productSku, selectSkus) { + bool gg = true; + selectSkus.forEach((element1) { + if (productSku.skuNameStr.indexOf(element1) < 0) { + gg = false; + return gg; + } + }); + return gg; } + @override Widget build(BuildContext context) { return StatefulBuilder(builder: (context1, state) { @@ -150,22 +180,10 @@ class _ProductSku extends State { itemBuilder: (context, position) { return attrItem( (index) { - if (tempSelected.attr == null) { - tempSelected.attr = []; - } - - tempSelected.attr.forEach((e0) { - if (e0.attrId == widget.miNiDetail.attrList[position].id) { - String valueId = widget.miNiDetail.attrList[position].attrValueList[index].id; - if (e0.attrValue != valueId) { - tempSelected.attr[tempSelected.attr.indexOf(e0)].attrValue = valueId; - } - } - }); - state(() { selectSkus[position] = widget.miNiDetail .attrList[position].attrValueList[index].attrValue; + buildCount(); }); }, widget.miNiDetail.attrList[position], @@ -192,10 +210,11 @@ class _ProductSku extends State { ), InkWell( onTap: () { - state(() { - if (selectCount > 1) selectCount--; - }); - tempSelected.count = selectCount; + if (count > 1) + setState(() { + count -= 1; + }); + widget.reduce(widget.miNiDetail, selectSkus); }, child: Image.asset( "assets/image/reduce.png", @@ -206,7 +225,7 @@ class _ProductSku extends State { Padding( padding: EdgeInsets.only(left: 8, right: 8), child: Text( - selectCount.toString(), + "$count", style: TextStyle( color: Colors.black, fontSize: 14.sp, @@ -216,10 +235,10 @@ class _ProductSku extends State { ), InkWell( onTap: () { - state(() { - selectCount++; - tempSelected.count = selectCount; + setState(() { + count += 1; }); + widget.add(widget.miNiDetail, selectSkus); }, child: Image.asset( "assets/image/add.png", @@ -242,7 +261,7 @@ class _ProductSku extends State { backgroup: Color(0xFF32A060), fontSize: 16.sp, callback: () { - _result(); + widget.addShopCar(widget.miNiDetail, selectSkus, 1); }, ), SizedBox( @@ -254,25 +273,6 @@ class _ProductSku extends State { }); } - _result() async { - ProductSkuVOListBean productSku = widget.miNiDetail.productSkuVOList.firstWhere((element) { - bool gg = true; - selectSkus.forEach((element1) { - if (element.skuNameStr.indexOf(element1) < 0) { - gg = false; - return gg; - } - }); - return gg; - }); - // print("productSku: ${selectSkus.toString()}"); - print("productSku: ${tempSelected.toJson()}"); - if (productSku == null) return; - tempSelected.skuId = productSku.id; - tempSelected.skuValue = selectSkus.toString(); - widget.addShopCar(tempSelected); - } - Widget attrItem(Function fc, AttrListBean attrListBean, position) { return Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/lib/store/store_view/shop_car.dart b/lib/store/store_view/shop_car.dart index 5432770c..90e7a1da 100644 --- a/lib/store/store_view/shop_car.dart +++ b/lib/store/store_view/shop_car.dart @@ -1,9 +1,6 @@ -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/shoppingCart.dart'; -import 'package:huixiang/store/sku.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'; @@ -13,18 +10,20 @@ class ShopCar extends StatefulWidget { final Function(String id) queryMiNiDetail; final Function() clearShopCar; final Function() toDownOrder; + final Function(ShoppingCartSkuItemListBean cart) shopCartAdd; + final Function(ShoppingCartSkuItemListBean cart) shopCartReduce; - final List shopingCar; - - final Function(int count, String productId, int allCount, double allPrice) fc; + ShoppingCart shopingCar; ShopCar( + Key key, this.shopingCar, this.clearShopCar, this.toDownOrder, - this.fc, { + this.shopCartAdd, + this.shopCartReduce, { this.queryMiNiDetail, - }); + }) : super(key: key); @override State createState() { @@ -33,13 +32,6 @@ class ShopCar extends StatefulWidget { } class _ShopCar extends State { - int allCount = 0; - double allPrice = 0.0; - - @override - void initState() { - super.initState(); - } @override Widget build(BuildContext context) { @@ -81,9 +73,7 @@ class _ShopCar extends State { ), InkWell( onTap: () { - allPrice = 0; - allCount = 0; - + widget.shopingCar.shoppingCartSkuItemList.clear(); state(() {}); widget.clearShopCar(); }, @@ -111,27 +101,17 @@ class _ShopCar extends State { right: 16, ), child: ListView.builder( - itemCount: widget.shopingCar == null - ? 0 - : widget.shopingCar.length, + itemCount: itemCount(), physics: BouncingScrollPhysics(), itemBuilder: (context, position) { - return GestureDetector( - onTap: () {}, - child: ShopGoods( - widget.shopingCar[position], - (int count, List attr, String productId) { - - - widget.fc( - count, productId, allCount, allPrice); - - state(() {}); - }, - count: _shopCarGoods(position).count, - shopCarGoods: _shopCarGoods(position), - queryMiNiDetail: widget.queryMiNiDetail, - ), + return ShopGoods( + widget.shopCartAdd, + widget.shopCartReduce, + count: widget.shopingCar + .shoppingCartSkuItemList[position].buyNum, + shoppingCartSkuItemListBean: + widget.shopingCar.shoppingCartSkuItemList[position], + queryMiNiDetail: widget.queryMiNiDetail, ); }, ), @@ -169,7 +149,10 @@ class _ShopCar extends State { ), ), Text( - "¥" + allPrice.toString(), + "¥" + + (widget.shopingCar == null + ? "0.0" + : widget.shopingCar.cartSum), style: TextStyle( fontSize: 20.sp, fontWeight: MyFontWeight.medium, @@ -215,7 +198,7 @@ class _ShopCar extends State { child: RoundButton( width: 17, height: 17, - text: allCount.toString(), + text: "${count()}", textColor: Colors.white, fontWeight: MyFontWeight.regular, backgroup: Color(0xFF32A060), @@ -243,14 +226,24 @@ class _ShopCar extends State { ); } - Sku _shopCarGoods(int position) { - Sku map = widget.shopCarGoods[position]; - // widget.shopCarGoods.forEach((element) { - // if (widget.productLists[position].id == element.id) { - // map = element; - // } - // }); - // print("shopCarGoods123123: ${map.toJson()}"); - return map; + int itemCount() { + if (widget.shopingCar == null || widget.shopingCar.shoppingCartSkuItemList == null || widget.shopingCar.shoppingCartSkuItemList.length == 0) return 0; + int index = widget.shopingCar.shoppingCartSkuItemList.indexWhere((element) => element.buyNum == 0); + if (index >= 0) { + widget.shopingCar.shoppingCartSkuItemList.removeAt(index); + } + int count = widget.shopingCar.shoppingCartSkuItemList.length; + return count; + } + + int count() { + if (widget.shopingCar == null || + widget.shopingCar.shoppingCartSkuItemList == null || + widget.shopingCar.shoppingCartSkuItemList.length == 0) return 0; + int count = 0; + widget.shopingCar.shoppingCartSkuItemList.forEach((element) { + count += element.buyNum; + }); + return count; } } diff --git a/lib/store/store_view/shop_goods.dart b/lib/store/store_view/shop_goods.dart index 728de675..806db92c 100644 --- a/lib/store/store_view/shop_goods.dart +++ b/lib/store/store_view/shop_goods.dart @@ -1,36 +1,37 @@ import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/findMiNiGroupList.dart'; -import 'package:huixiang/store/sku.dart'; +import 'package:huixiang/retrofit/data/shoppingCart.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/round_button.dart'; class ShopGoods extends StatefulWidget { - final ProductListBean product; + final Function(String id) queryMiNiDetail; - final Function(int count, List attr, String productId) fc; - final Sku shopCarGoods; + final Function(ShoppingCartSkuItemListBean shoppingCartSkuItemListBean) add; + final Function(ShoppingCartSkuItemListBean shoppingCartSkuItemListBean) reduce; + final ProductListBean productListBean; + final ShoppingCartSkuItemListBean shoppingCartSkuItemListBean; final int count; ShopGoods( - this.product, - this.fc, { + this.add, + this.reduce, { + this.productListBean, this.count = 0, this.queryMiNiDetail, - this.shopCarGoods, + this.shoppingCartSkuItemListBean, }); @override State createState() { return _ShopGoods(); } - } class _ShopGoods extends State { - @override Widget build(BuildContext context) { return Container( @@ -47,7 +48,10 @@ class _ShopGoods extends State { children: [ SizedBox(width: 12.w), MImage( - widget.product != null ? widget.product.imgPath : "", + widget.productListBean != null ? widget.productListBean.imgPath : + (widget.shoppingCartSkuItemListBean != null + ? widget.shoppingCartSkuItemListBean.skuImg + : ""), width: 70, height: 70, fit: BoxFit.cover, @@ -62,7 +66,7 @@ class _ShopGoods extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - widget.product.productName, + widget.productListBean != null ? widget.productListBean.productName : widget.shoppingCartSkuItemListBean.productName, style: TextStyle( color: Colors.black, fontSize: 13.sp, @@ -72,11 +76,13 @@ class _ShopGoods extends State { SizedBox( height: 2, ), + ///{"id":"1442420413828169728","productId":"1432652566000304128","productName":"桂花乌龙茶","skuName":null,"buyNum":1,"skuStock":1000,"skuImg":"https://pos.upload.gznl.top/1177/2021/08/f855ffb8-d473-4f5f-93c9-5907b539b2fd.jpg", + ///"skuPrice":"0.00","selected":1,"storeId":null,"groupId":"1426067376096411648","createTime":"2021-10-20 10:27:46","tableId":null,"platterList":null} Row( children: [ Expanded( child: Text( - widget.product.shortName, + (widget.productListBean != null ? widget.productListBean.shortName : widget.shoppingCartSkuItemListBean.skuName ?? ""), overflow: TextOverflow.ellipsis, maxLines: 2, style: TextStyle( @@ -113,7 +119,7 @@ class _ShopGoods extends State { width: 4, ), Text( - "¥${widget.product.price}", + "¥${widget.productListBean != null ? widget.productListBean.price : widget.shoppingCartSkuItemListBean.skuPrice}", style: TextStyle( color: Color(0xFFFF7A1A), fontSize: 11.sp, @@ -136,7 +142,7 @@ class _ShopGoods extends State { width: 4, ), Text( - "¥${widget.product.applyPrice}", + "¥${widget.productListBean != null ? widget.productListBean.applyPrice : widget.shoppingCartSkuItemListBean.skuPrice}", style: TextStyle( color: Color(0xFFA29E9E), fontSize: 10.sp, @@ -159,15 +165,14 @@ class _ShopGoods extends State { fontSize: 11.sp, padding: EdgeInsets.symmetric(vertical: 5.h), callback: () { - widget.queryMiNiDetail(widget.product.id); + widget.queryMiNiDetail( + widget.productListBean != null ? widget.productListBean.id : widget.shoppingCartSkuItemListBean.productId); }, ), if (widget.count > 0) InkWell( onTap: () { - reduceCount(); - widget.fc(widget.shopCarGoods.count, widget.shopCarGoods.attr, widget.product.id); - setState(() {}); + widget.reduce(widget.shoppingCartSkuItemListBean); }, child: Image.asset( "assets/image/reduce.png", @@ -191,9 +196,7 @@ class _ShopGoods extends State { if (widget.count > 0) InkWell( onTap: () { - addCount(); - widget.fc(widget.shopCarGoods.count, widget.shopCarGoods.attr, widget.product.id); - setState(() {}); + widget.add(widget.shoppingCartSkuItemListBean); }, child: Image.asset( "assets/image/add.png", @@ -210,14 +213,4 @@ class _ShopGoods extends State { ), ); } - - addCount() { - widget.shopCarGoods.count += 1; - } - - reduceCount() { - widget.shopCarGoods.count -= 1; - } - - } diff --git a/lib/store/store_view/store_order_list.dart b/lib/store/store_view/store_order_list.dart index b3c5464c..aacfef32 100644 --- a/lib/store/store_view/store_order_list.dart +++ b/lib/store/store_view/store_order_list.dart @@ -8,7 +8,6 @@ import 'package:huixiang/retrofit/data/shoppingCart.dart'; import 'package:huixiang/retrofit/data/store_info.dart'; import 'package:huixiang/retrofit/min_api.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; -import 'package:huixiang/store/sku.dart'; import 'package:huixiang/store/store_view/shop_goods.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -22,8 +21,8 @@ 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; + // final Function(List appletProducts) productListResult; + ShoppingCart shopCarGoods; StoreOrderListPage( this.arguments, @@ -32,7 +31,7 @@ class StoreOrderListPage extends StatefulWidget { this.shopCarGoods, this.controller, this.queryMiNiDetail, - this.productListResult, + // this.productListResult, this.fc); @override @@ -77,7 +76,7 @@ class _StoreOrderListPage extends State { if (baseData != null && baseData.isSuccess) { setState(() { appletProducts = baseData.data; - widget.productListResult(appletProducts); + // widget.productListResult(appletProducts); productListBeans.clear(); if (appletProducts.length > 0) productListBeans.addAll(appletProducts[0].productList); @@ -198,11 +197,12 @@ class _StoreOrderListPage extends State { } int calculateItemCount(FindMiNiGroupList findMiNiGroupList) { + if (widget.shopCarGoods == null) return 0; int count = 0; findMiNiGroupList.productList.forEach((element) { - widget.shopCarGoods.forEach((element1) { - if (element.id == element1.id) { - count += element1.count; + widget.shopCarGoods.shoppingCartSkuItemList.forEach((element1) { + if (element.id == element1.productId) { + count += element1.buyNum; } }); }); @@ -210,46 +210,35 @@ class _StoreOrderListPage extends State { } Widget goodsItem(position) { - Sku map; + ShoppingCartSkuItemListBean shoppingCartSkuItemListBean; int count = 0; - widget.shopCarGoods.forEach((element) { - if (productListBeans[position].id == element.id) { - if (map == null) { - map = element; - count = map.count; - } else { - count += element.count; + if (widget.shopCarGoods != null) { + widget.shopCarGoods.shoppingCartSkuItemList.forEach((element) { + if (productListBeans[position].id == element.productId) { + if (shoppingCartSkuItemListBean == null) { + shoppingCartSkuItemListBean = element; + count = shoppingCartSkuItemListBean.buyNum; + } else { + count += element.buyNum; + } } - } - }); + }); + } return InkWell( onTap: () { widget.queryMiNiDetail(productListBeans[position].id); }, child: ShopGoods( - productListBeans[position], - (int count, List attr, String productId) { - int allCount = 0; - AiDecimalAccuracy allPrice = AiDecimalAccuracy.zero; - widget.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"); - - allPrice += (aiPrice * aiDecimalAccuracy); - } - }); - widget.fc(count, productId, allCount, allPrice.toDouble()); - setState(() {}); + (ShoppingCartSkuItemListBean shoppingCartSkuItemListBean) { + widget.queryMiNiDetail(productListBeans[position].id); + }, + (ShoppingCartSkuItemListBean shoppingCartSkuItemListBean) { + widget.queryMiNiDetail(productListBeans[position].id); }, + productListBean: productListBeans[position], count: count, queryMiNiDetail: widget.queryMiNiDetail, - shopCarGoods: map, + shoppingCartSkuItemListBean: shoppingCartSkuItemListBean, ), ); }