Skip to content

Commit f79a6a6

Browse files
committed
v6.11.0
1 parent 3cbf98e commit f79a6a6

File tree

11 files changed

+160
-51
lines changed

11 files changed

+160
-51
lines changed

LabelStoreMax/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [6.11.0] - 2023-10-08
2+
3+
* Add `eventBus()` to provider
4+
* Add state managed widget `CartQuantity` and `ProductQuantity`
5+
* Pubspec.yaml dependency updates
6+
17
## [6.10.4] - 2023-10-05
28

39
* Update README

LabelStoreMax/lib/app/controllers/product_detail_controller.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import 'package:flutter_app/app/models/cart.dart';
1313
import 'package:flutter_app/app/models/cart_line_item.dart';
1414
import 'package:flutter_app/bootstrap/enums/wishlist_action_enums.dart';
1515
import 'package:flutter_app/bootstrap/helpers.dart';
16+
import 'package:flutter_app/resources/widgets/cart_quantity_widget.dart';
17+
import 'package:flutter_app/resources/widgets/product_quantity_widget.dart';
1618
import 'package:nylo_framework/nylo_framework.dart';
1719
import 'package:woosignal/models/response/product.dart';
1820
import 'package:woosignal/models/response/product_variation.dart'
@@ -37,7 +39,7 @@ class ProductDetailController extends Controller {
3739
}
3840

3941
itemAddToCart(
40-
{required CartLineItem cartLineItem, required Function onSuccess}) async {
42+
{required CartLineItem cartLineItem, Function? onSuccess}) async {
4143
await Cart.getInstance.addToCart(cartLineItem: cartLineItem);
4244
showStatusAlert(
4345
context,
@@ -46,10 +48,13 @@ class ProductDetailController extends Controller {
4648
duration: 1,
4749
icon: Icons.add_shopping_cart,
4850
);
49-
onSuccess();
51+
updateState(CartQuantity.state);
52+
if (onSuccess != null) {
53+
onSuccess();
54+
}
5055
}
5156

52-
addQuantityTapped({required Function onSuccess}) {
57+
addQuantityTapped({Function? onSuccess}) {
5358
if (product!.manageStock != null && product!.manageStock == true) {
5459
if (quantity >= product!.stockQuantity!) {
5560
showToastNotification(context!,
@@ -62,14 +67,26 @@ class ProductDetailController extends Controller {
6267
}
6368
if (quantity != 0) {
6469
quantity++;
65-
onSuccess();
70+
if (onSuccess != null) {
71+
onSuccess();
72+
}
73+
updateState(ProductQuantity.state, data: {
74+
"product_id": product?.id,
75+
"quantity": quantity
76+
});
6677
}
6778
}
6879

69-
removeQuantityTapped({required Function onSuccess}) {
80+
removeQuantityTapped({Function? onSuccess}) {
7081
if ((quantity - 1) >= 1) {
7182
quantity--;
72-
onSuccess();
83+
if (onSuccess != null) {
84+
onSuccess();
85+
}
86+
updateState(ProductQuantity.state, data: {
87+
"product_id": product?.id,
88+
"quantity": quantity
89+
});
7390
}
7491
}
7592

LabelStoreMax/lib/app/providers/event_provider.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class EventProvider implements NyProvider {
55
@override
66
boot(Nylo nylo) async {
77
nylo.addEvents(events);
8+
nylo.addEventBus();
89

910
return nylo;
1011
}

LabelStoreMax/lib/resources/pages/cart_page.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class _CartPageState extends NyState<CartPage> {
110110
Navigator.pushNamed(context, "/checkout");
111111
}
112112

113-
actionIncrementQuantity({required CartLineItem cartLineItem}) {
113+
actionIncrementQuantity({required CartLineItem cartLineItem}) async {
114114
if (cartLineItem.isManagedStock! &&
115115
cartLineItem.quantity + 1 > cartLineItem.stockQuantity!) {
116116
showToastNotification(
@@ -122,24 +122,24 @@ class _CartPageState extends NyState<CartPage> {
122122
);
123123
return;
124124
}
125-
Cart.getInstance
125+
await Cart.getInstance
126126
.updateQuantity(cartLineItem: cartLineItem, incrementQuantity: 1);
127127
cartLineItem.quantity += 1;
128128
setState(() {});
129129
}
130130

131-
actionDecrementQuantity({required CartLineItem cartLineItem}) {
131+
actionDecrementQuantity({required CartLineItem cartLineItem}) async {
132132
if (cartLineItem.quantity - 1 <= 0) {
133133
return;
134134
}
135-
Cart.getInstance
135+
await Cart.getInstance
136136
.updateQuantity(cartLineItem: cartLineItem, incrementQuantity: -1);
137137
cartLineItem.quantity -= 1;
138138
setState(() {});
139139
}
140140

141-
actionRemoveItem({required int index}) {
142-
Cart.getInstance.removeCartItemForIndex(index: index);
141+
actionRemoveItem({required int index}) async {
142+
await Cart.getInstance.removeCartItemForIndex(index: index);
143143
_cartLines.removeAt(index);
144144
showToastNotification(
145145
context,
@@ -151,8 +151,8 @@ class _CartPageState extends NyState<CartPage> {
151151
setState(() {});
152152
}
153153

154-
_clearCart() {
155-
Cart.getInstance.clear();
154+
_clearCart() async {
155+
await Cart.getInstance.clear();
156156
_cartLines = [];
157157
showToastNotification(context,
158158
title: trans("Success"),

LabelStoreMax/lib/resources/pages/product_detail_page.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ class _ProductDetailState extends NyState<ProductDetailPage> {
210210

211211
await widget.controller.itemAddToCart(
212212
cartLineItem: cartLineItem,
213-
onSuccess: () => setState(() => {}));
214-
setState(() {});
213+
);
215214
Navigator.of(context).pop();
216215
}),
217216
],
@@ -268,9 +267,9 @@ class _ProductDetailState extends NyState<ProductDetailPage> {
268267
onViewExternalProduct:
269268
widget.controller.viewExternalProduct,
270269
onAddQuantity: () => widget.controller
271-
.addQuantityTapped(onSuccess: () => setState(() {})),
270+
.addQuantityTapped(),
272271
onRemoveQuantity: () => widget.controller
273-
.removeQuantityTapped(onSuccess: () => setState(() {})),
272+
.removeQuantityTapped(),
274273
product: _product,
275274
quantity: widget.controller.quantity,
276275
)
@@ -297,6 +296,6 @@ class _ProductDetailState extends NyState<ProductDetailPage> {
297296
await widget.controller.itemAddToCart(
298297
cartLineItem: CartLineItem.fromProduct(
299298
quantityAmount: widget.controller.quantity, product: _product!),
300-
onSuccess: () => setState(() {}));
299+
);
301300
}
302301
}

LabelStoreMax/lib/resources/widgets/cart_icon_widget.dart

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010

1111
import 'package:flutter/material.dart';
12-
import 'package:flutter_app/app/models/cart.dart';
13-
import 'package:flutter_app/app/models/cart_line_item.dart';
14-
import 'package:nylo_framework/nylo_framework.dart';
12+
import 'package:flutter_app/resources/widgets/cart_quantity_widget.dart';
1513

1614
class CartIconWidget extends StatefulWidget {
1715
CartIconWidget({Key? key}) : super(key: key);
@@ -37,27 +35,7 @@ class _CartIconWidgetState extends State<CartIconWidget> {
3735
),
3836
Positioned.fill(
3937
child: Align(
40-
child: NyFutureBuilder<List<CartLineItem>>(
41-
future: Cart.getInstance.getCart(),
42-
child: (BuildContext context,
43-
data) {
44-
if (data == null) return SizedBox.shrink();
45-
46-
List<int?> cartItems =
47-
data.map((e) => e.quantity).toList();
48-
String cartValue = "0";
49-
if (cartItems.isNotEmpty) {
50-
cartValue = cartItems
51-
.reduce((value, element) => value! + element!)
52-
.toString();
53-
}
54-
return Text(
55-
cartValue,
56-
style: Theme.of(context).textTheme.bodyMedium,
57-
textAlign: TextAlign.center,
58-
);
59-
},
60-
),
38+
child: CartQuantity(),
6139
alignment: Alignment.topCenter,
6240
),
6341
top: 0,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_app/app/models/cart.dart';
3+
import 'package:flutter_app/app/models/cart_line_item.dart';
4+
import 'package:nylo_framework/nylo_framework.dart';
5+
6+
class CartQuantity extends StatefulWidget {
7+
8+
CartQuantity({Key? key, this.childOfNavBar = false}) : super(key: key);
9+
10+
final bool childOfNavBar;
11+
12+
static String state = "cart_quantity";
13+
14+
@override
15+
_CartQuantityState createState() => _CartQuantityState(childOfNavBar);
16+
}
17+
18+
class _CartQuantityState extends NyState<CartQuantity> {
19+
20+
bool _childOfNavBar = false;
21+
22+
_CartQuantityState(childOfNavBar) {
23+
stateName = CartQuantity.state;
24+
_childOfNavBar = childOfNavBar;
25+
}
26+
27+
@override
28+
init() async {
29+
super.init();
30+
31+
}
32+
33+
@override
34+
stateUpdated(dynamic data) async {
35+
setState(() { });
36+
}
37+
38+
@override
39+
Widget build(BuildContext context) {
40+
return NyFutureBuilder<List<CartLineItem>>(
41+
future: Cart.getInstance.getCart(),
42+
child: (BuildContext context, data) {
43+
if (data == null) {
44+
return SizedBox.shrink();
45+
}
46+
List<int?> cartItems = data.map((e) => e.quantity).toList();
47+
String cartValue = "0";
48+
if (cartItems.isNotEmpty) {
49+
cartValue = cartItems
50+
.reduce((value, element) => value! + element!)
51+
.toString();
52+
}
53+
if (cartValue == "0" && _childOfNavBar == true) {
54+
return SizedBox.shrink();
55+
}
56+
return Text(
57+
cartValue,
58+
style: Theme.of(context).textTheme.bodyMedium,
59+
textAlign: TextAlign.center,
60+
);
61+
},
62+
loading: SizedBox.shrink(),
63+
);
64+
}
65+
}

LabelStoreMax/lib/resources/widgets/product_detail_footer_actions_widget.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:auto_size_text/auto_size_text.dart';
1212
import 'package:flutter/material.dart';
1313
import 'package:flutter_app/bootstrap/helpers.dart';
1414
import 'package:flutter_app/resources/widgets/buttons.dart';
15+
import 'package:flutter_app/resources/widgets/product_quantity_widget.dart';
1516
import 'package:nylo_framework/nylo_framework.dart';
1617
import 'package:woosignal/models/response/product.dart';
1718

@@ -76,10 +77,7 @@ class ProductDetailFooterActionsWidget extends StatelessWidget {
7677
),
7778
onPressed: onRemoveQuantity as void Function()?,
7879
),
79-
Text(
80-
quantity.toString(),
81-
style: Theme.of(context).textTheme.bodyLarge,
82-
),
80+
ProductQuantity(productId: product!.id!),
8381
IconButton(
8482
icon: Icon(
8583
Icons.add_circle_outline,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:nylo_framework/nylo_framework.dart';
3+
4+
class ProductQuantity extends StatefulWidget {
5+
6+
ProductQuantity({Key? key, required this.productId}) : super(key: key);
7+
8+
final int productId;
9+
10+
static String state = "product_quantity";
11+
12+
@override
13+
_ProductQuantityState createState() => _ProductQuantityState(productId);
14+
}
15+
16+
class _ProductQuantityState extends NyState<ProductQuantity> {
17+
18+
int quantity = 1;
19+
late int productId;
20+
21+
_ProductQuantityState(this.productId) {
22+
stateName = ProductQuantity.state;
23+
}
24+
25+
@override
26+
init() async {
27+
super.init();
28+
}
29+
30+
@override
31+
stateUpdated(dynamic data) async {
32+
if (productId != data['product_id']) return;
33+
setState(() {
34+
quantity = data['quantity'];
35+
});
36+
}
37+
38+
@override
39+
Widget build(BuildContext context) {
40+
return Text(
41+
quantity.toString(),
42+
style: Theme.of(context).textTheme.bodyLarge,
43+
);
44+
}
45+
}

LabelStoreMax/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ packages:
505505
dependency: "direct main"
506506
description:
507507
name: html
508-
sha256: "58e3491f7bf0b6a4ea5110c0c688877460d1a6366731155c4a4580e7ded773e8"
508+
sha256: "3a7812d5bcd2894edf53dfaf8cd640876cf6cef50a8f238745c8b8120ea74d3a"
509509
url: "https://pub.dev"
510510
source: hosted
511-
version: "0.15.3"
511+
version: "0.15.4"
512512
http:
513513
dependency: "direct overridden"
514514
description:

0 commit comments

Comments
 (0)