@@ -17,6 +17,7 @@ class CartPage extends StatefulWidget {
17
17
class _CartPageState extends State <CartPage > {
18
18
List <dynamic > cartItems = [];
19
19
List <int > cartItemCount = [1 , 1 , 1 , 1 ];
20
+ int totalPrice = 0 ;
20
21
21
22
Future <void > fetchItems () async {
22
23
final String response = await rootBundle.loadString ('assets/products.json' );
@@ -25,6 +26,14 @@ class _CartPageState extends State<CartPage> {
25
26
setState (() {
26
27
cartItems = data['products' ]
27
28
.map ((data) => Product .fromJson (data)).toList ();
29
+
30
+ sumTotal ();
31
+ });
32
+ }
33
+
34
+ sumTotal () {
35
+ cartItems.forEach ((item) {
36
+ totalPrice = item.price + totalPrice;
28
37
});
29
38
}
30
39
@@ -85,7 +94,7 @@ class _CartPageState extends State<CartPage> {
85
94
mainAxisAlignment: MainAxisAlignment .spaceBetween,
86
95
children: < Widget > [
87
96
Text ('Total' , style: TextStyle (fontSize: 20 )),
88
- Text ('\$ 200 ' , style: TextStyle (fontSize: 20 , fontWeight: FontWeight .bold))
97
+ Text ('\$ ${ totalPrice + 5.99 } ' , style: TextStyle (fontSize: 20 , fontWeight: FontWeight .bold))
89
98
],
90
99
),
91
100
)),
@@ -180,6 +189,7 @@ class _CartPageState extends State<CartPage> {
180
189
setState (() {
181
190
if (cartItemCount[index] > 1 ) {
182
191
cartItemCount[index]-- ;
192
+ totalPrice = totalPrice - product.price;
183
193
}
184
194
});
185
195
},
@@ -194,6 +204,7 @@ class _CartPageState extends State<CartPage> {
194
204
onPressed: () {
195
205
setState (() {
196
206
cartItemCount[index]++ ;
207
+ totalPrice = totalPrice + product.price;
197
208
});
198
209
},
199
210
shape: CircleBorder (),
0 commit comments