import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_wisetronic/events/eventbus.dart'; import 'package:flutter_wisetronic/events/events.dart'; import 'package:flutter_wisetronic/generated/l10n.dart'; import 'package:flutter_wisetronic/models/cart_info.dart'; import 'package:flutter_wisetronic/models/cart_line_item.dart'; import 'package:flutter_wisetronic/models/product.dart'; import 'package:flutter_wisetronic/utils/utils.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:responsive_builder/responsive_builder.dart'; import '../constants.dart'; import '../store/actions.dart'; import '../store/store.dart'; import '../widgets/desktop/desktop_shop.dart'; import '../widgets/mobile/mobile_shop.dart'; class Shop extends StatefulWidget { final int businessId; const Shop({this.businessId = Constants.BUSINESS_ID, Key key}) : super(key: key); @override State createState() => ShopState(); } class ShopState extends State { StreamSubscription onProductWillAddToCartSubscription; StreamSubscription onProductWillRemoveFromCartSubscription; @override Widget build(BuildContext context) { store.dispatch(UpdateContext(context)); return ResponsiveBuilder( builder: (context, sizingInformation) => ScreenTypeLayout( mobile: MobileShop(businessId: widget.businessId,), tablet: DesktopShop(businessId: widget.businessId,), desktop: DesktopShop(businessId: widget.businessId,), ), ); } @override void dispose() { onProductWillAddToCartSubscription?.cancel(); onProductWillRemoveFromCartSubscription?.cancel(); super.dispose(); print("shop parent dispose"); } @override void initState() { super.initState(); onProductWillAddToCartSubscription = eventBus.on().listen((event) { if (mounted) { CartInfo cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, event.business); if (cartInfo == null || cartInfo.productList.length == 0) { cartInfo = CartInfo(); cartInfo.id = 0; cartInfo.amountPaid = 0.0; cartInfo.businessInfo = event.business; cartInfo.extraFeeList = []; cartInfo.discountList = []; CartLineItem lineItem = Utils.newCartLineItem( id: 0, price: event.price, product: event.product, name: event.product.name, description: event.description, quantity: 1.0); cartInfo.productList = [lineItem]; Utils.addSubproductToCard(cartInfo, lineItem); store.dispatch(new UpdateCartInfo(Utils.addCartInfoToCartInfoList( store.state.cartInfos, cartInfo))); eventBus.fire(new OnCartInfoUpdated()); } else { if (event.product.productAttributes.length > 0) { CartLineItem lineItem = Utils.newCartLineItem( id: 0, price: event.price, product: event.product, name: event.product.name, description: event.description, quantity: 1.0); cartInfo.productList.add(lineItem); Utils.addSubproductToCard(cartInfo, lineItem); store.dispatch(new UpdateCartInfo(Utils.addCartInfoToCartInfoList( store.state.cartInfos, cartInfo))); eventBus.fire(new OnCartInfoUpdated()); } else { int found = -1; for (var i = 0; i < cartInfo.productList.length; i++) { if (event.product.id == cartInfo.productList[i].product.id) { found = i; break; } } if (found == -1) { CartLineItem lineItem = Utils.newCartLineItem( id: 0, price: event.price, product: event.product, name: event.product.name, description: event.description, quantity: 1.0); cartInfo.productList.add(lineItem); Utils.addSubproductToCard(cartInfo, lineItem); store.dispatch(new UpdateCartInfo(Utils.addCartInfoToCartInfoList( store.state.cartInfos, cartInfo))); eventBus.fire(new OnCartInfoUpdated()); } else { if (cartInfo.productList[found].quantity + 1.0 > event.product.leftNum) { Fluttertoast.showToast( msg: S.of(context).product_insufficient, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, backgroundColor: Colors.red, textColor: Colors.white); } else { cartInfo.productList[found].quantity += 1; Utils.addSubproductQty(cartInfo, cartInfo.productList[found]); store.dispatch(new UpdateCartInfo( Utils.addCartInfoToCartInfoList( store.state.cartInfos, cartInfo))); eventBus.fire(new OnCartInfoUpdated()); } } } } } }); onProductWillRemoveFromCartSubscription = eventBus.on().listen((event) { if (mounted) { CartInfo cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, event.business); if (cartInfo != null) { if (cartInfo.productList.length > 0) { if (event.productListIndex != -1) { if (cartInfo.productList[event.productListIndex].quantity <= 1) { String uuid = cartInfo.productList[event.productListIndex].uuid; cartInfo.productList.removeAt(event.productListIndex); Utils.removeSubproduct(cartInfo, uuid); } else { cartInfo.productList[event.productListIndex].quantity -= 1; Utils.addSubproductQty(cartInfo, cartInfo.productList[event.productListIndex], remove: true); } } else { int productListIndex = -1; for (var i = 0; i < cartInfo.productList.length; i++) { if (cartInfo.productList[i].product.id == event.product.id) { productListIndex = i; break; } } if (productListIndex != -1) { if (cartInfo.productList[productListIndex].quantity <= 1) { String uuid = cartInfo.productList[productListIndex].uuid; cartInfo.productList.removeAt(productListIndex); Utils.removeSubproduct(cartInfo, uuid); } else { cartInfo.productList[productListIndex].quantity -= 1; Utils.addSubproductQty(cartInfo, cartInfo.productList[productListIndex], remove: true); } } } } if (cartInfo.productList.length <= 0) { store.dispatch(UpdateCartInfo(Utils.removeCartInfoFromCartInfoList( store.state.cartInfos, cartInfo))); } else { store.dispatch(UpdateCartInfo(Utils.addCartInfoToCartInfoList( store.state.cartInfos, cartInfo))); } eventBus.fire(new OnCartInfoUpdated()); } } }); } }