Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ef3886

Browse files
Add sumTotal function
1 parent 6220b07 commit 9ef3886

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

‎lib/pages/cart.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CartPage extends StatefulWidget {
1717
class _CartPageState extends State<CartPage> {
1818
List<dynamic> cartItems = [];
1919
List<int> cartItemCount = [1, 1, 1, 1];
20+
int totalPrice = 0;
2021

2122
Future<void> fetchItems() async {
2223
final String response = await rootBundle.loadString('assets/products.json');
@@ -25,6 +26,14 @@ class _CartPageState extends State<CartPage> {
2526
setState(() {
2627
cartItems = data['products']
2728
.map((data) => Product.fromJson(data)).toList();
29+
30+
sumTotal();
31+
});
32+
}
33+
34+
sumTotal() {
35+
cartItems.forEach((item) {
36+
totalPrice = item.price + totalPrice;
2837
});
2938
}
3039

@@ -85,7 +94,7 @@ class _CartPageState extends State<CartPage> {
8594
mainAxisAlignment: MainAxisAlignment.spaceBetween,
8695
children: <Widget>[
8796
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))
8998
],
9099
),
91100
)),
@@ -180,6 +189,7 @@ class _CartPageState extends State<CartPage> {
180189
setState(() {
181190
if (cartItemCount[index] > 1) {
182191
cartItemCount[index]--;
192+
totalPrice = totalPrice - product.price;
183193
}
184194
});
185195
},
@@ -194,6 +204,7 @@ class _CartPageState extends State<CartPage> {
194204
onPressed: () {
195205
setState(() {
196206
cartItemCount[index]++;
207+
totalPrice = totalPrice + product.price;
197208
});
198209
},
199210
shape: CircleBorder(),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /