From 6d72be2a73eb911410d73b720881c055e808ec59 Mon Sep 17 00:00:00 2001 From: Fatimah Alshammari Date: Mon, 6 Sep 2021 12:22:51 +0300 Subject: [PATCH] fixed cart order issues --- .../pharmacyModule/OrderPreviewViewModel.dart | 21 +++-- .../pharmacies/ProductCheckTypeWidget.dart | 93 ++++++++++++++----- lib/pages/pharmacies/product_detail.dart | 10 +- .../pharmacies/screens/cart-order-page.dart | 19 ++-- lib/widgets/pharmacy/product_tile.dart | 53 ++++++----- 5 files changed, 126 insertions(+), 70 deletions(-) diff --git a/lib/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart b/lib/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart index 1fa34ee2..104dec05 100644 --- a/lib/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart +++ b/lib/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart @@ -55,30 +55,30 @@ class OrderPreviewViewModel extends BaseViewModel { } } - Future changeProductQuantity(ShoppingCart product) async { + Future changeProductQuantity(ShoppingCart product) async { setState(ViewState.Busy); - await _orderService.changeProductQuantity(product.id, product).then((res) { - _handleGetShoppingCartResponse(res); - }); + var resp = await _orderService.changeProductQuantity(product.id, product); + var object = _handleGetShoppingCartResponse(resp); if (_orderService.hasError) { error = _orderService.error; setState(ViewState.ErrorLocal); } else { setState(ViewState.Idle); } + return object; } - Future deleteProduct(ShoppingCart product) async { + Future deleteProduct(ShoppingCart product) async { setState(ViewState.Busy); - await _orderService.deleteProduct(product.id).then((res) { - _handleGetShoppingCartResponse(res); - }); + var resp = await _orderService.deleteProduct(product.id); + var object = _handleGetShoppingCartResponse(resp); if (_orderService.hasError) { error = _orderService.error; setState(ViewState.Error); } else { setState(ViewState.Idle); } + return object; } Future deleteShoppingCart() async { @@ -94,12 +94,12 @@ class OrderPreviewViewModel extends BaseViewModel { } } - _handleGetShoppingCartResponse(Map res) { + ShoppingCartResponse _handleGetShoppingCartResponse(Map res) { totalAdditionalShippingCharge = 0; if (res == null) { error = "response is null"; setState(ViewState.Error); - return; + return null; } print(res); cartResponse.itemCount = res["item_count"]; @@ -115,6 +115,7 @@ class OrderPreviewViewModel extends BaseViewModel { totalAdditionalShippingCharge += shoppingCart.product.additionalShippingCharge; }); cartResponse.updateShoppingCard(); + return cartResponse; } String getPaymentOptionImage(PaymentOption paymentOption) { diff --git a/lib/pages/pharmacies/ProductCheckTypeWidget.dart b/lib/pages/pharmacies/ProductCheckTypeWidget.dart index 878544e1..edecff62 100644 --- a/lib/pages/pharmacies/ProductCheckTypeWidget.dart +++ b/lib/pages/pharmacies/ProductCheckTypeWidget.dart @@ -1,47 +1,67 @@ import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; +import 'package:diplomaticquarterapp/pages/pharmacies/wishlist.dart'; import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart'; import 'package:diplomaticquarterapp/widgets/pharmacy/product_tile.dart'; import 'package:flutter/material.dart'; +import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/product_detail_view_model.dart'; +import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart'; +import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/wishlist_view_model.dart'; +import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart'; AppSharedPreferences sharedPref = AppSharedPreferences(); var languageID; -class ProductCheckTypeWidget extends StatelessWidget { + +class ProductCheckTypeWidget extends StatefulWidget { final List wishlist; final bool isTrue; ProductCheckTypeWidget(this.isTrue, this.wishlist); + @override + _ProductCheckTypeWidgetState createState() => _ProductCheckTypeWidgetState(); +} +class _ProductCheckTypeWidgetState extends State { void initState() { getLanguageID(); } @override Widget build(BuildContext context) { - return isTrue + return widget.isTrue ? ListView.builder( - itemCount: wishlist.length, + itemCount: widget.wishlist.length, itemBuilder: (BuildContext context, int index) { return Column( children: [ Container( - child: isTrue + child: widget.isTrue ? productTile( - productName: languageID == 'ar' ? wishlist[index].product.namen : wishlist[index].product.name, - productPrice: wishlist[index].subtotal, - productRate: - double.parse(wishlist[index].subtotalVatRate), - productImage: wishlist[index].product.images[0].src, - showLine: isTrue, - productID: wishlist[index].product.id, + productName: languageID == 'ar' + ? widget.wishlist[index].product.namen + : widget.wishlist[index].product.name, + productPrice: widget.wishlist[index].subtotal, + productRate: double.parse( + widget.wishlist[index].subtotalVatRate), + productImage: + widget.wishlist[index].product.images[0].src, + showLine: widget.isTrue, + productID: widget.wishlist[index].product.id, + onDelete: deleteWishListItem, + ) : productTile( - productName: languageID == 'ar' ? wishlist[index].product.namen : wishlist[index].product.name, - productPrice: wishlist[index].subtotal, - productRate: - double.parse(wishlist[index].subtotalVatRate), - productImage: wishlist[index].product.images[0].src, - showLine: isTrue, + productName: languageID == 'ar' + ? widget.wishlist[index].product.namen + : widget.wishlist[index].product.name, + productPrice: widget.wishlist[index].subtotal, + productRate: double.parse( + widget.wishlist[index].subtotalVatRate), + productImage: + widget.wishlist[index].product.images[0].src, + showLine: widget.isTrue, + onDelete: deleteWishListItem, + ), ), Divider(height: 1, color: Colors.grey) @@ -51,20 +71,43 @@ class ProductCheckTypeWidget extends StatelessWidget { : GridView.count( crossAxisCount: 2, children: List.generate( - wishlist.length, + widget.wishlist.length, (index) => productTile( - productName: wishlist[index].product.name, - productPrice: wishlist[index].subtotal, - productRate: - double.parse(wishlist[index].subtotalVatRate), - productImage: wishlist[index].product.images[0].src, - showLine: isTrue, + productName: widget.wishlist[index].product.name, + productPrice: widget.wishlist[index].subtotal, + productRate: + double.parse(widget.wishlist[index].subtotalVatRate), + productImage: + widget.wishlist[index].product.images[0].src, + showLine: widget.isTrue, )), ); } + + deleteWishListItem(itemID) async { + ProductDetailViewModel x = new ProductDetailViewModel(); + GifLoaderDialogUtils.showMyDialog(context); + await x.deletWishlistData(itemID); + setState(() + async{ + await + //getWishlistData(); + Navigator.push(context, + FadePage(page: WishlistPage(false)), + ); + }); + GifLoaderDialogUtils.hideDialog(context); + print("Delete"); + } + + + getWishlistData() async { + WishListViewModel x = new WishListViewModel(); + await x.getWishlistData(); + } + } getLanguageID() async { languageID = await sharedPref.getString(APP_LANGUAGE); } - diff --git a/lib/pages/pharmacies/product_detail.dart b/lib/pages/pharmacies/product_detail.dart index b1ec665a..c12ec87d 100644 --- a/lib/pages/pharmacies/product_detail.dart +++ b/lib/pages/pharmacies/product_detail.dart @@ -666,12 +666,14 @@ class __ProductDetailPageState extends State { itemBuilder: (context, index) { return InkWell( onTap: () - { - Navigator.push( + async { + GifLoaderDialogUtils.showMyDialog(context); + await Navigator.push( context, FadePage( - page: ProductDetailPage(widget.product.id), + page: ProductDetailPage(model.recommendedProductList[index]['id']), )); + GifLoaderDialogUtils.hideDialog(context); }, child: Card( elevation: 2, @@ -1982,7 +1984,9 @@ class _productNameAndPriceState extends State { onPressed: () async { if (customerId != null) { if (!isInWishlit) { + GifLoaderDialogUtils.showMyDialog(context); await addToWishlistFunction(widget.item.id); + GifLoaderDialogUtils.hideDialog(context); } else { await deleteFromWishlistFunction(widget.item.id); } diff --git a/lib/pages/pharmacies/screens/cart-order-page.dart b/lib/pages/pharmacies/screens/cart-order-page.dart index 5271eda1..be87ae1d 100644 --- a/lib/pages/pharmacies/screens/cart-order-page.dart +++ b/lib/pages/pharmacies/screens/cart-order-page.dart @@ -26,7 +26,7 @@ class CartOrderPage extends StatelessWidget { return BaseView( onModelReady: (model){ model.getShoppingCart().then((value){ - appScaffold.appBar.badgeUpdater('${model.cartResponse.itemCount ?? 0}'); + appScaffold.appBar.badgeUpdater('${model.cartResponse.quantityCount ?? 0}'); }); }, builder: (_, model, wi) => ChangeNotifierProvider.value( @@ -76,10 +76,10 @@ class CartOrderPage extends StatelessWidget { cart.shoppingCarts[index], () { print(cart.shoppingCarts[index] .quantity); - model - .changeProductQuantity( - cart.shoppingCarts[index]) - .then((value) { + model.changeProductQuantity(cart.shoppingCarts[index]).then((value) { + if (model.state != ViewState.Error) { + appScaffold.appBar.badgeUpdater('${value.quantityCount ?? 0}'); + } if (model.state == ViewState.ErrorLocal) { Utils.showErrorToast( @@ -87,8 +87,13 @@ class CartOrderPage extends StatelessWidget { } }); }, - () => model.deleteProduct( - cart.shoppingCarts[index]))) + () { + model.deleteProduct(cart.shoppingCarts[index]).then((value){ + if (model.state != ViewState.Error) { + appScaffold.appBar.badgeUpdater('${value.quantityCount ?? 0}'); + } + }); + })) ], ), ), diff --git a/lib/widgets/pharmacy/product_tile.dart b/lib/widgets/pharmacy/product_tile.dart index 76e46949..b6f73823 100644 --- a/lib/widgets/pharmacy/product_tile.dart +++ b/lib/widgets/pharmacy/product_tile.dart @@ -24,23 +24,26 @@ class productTile extends StatelessWidget { final int status; final PharmacyProduct product; final dynamic productID; + final Function onDelete; +// final VoidCallback deleteWishlistItems; - productTile({ - this.productName, - this.productPrice, - this.productRate, - this.productReviews, - this.qyt, - this.totalPrice, - this.isOrderDetails = false, - this.productImage, - this.showLine = true, - this.img, - this.imgs, - this.status, - this.product, - this.productID, - }); + productTile( + {this.productName, + this.productPrice, + this.productRate, + this.productReviews, + this.qyt, + this.totalPrice, + this.isOrderDetails = false, + this.productImage, + this.showLine = true, + this.img, + this.imgs, + this.status, + this.product, + this.productID, + this.onDelete, + }); @override Widget build(BuildContext context) { @@ -136,9 +139,10 @@ class productTile extends StatelessWidget { children: [ IconButton( icon: Icon(FontAwesomeIcons.trashAlt, size: 15), - onPressed: (){ + onPressed: () { GifLoaderDialogUtils.showMyDialog(context); - deleteFromWishlistFunction(productID); +// deleteFromWishlistFunction(productID); + onDelete(productID); GifLoaderDialogUtils.hideDialog(context); }, ), @@ -148,14 +152,13 @@ class productTile extends StatelessWidget { IconButton( icon: Icon(FontAwesomeIcons.shoppingCart, size: 15), onPressed: () async { - GifLoaderDialogUtils.showMyDialog(context); + GifLoaderDialogUtils.showMyDialog(context); await addToCartFunction(1, productID); - GifLoaderDialogUtils.hideDialog(context); + GifLoaderDialogUtils.hideDialog(context); Navigator.push( context, FadePage(page: CartOrderPage()), ); - }, ), ], @@ -293,10 +296,10 @@ class productTile extends StatelessWidget { ); } - deleteFromWishlistFunction(itemID) async { - ProductDetailViewModel x = new ProductDetailViewModel(); - await x.deletWishlistData(itemID); - } +// deleteFromWishlistFunction(itemID) async { +// ProductDetailViewModel x = new ProductDetailViewModel(); +// await x.deletWishlistData(itemID); +// } addToCartFunction(quantity, itemID) async { ProductDetailViewModel x = new ProductDetailViewModel();