Skip to content

Commit 6184cd7

Browse files
committed
feat: update list menu and cart oage
1 parent 62f7f06 commit 6184cd7

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

lib/components/coffee_tile.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ class CoffeeTile extends StatelessWidget {
55
final Coffee coffee;
66

77
void Function()? onPressed;
8+
final Widget icon;
89

9-
CoffeeTile({super.key, required this.coffee, required this.onPressed});
10+
CoffeeTile({
11+
super.key,
12+
required this.coffee,
13+
required this.onPressed,
14+
required this.icon
15+
});
1016

1117
@override
1218
Widget build(BuildContext context) {
@@ -22,7 +28,7 @@ class CoffeeTile extends StatelessWidget {
2228
subtitle: Text(coffee.price),
2329
leading: Image.asset(coffee.imagePath),
2430
trailing: IconButton(
25-
icon: const Icon(Icons.add),
31+
icon: icon,
2632
onPressed: onPressed,
2733
),
2834
),

lib/pages/cart_page.dart

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import 'package:flutter/material.dart';
2+
import 'package:ngopskuy/components/coffee_tile.dart';
3+
import 'package:ngopskuy/models/coffee.dart';
4+
import 'package:ngopskuy/models/coffee_shop.dart';
5+
import 'package:provider/provider.dart';
26

37
class CartPage extends StatefulWidget {
48
const CartPage({super.key});
@@ -8,10 +12,64 @@ class CartPage extends StatefulWidget {
812
}
913

1014
class _CartPageState extends State<CartPage> {
15+
void removeFromCart(Coffee coffee) {
16+
Provider.of<CoffeeShop>(context, listen: false).removeItemFromCart(coffee);
17+
}
18+
19+
void payNow() {
20+
21+
}
22+
1123
@override
1224
Widget build(BuildContext context) {
13-
return Center(
14-
child: Text('CART'),
25+
return Consumer<CoffeeShop>(
26+
builder: (context, value, child) => SafeArea(
27+
child: Padding(
28+
padding: const EdgeInsets.all(25.0),
29+
child: Column(
30+
children: [
31+
// heading
32+
const Text(
33+
"Your Cart",
34+
style: TextStyle(fontSize: 20),
35+
),
36+
37+
// list of cart items
38+
Expanded(
39+
child: ListView.builder(
40+
itemCount: value.userCart.length,
41+
itemBuilder: (context, index) {
42+
// get individual cart items
43+
Coffee eachCoffee = value.userCart[index];
44+
45+
// return coffee tile
46+
return CoffeeTile(
47+
coffee: eachCoffee,
48+
onPressed: () => removeFromCart(eachCoffee),
49+
icon: const Icon(Icons.remove_circle_outline,
50+
color: Colors.red),
51+
);
52+
})),
53+
54+
// pay button
55+
GestureDetector(
56+
onTap: payNow,
57+
child: Container(
58+
padding: EdgeInsets.all(25),
59+
width: double.infinity,
60+
decoration: BoxDecoration(
61+
color: Colors.brown,
62+
borderRadius: BorderRadius.circular(12),
63+
),
64+
child: const Center(
65+
child: Text(
66+
"Pay Now",
67+
style: TextStyle(color: Colors.white),
68+
),
69+
)),
70+
)
71+
],
72+
))),
1573
);
1674
}
1775
}

lib/pages/shop_page.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class ShopPage extends StatefulWidget {
1414
class _ShopPageState extends State<ShopPage> {
1515
// add coffee to cart
1616
void addToCart(Coffee coffee) {
17+
// add to cart
1718
Provider.of<CoffeeShop>(context, listen: false).addItemToCart(coffee);
19+
20+
// let user know it add beem successfully added
21+
showDialog(context: context, builder: (context) => AlertDialog(title: Text("Successfully added to cart")));
1822
}
1923

2024
@override
@@ -44,6 +48,7 @@ class _ShopPageState extends State<ShopPage> {
4448
return CoffeeTile(
4549
coffee: eachCoffee,
4650
onPressed: () => addToCart(eachCoffee),
51+
icon: const Icon(Icons.add_circle_outline, color: Colors.green),
4752
);
4853
}))
4954
],

0 commit comments

Comments
 (0)