final
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
|
||||
import 'package:badges/badges.dart';
|
||||
import 'package:badges/badges.dart' as badges;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
@@ -17,16 +17,16 @@ import '../../store/store.dart';
|
||||
import '../../utils/utils.dart';
|
||||
|
||||
class AddRemoveButton extends StatefulWidget {
|
||||
final Product product;
|
||||
final Product? product;
|
||||
final Business business;
|
||||
final bool addOnly;
|
||||
final int cartLineItemIndex;
|
||||
final bool addToBasket;
|
||||
final bool onHovor;
|
||||
final bool? addOnly;
|
||||
final int? cartLineItemIndex;
|
||||
final bool? addToBasket;
|
||||
final bool? onHovor;
|
||||
|
||||
AddRemoveButton({
|
||||
this.product,
|
||||
this.business,
|
||||
required this.business,
|
||||
this.addOnly = false,
|
||||
this.cartLineItemIndex = -1,
|
||||
this.addToBasket = false,
|
||||
@@ -40,7 +40,7 @@ class AddRemoveButton extends StatefulWidget {
|
||||
}
|
||||
|
||||
class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
int _qty;
|
||||
int? _qty;
|
||||
var zeroColor = const Color(0xFFEFEFEF);
|
||||
var qtyColor = const Color(0xFFFF6666);
|
||||
var zeroFontColor = const Color(0xFF888888);
|
||||
@@ -48,7 +48,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
|
||||
var d = 1;
|
||||
|
||||
CartInfo cartInfo;
|
||||
CartInfo? cartInfo;
|
||||
|
||||
GlobalKey startKey = GlobalKey();
|
||||
|
||||
@@ -59,14 +59,14 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.product.leftNum == null) {
|
||||
if (widget.product?.leftNum == null) {
|
||||
_qty = 0;
|
||||
cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, widget.business);
|
||||
if (cartInfo != null) {
|
||||
for (var i = 0; i < cartInfo.productList.length; i++) {
|
||||
if (cartInfo.productList[i].product.id == widget.product.id
|
||||
&& cartInfo.productList[i].unitPrice == 0.0) {
|
||||
_qty = cartInfo.productList[i].quantity.round();
|
||||
for (var i = 0; i < cartInfo!.productList!.length; i++) {
|
||||
if (cartInfo!.productList?[i].product?.id == widget.product?.id
|
||||
&& cartInfo!.productList?[i].unitPrice == 0.0) {
|
||||
_qty = cartInfo!.productList?[i].quantity?.round();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
),
|
||||
);
|
||||
}
|
||||
if (widget.product.leftNum <= 0) {
|
||||
if (widget.product!.leftNum! <= 0) {
|
||||
return Container(
|
||||
padding: EdgeInsets.only(top: 0.0, bottom: 10.0, left: 8.0, right: 8.0),
|
||||
child: Text(
|
||||
@@ -100,19 +100,19 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
),
|
||||
);
|
||||
}
|
||||
if (widget.addToBasket) {
|
||||
if (widget.addToBasket!) {
|
||||
_qty = 0;
|
||||
cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, widget.business);
|
||||
if (cartInfo != null) {
|
||||
for (var i = 0; i < cartInfo.productList.length; i++) {
|
||||
if (cartInfo.productList[i].product.id == widget.product.id) {
|
||||
_qty = cartInfo.productList[i].quantity.round();
|
||||
for (var i = 0; i < cartInfo!.productList!.length; i++) {
|
||||
if (cartInfo!.productList?[i].product?.id == widget.product?.id) {
|
||||
_qty = cartInfo!.productList?[i].quantity?.round();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_qty > 0) {
|
||||
return Badge(
|
||||
if (_qty! > 0) {
|
||||
return badges.Badge(
|
||||
badgeContent: Text(
|
||||
'$_qty',
|
||||
style: TextStyle(
|
||||
@@ -120,14 +120,17 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
fontSize: 17.0,
|
||||
),
|
||||
),
|
||||
padding: EdgeInsets.all(10),
|
||||
position: BadgePosition.topEnd(top: -15, end: -10),
|
||||
badgeColor: Colors.lightBlueAccent,
|
||||
badgeStyle: badges.BadgeStyle(
|
||||
padding: EdgeInsets.all(10),
|
||||
badgeColor: Colors.lightBlueAccent,
|
||||
),
|
||||
position: badges.BadgePosition.topEnd(top: -15, end: -10),
|
||||
child: ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
primary: Colors.redAccent,
|
||||
onPrimary: Colors.white,
|
||||
padding: EdgeInsets.only(left: 32, right: 32, top: 20, bottom: 20),
|
||||
backgroundColor: Colors.redAccent,
|
||||
foregroundColor: Colors.white,
|
||||
padding:
|
||||
EdgeInsets.only(left: 32, right: 32, top: 20, bottom: 20),
|
||||
elevation: 2.0,
|
||||
shape: new RoundedRectangleBorder(
|
||||
borderRadius: new BorderRadius.circular(10.0),
|
||||
@@ -154,7 +157,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
} else {
|
||||
return ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
primary: Colors.redAccent,
|
||||
backgroundColor: Colors.redAccent,
|
||||
padding: EdgeInsets.only(left: 32, right: 32, top: 20, bottom: 20),
|
||||
elevation: 2.0,
|
||||
shape: new RoundedRectangleBorder(
|
||||
@@ -179,7 +182,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
},
|
||||
);
|
||||
}
|
||||
} else if (widget.addOnly) {
|
||||
} else if (widget.addOnly!) {
|
||||
return Container(
|
||||
key: startKey,
|
||||
padding: EdgeInsets.all(0.0),
|
||||
@@ -197,15 +200,15 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
_qty = 0;
|
||||
cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, widget.business);
|
||||
if (cartInfo != null) {
|
||||
for (var i = 0; i < cartInfo.productList.length; i++) {
|
||||
if (cartInfo.productList[i].product.id == widget.product.id) {
|
||||
_qty = cartInfo.productList[i].quantity.round();
|
||||
for (var i = 0; i < cartInfo!.productList!.length; i++) {
|
||||
if (cartInfo!.productList?[i].product?.id == widget.product?.id) {
|
||||
_qty = cartInfo!.productList?[i].quantity?.round();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (widget.product.productAttributes != null &&
|
||||
widget.product.productAttributes.length > 0 && widget.cartLineItemIndex == -1) {
|
||||
if (widget.product!.productAttributes != null &&
|
||||
widget.product!.productAttributes!.length > 0 && widget.cartLineItemIndex == -1) {
|
||||
return new Row(
|
||||
key: startKey,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
@@ -219,13 +222,13 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
style: new TextStyle(
|
||||
fontSize: 13.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: _qty > 0 ? qtyFontColor : zeroFontColor
|
||||
color: _qty! > 0 ? qtyFontColor : zeroFontColor
|
||||
),
|
||||
),
|
||||
),
|
||||
padding: EdgeInsets.all(5.0).copyWith(left: 10.0, right: 10.0),
|
||||
decoration: BoxDecoration(
|
||||
color: _qty > 0 ? qtyColor : zeroColor,
|
||||
color: _qty! > 0 ? qtyColor : zeroColor,
|
||||
shape: BoxShape.rectangle,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(10.0),
|
||||
@@ -242,7 +245,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
],
|
||||
);
|
||||
} else {
|
||||
if (!widget.onHovor && _qty == 0) {
|
||||
if (!widget.onHovor! && _qty == 0) {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
return new Row(
|
||||
@@ -258,13 +261,13 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
style: new TextStyle(
|
||||
fontSize: 13.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: _qty > 0 ? qtyFontColor : zeroFontColor
|
||||
color: _qty! > 0 ? qtyFontColor : zeroFontColor
|
||||
),
|
||||
),
|
||||
),
|
||||
padding: EdgeInsets.all(5.0).copyWith(left: 10.0, right: 10.0),
|
||||
decoration: BoxDecoration(
|
||||
color: _qty > 0 ? qtyColor : zeroColor,
|
||||
color: _qty! > 0 ? qtyColor : zeroColor,
|
||||
shape: BoxShape.rectangle,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(10.0),
|
||||
@@ -299,7 +302,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
if (_qty > 0) {
|
||||
if (_qty! > 0) {
|
||||
_removeFromCart(context);
|
||||
}
|
||||
},
|
||||
@@ -312,7 +315,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
|
||||
void _addToCart(BuildContext context) {
|
||||
if (widget.cartLineItemIndex != -1) {
|
||||
if (cartInfo.productList[widget.cartLineItemIndex].quantity + 1.0 > widget.product.leftNum) {
|
||||
if (cartInfo!.productList![widget.cartLineItemIndex!].quantity! + 1.0 > widget.product!.leftNum!) {
|
||||
Fluttertoast.showToast(
|
||||
msg: S.of(context).product_insufficient,
|
||||
toastLength: Toast.LENGTH_SHORT,
|
||||
@@ -321,23 +324,23 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
textColor: Colors.white
|
||||
);
|
||||
} else {
|
||||
cartInfo.productList[widget.cartLineItemIndex].quantity += 1.0;
|
||||
Utils.addSubproductQty(cartInfo, cartInfo.productList[widget.cartLineItemIndex]);
|
||||
cartInfo!.productList![widget.cartLineItemIndex!].quantity = cartInfo!.productList![widget.cartLineItemIndex!].quantity! + 1.0;
|
||||
Utils.addSubproductQty(cartInfo!, cartInfo!.productList![widget.cartLineItemIndex!]);
|
||||
store.dispatch(UpdateCartInfo(
|
||||
Utils.addCartInfoToCartInfoList(store.state.cartInfos, cartInfo)));
|
||||
Utils.addCartInfoToCartInfoList(store.state.cartInfos, cartInfo!)));
|
||||
eventBus.fire(new OnCartInfoUpdated());
|
||||
}
|
||||
} else {
|
||||
if (widget.product.productAttributes.length > 0) {
|
||||
if (widget.product!.productAttributes!.length > 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) =>
|
||||
new AttributeSelection(
|
||||
product: widget.product, business: widget.business, startKey: startKey,)),
|
||||
product: widget.product!, business: widget.business, startKey: startKey,)),
|
||||
);
|
||||
} else {
|
||||
eventBus.fire(new OnProductWillAddToCart(widget.product, {},
|
||||
widget.product.price, widget.product.description,
|
||||
eventBus.fire(new OnProductWillAddToCart(widget.product!, {},
|
||||
widget.product!.price!, widget.product!.description!,
|
||||
widget.business, buttonKey: startKey));
|
||||
}
|
||||
}
|
||||
@@ -345,7 +348,7 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
|
||||
void _removeFromCart(BuildContext context) {
|
||||
if (widget.cartLineItemIndex != -1) {
|
||||
if (cartInfo.productList[widget.cartLineItemIndex].quantity <= 1) {
|
||||
if (cartInfo!.productList![widget.cartLineItemIndex!].quantity! <= 1) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
@@ -375,23 +378,23 @@ class AddRemoveButtonState extends State<AddRemoveButton> {
|
||||
}
|
||||
} else {
|
||||
eventBus.fire(new OnProductWillRemoveFromCart(
|
||||
widget.product, -1, widget.business));
|
||||
widget.product!, -1, widget.business));
|
||||
}
|
||||
}
|
||||
|
||||
void _removeCartLineItem() {
|
||||
if (cartInfo.productList[widget.cartLineItemIndex].quantity <= 1) {
|
||||
String uuid = cartInfo.productList[widget.cartLineItemIndex].uuid;
|
||||
cartInfo.productList.removeAt(widget.cartLineItemIndex);
|
||||
Utils.removeSubproduct(cartInfo, uuid);
|
||||
if (cartInfo!.productList![widget.cartLineItemIndex!].quantity! <= 1) {
|
||||
String uuid = cartInfo!.productList![widget.cartLineItemIndex!].uuid!;
|
||||
cartInfo!.productList?.removeAt(widget.cartLineItemIndex!);
|
||||
Utils.removeSubproduct(cartInfo!, uuid);
|
||||
} else {
|
||||
cartInfo.productList[widget.cartLineItemIndex].quantity -= 1;
|
||||
Utils.addSubproductQty(cartInfo, cartInfo.productList[widget.cartLineItemIndex], remove: true);
|
||||
cartInfo!.productList![widget.cartLineItemIndex!].quantity = cartInfo!.productList![widget.cartLineItemIndex!].quantity! - 1;
|
||||
Utils.addSubproductQty(cartInfo!, cartInfo!.productList![widget.cartLineItemIndex!], remove: true);
|
||||
}
|
||||
if (cartInfo.productList.length <= 0) {
|
||||
if (cartInfo!.productList!.length <= 0) {
|
||||
store.dispatch(new UpdateCartInfo(Utils.removeCartInfoFromCartInfoList(store.state.cartInfos, cartInfo)));
|
||||
} else {
|
||||
store.dispatch(new UpdateCartInfo(Utils.addCartInfoToCartInfoList(store.state.cartInfos, cartInfo)));
|
||||
store.dispatch(new UpdateCartInfo(Utils.addCartInfoToCartInfoList(store.state.cartInfos, cartInfo!)));
|
||||
}
|
||||
eventBus.fire(new OnCartInfoUpdated());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user