Skip to content

Commit 0e12d59

Browse files
committed
cart added
1 parent 14d21f4 commit 0e12d59

File tree

15 files changed

+359
-12
lines changed

15 files changed

+359
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- Modify this file to customize your launch splash screen -->
33
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
4-
<item android:drawable="@android:color/white" />
4+
<item android:drawable="@android:color/black" />
55

66
<!-- You can insert your own image assets here -->
7-
<!-- <item>
7+
<item>
88
<bitmap
99
android:gravity="center"
10-
android:src="@mipmap/launch_image" />
11-
</item> -->
10+
android:src="@mipmap/ic_launcher" />
11+
</item>
1212
</layer-list>
Loading
Loading
Loading
Loading
Loading

lib/main.dart

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import 'package:flutter/material.dart';
22
import 'package:provider/provider.dart';
3+
import 'package:u_shop/providers/cart.dart';
34
import 'package:u_shop/providers/products.dart';
5+
import 'package:u_shop/screens/cart_screen.dart';
6+
import 'package:u_shop/screens/place_order_screen.dart';
47
import 'package:u_shop/screens/product_detail_screen.dart';
58
import 'package:u_shop/screens/product_overview_screen.dart';
9+
import 'package:u_shop/widgets/badge.dart';
610
import 'package:u_shop/widgets/drawer_menu.dart';
711

812
void main() => runApp(MyApp());
@@ -11,8 +15,15 @@ class MyApp extends StatelessWidget {
1115
// This widget is the root of your application.
1216
@override
1317
Widget build(BuildContext context) {
14-
return ChangeNotifierProvider(
15-
create: (_) => Products(),
18+
return MultiProvider(
19+
providers: [
20+
ChangeNotifierProvider.value(
21+
value: Products(),
22+
),
23+
ChangeNotifierProvider.value(
24+
value: Cart(),
25+
),
26+
],
1627
child: MaterialApp(
1728
title: 'UShop',
1829
theme: ThemeData(
@@ -28,6 +39,8 @@ class MyApp extends StatelessWidget {
2839
//register all routes here
2940
routes: {
3041
ProductDetailScreen.routeName: (context) => ProductDetailScreen(),
42+
CartScreen.routeName: (context) => CartScreen(),
43+
PlaceOrderScreen.routeName: (context) => PlaceOrderScreen(),
3144
},
3245
debugShowCheckedModeBanner: false,
3346
),
@@ -48,10 +61,19 @@ class MyHomePage extends StatelessWidget {
4861
@override
4962
Widget build(BuildContext context) {
5063
final productContainer = Provider.of<Products>(context);
64+
final cart = Provider.of<Cart>(context);
5165
return Scaffold(
5266
appBar: AppBar(
53-
//leading: Icon(Icons.menu),
5467
actions: <Widget>[
68+
Badge(
69+
child: IconButton(
70+
icon: Icon(Icons.shopping_cart),
71+
onPressed: () {
72+
Navigator.of(context).pushNamed(CartScreen.routeName);
73+
},
74+
),
75+
value: cart.itemCount.toString(),
76+
),
5577
PopupMenuButton(
5678
onSelected: (_popupValue) {
5779
if (_popupValue == PopupValue.showFavorite) {

lib/providers/cart.dart

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter/material.dart';
2+
3+
class CartItem {
4+
final String id;
5+
final String title;
6+
final int quantity;
7+
final double price;
8+
9+
CartItem({
10+
@required this.id,
11+
@required this.title,
12+
@required this.quantity,
13+
@required this.price,
14+
});
15+
}
16+
17+
class Cart with ChangeNotifier {
18+
Map<String, CartItem> _items = {};
19+
20+
Map<String, CartItem> get items {
21+
return {..._items};
22+
}
23+
24+
int get itemCount {
25+
return _items.length;
26+
}
27+
28+
double get totalAmount {
29+
var total = 0.0;
30+
_items.forEach((key, cartItem) {
31+
total = total + cartItem.price * cartItem.quantity;
32+
});
33+
return total;
34+
}
35+
36+
void addItem(String id, double price, String title) {
37+
if (_items.containsKey(id)) {
38+
_items.update(
39+
id,
40+
(existing) => CartItem(
41+
id: existing.id,
42+
price: existing.price,
43+
quantity: existing.quantity + 1,
44+
title: existing.title,
45+
));
46+
print("$title is added to cart multiple");
47+
} else {
48+
_items.putIfAbsent(
49+
id,
50+
() => CartItem(
51+
id: DateTime.now().toString(),
52+
price: price,
53+
quantity: 1,
54+
title: title,
55+
));
56+
print("$title is added to cart");
57+
}
58+
notifyListeners();
59+
}
60+
61+
void removeItem(String id) {
62+
_items.remove(id);
63+
notifyListeners();
64+
}
65+
}

lib/providers/oders.dart

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:u_shop/providers/cart.dart';
3+
4+
class OrderItem {
5+
final String id;
6+
final double amount;
7+
final List<CartItem> orderedProducts;
8+
final DateTime datetime;
9+
10+
OrderItem({
11+
@required this.id,
12+
@required this.amount,
13+
@required this.orderedProducts,
14+
@required this.datetime,
15+
});
16+
}
17+
18+
class Oders with ChangeNotifier {
19+
List<OrderItem> _orders = [];
20+
21+
List<OrderItem> get orders {
22+
return [..._orders];
23+
}
24+
25+
void addOder(List<CartItem> cartProducts, double total) {
26+
_orders.add(OrderItem(
27+
amount: total,
28+
datetime: DateTime.now(),
29+
id: DateTime.now().toString(),
30+
orderedProducts: cartProducts,
31+
));
32+
33+
notifyListeners();
34+
}
35+
36+
void clearAll() {
37+
_orders = [];
38+
notifyListeners();
39+
}
40+
}

lib/screens/cart_screen.dart

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:provider/provider.dart';
3+
import 'package:u_shop/providers/cart.dart';
4+
import 'package:u_shop/screens/place_order_screen.dart';
5+
6+
class CartScreen extends StatelessWidget {
7+
static const routeName = '/cart-screen';
8+
@override
9+
Widget build(BuildContext context) {
10+
final cart = Provider.of<Cart>(context);
11+
return Scaffold(
12+
appBar: AppBar(
13+
title: Text("Cart"),
14+
centerTitle: true,
15+
),
16+
body: Column(
17+
children: <Widget>[
18+
Container(
19+
padding: EdgeInsets.all(4.0),
20+
child: Card(
21+
child: Padding(
22+
padding: const EdgeInsets.all(8.0),
23+
child: Row(
24+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
25+
children: <Widget>[
26+
Text(
27+
"Total",
28+
style:
29+
TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
30+
),
31+
SizedBox(width: 10),
32+
Chip(
33+
label: Text("${cart.totalAmount}"),
34+
)
35+
],
36+
),
37+
),
38+
),
39+
),
40+
Expanded(
41+
child: ListView.builder(
42+
itemBuilder: (BuildContext context, int index) {
43+
return Dismissible(
44+
child: Card(
45+
child: ListTile(
46+
leading: CircleAvatar(
47+
child: FittedBox(
48+
child: Padding(
49+
padding: const EdgeInsets.all(8.0),
50+
child: Text(
51+
"${cart.items.values.toList()[index].price}"),
52+
)),
53+
),
54+
title: Text("${cart.items.values.toList()[index].title}"),
55+
subtitle: Text(
56+
"Total: ${cart.items.values.toList()[index].price * cart.items.values.toList()[index].quantity}"),
57+
trailing: Text(
58+
"${cart.items.values.toList()[index].quantity} x"),
59+
),
60+
),
61+
key: ValueKey(cart.items.keys.toList()[index]),
62+
direction: DismissDirection.endToStart,
63+
onDismissed: (direction) {
64+
cart.removeItem(cart.items.keys.toList()[index]);
65+
},
66+
background: Container(
67+
padding: EdgeInsets.only(right: 20),
68+
color: Theme.of(context).errorColor,
69+
child: Icon(
70+
Icons.delete,
71+
color: Colors.white,
72+
),
73+
alignment: Alignment.centerRight,
74+
),
75+
);
76+
},
77+
itemCount: cart.itemCount,
78+
),
79+
),
80+
Container(
81+
padding: EdgeInsets.all(8.0),
82+
width: double.infinity,
83+
child: RaisedButton(
84+
color: Theme.of(context).accentColor,
85+
child: Text(
86+
"ORDER NOW",
87+
style: TextStyle(color: Colors.white),
88+
),
89+
onPressed: () {
90+
Navigator.of(context).pushNamed(PlaceOrderScreen.routeName);
91+
},
92+
),
93+
),
94+
],
95+
),
96+
);
97+
}
98+
}

lib/screens/place_order_screen.dart

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/material.dart';
2+
3+
class PlaceOrderScreen extends StatelessWidget {
4+
static const routeName = '/place-order';
5+
@override
6+
Widget build(BuildContext context) {
7+
return Scaffold(
8+
appBar: AppBar(
9+
title: Text("Place Order"),
10+
centerTitle: true,
11+
),
12+
);
13+
}
14+
}

lib/screens/product_detail_screen.dart

+52
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,58 @@ class ProductDetailScreen extends StatelessWidget {
1515
appBar: AppBar(
1616
title: Text(product.title),
1717
),
18+
body: Column(
19+
children: <Widget>[
20+
Container(
21+
height: 300,
22+
width: double.infinity,
23+
child: Hero(
24+
tag: product.id,
25+
child: Image.network(
26+
product.imageUrl,
27+
fit: BoxFit.cover,
28+
alignment: Alignment.topCenter,
29+
),
30+
),
31+
),
32+
Card(
33+
child: Container(
34+
width: double.infinity,
35+
child: Padding(
36+
padding: const EdgeInsets.all(16.0),
37+
child: Row(
38+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
39+
children: <Widget>[
40+
Text(
41+
"Price",
42+
style: TextStyle(
43+
fontSize: 20, fontWeight: FontWeight.bold),
44+
),
45+
Chip(
46+
label: Text(
47+
"\$${product.price}",
48+
style: TextStyle(
49+
fontSize: 20, fontWeight: FontWeight.bold),
50+
),
51+
)
52+
]),
53+
),
54+
),
55+
),
56+
Container(
57+
width: double.infinity,
58+
child: Padding(
59+
padding: const EdgeInsets.all(16.0),
60+
child: Text(
61+
"${product.description}",
62+
style: TextStyle(
63+
fontSize: 17,
64+
),
65+
),
66+
),
67+
),
68+
],
69+
),
1870
);
1971
}
2072
}

0 commit comments

Comments
 (0)