Skip to content

Commit 315643e

Browse files
committed
task: product management section
1 parent 068fa08 commit 315643e

File tree

6 files changed

+181
-2
lines changed

6 files changed

+181
-2
lines changed

lib/models/cart.dart

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class CartItem {
22
final String id;
33
final dynamic docId;
4+
final String sellerId;
45
final String prodId;
56
final String prodName;
67
final String prodImgUrl;
@@ -11,6 +12,7 @@ class CartItem {
1112
CartItem({
1213
required this.id,
1314
required this.docId,
15+
required this.sellerId,
1416
required this.prodId,
1517
required this.prodName,
1618
required this.prodPrice,

lib/providers/cart.dart

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CartData extends ChangeNotifier {
3737
id: DateTime.now().toString(),
3838
docId: cart.docId,
3939
prodId: cart.prodId,
40+
sellerId: cart.sellerId,
4041
prodName: cart.prodName,
4142
prodPrice: cart.prodPrice,
4243
prodImgUrl: cart.prodImgUrl,

lib/utilities/products_stream_builder.dart

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ProductStreamBuilder extends StatelessWidget {
3131
void addToCart(
3232
var docId,
3333
var prodId,
34+
var sellerId,
3435
var prodName,
3536
var prodPrice,
3637
var prodImgUrl,
@@ -40,6 +41,7 @@ class ProductStreamBuilder extends StatelessWidget {
4041
id: '',
4142
docId: docId,
4243
prodId: prodId,
44+
sellerId: sellerId,
4345
prodName: prodName,
4446
prodPrice: double.parse(prodPrice),
4547
prodImgUrl: prodImgUrl,
@@ -193,6 +195,7 @@ class ProductStreamBuilder extends StatelessWidget {
193195
addToCart(
194196
data.id,
195197
data['prod_id'],
198+
data['seller_id'],
196199
data['title'],
197200
data['price'],
198201
data['images'][0],

lib/views/main/product/details.dart

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class _DetailsScreenState extends State<DetailsScreen>
146146
id: '',
147147
docId: product.id,
148148
prodId: product['prod_id'],
149+
sellerId: product['seller_id'],
149150
prodName: product['title'],
150151
prodPrice: double.parse(product['price']),
151152
prodImgUrl: product['images'][0],

lib/views/main/seller/dashboard_screens/manage_products.dart

+172
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:firebase_auth/firebase_auth.dart';
13
import 'package:flutter/material.dart';
24
import 'package:flutter/services.dart';
5+
import '../../../../components/loading.dart';
6+
import '../../../../components/search_box.dart';
37
import '../../../../constants/colors.dart';
8+
import '../../../../utilities/products_stream_builder.dart';
9+
import '../../product/details.dart';
410

511
class ManageProductsScreen extends StatefulWidget {
612
static const routeName = '/manage_products';
13+
714
const ManageProductsScreen({Key? key}) : super(key: key);
815

916
@override
@@ -13,6 +20,19 @@ class ManageProductsScreen extends StatefulWidget {
1320
class _ManageProductsScreenState extends State<ManageProductsScreen> {
1421
@override
1522
Widget build(BuildContext context) {
23+
final firebase = FirebaseFirestore.instance;
24+
final userId = FirebaseAuth.instance.currentUser!.uid;
25+
26+
// remove product
27+
void removeProduct(String id) {
28+
firebase.collection('products').doc(id).delete();
29+
}
30+
31+
Stream<QuerySnapshot> productStream = firebase
32+
.collection('products')
33+
.where('seller_id', isEqualTo: userId)
34+
.snapshots();
35+
1636
SystemChrome.setSystemUIOverlayStyle(
1737
SystemUiOverlayStyle(
1838
statusBarColor: Colors.transparent,
@@ -33,6 +53,158 @@ class _ManageProductsScreenState extends State<ManageProductsScreen> {
3353
),
3454
),
3555
),
56+
body: SingleChildScrollView(
57+
child: Padding(
58+
padding: const EdgeInsets.symmetric(horizontal: 10.0),
59+
child: SizedBox(
60+
height: MediaQuery.of(context).size.height / 1.2,
61+
child: StreamBuilder<QuerySnapshot>(
62+
stream: productStream,
63+
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
64+
if (snapshot.hasError) {
65+
return const Center(
66+
child: Text('An error occurred ): '),
67+
);
68+
}
69+
70+
if (snapshot.connectionState == ConnectionState.waiting) {
71+
return const Center(
72+
child: Loading(
73+
color: primaryColor,
74+
kSize: 30,
75+
),
76+
);
77+
}
78+
if (snapshot.data!.docs.isEmpty) {
79+
return Center(
80+
child: Column(
81+
mainAxisAlignment: MainAxisAlignment.center,
82+
crossAxisAlignment: CrossAxisAlignment.center,
83+
children: [
84+
Image.asset(
85+
'assets/images/sad.png',
86+
width: 150,
87+
),
88+
const SizedBox(height: 10),
89+
const Text(
90+
'No data available!',
91+
style: TextStyle(
92+
color: primaryColor,
93+
),
94+
)
95+
],
96+
),
97+
);
98+
}
99+
100+
return ListView.builder(
101+
itemCount: snapshot.data!.docs.length,
102+
itemBuilder: (context, index) {
103+
var item = snapshot.data!.docs[index];
104+
return GestureDetector(
105+
onTap: () => Navigator.of(context).push(
106+
MaterialPageRoute(
107+
builder: (context) => DetailsScreen(product: item.id),
108+
),
109+
),
110+
child: Dismissible(
111+
onDismissed: (direction) => removeProduct(item.id),
112+
direction: DismissDirection.endToStart,
113+
background: Container(
114+
height: 115,
115+
decoration: BoxDecoration(
116+
borderRadius: BorderRadius.circular(10),
117+
color: Colors.red,
118+
),
119+
alignment: Alignment.centerRight,
120+
padding: const EdgeInsets.only(right: 20),
121+
child: const Icon(
122+
Icons.delete_forever,
123+
color: Colors.white,
124+
size: 40,
125+
),
126+
),
127+
confirmDismiss: (direction) => showDialog(
128+
context: context,
129+
builder: (context) => AlertDialog(
130+
title: Text('Remove ${item['title']}'),
131+
content: Text(
132+
'Are you sure you want to remove ${item['title']} from your products?',
133+
),
134+
actions: [
135+
ElevatedButton(
136+
style: ElevatedButton.styleFrom(
137+
primary: primaryColor,
138+
shape: RoundedRectangleBorder(
139+
borderRadius: BorderRadius.circular(10),
140+
),
141+
),
142+
onPressed: () =>
143+
Navigator.of(context).pop(true),
144+
child: const Text(
145+
'Yes',
146+
style: TextStyle(
147+
color: Colors.white,
148+
),
149+
),
150+
),
151+
ElevatedButton(
152+
style: ElevatedButton.styleFrom(
153+
primary: primaryColor,
154+
shape: RoundedRectangleBorder(
155+
borderRadius: BorderRadius.circular(10),
156+
),
157+
),
158+
onPressed: () =>
159+
Navigator.of(context).pop(false),
160+
child: const Text(
161+
'Cancel',
162+
style: TextStyle(
163+
color: Colors.white,
164+
),
165+
),
166+
),
167+
],
168+
),
169+
),
170+
key: ValueKey(item.id),
171+
child: Card(
172+
child: ListTile(
173+
contentPadding: const EdgeInsets.only(
174+
left: 10,
175+
right: 10,
176+
top: 5,
177+
),
178+
leading: CircleAvatar(
179+
backgroundColor: primaryColor,
180+
radius: 35,
181+
backgroundImage: NetworkImage(item['images'][0]),
182+
),
183+
title: Text(
184+
item['title'],
185+
style: const TextStyle(
186+
fontSize: 16,
187+
),
188+
),
189+
subtitle: Text('\$${item['price']}'),
190+
trailing: IconButton(
191+
onPressed: () => {},
192+
icon: const Icon(
193+
Icons.edit_note,
194+
color: primaryColor,
195+
),
196+
),
197+
),
198+
),
199+
),
200+
);
201+
},
202+
);
203+
},
204+
),
205+
),
206+
),
207+
),
36208
);
37209
}
38210
}

lib/views/splash/entry.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class _EntryScreenState extends State<EntryScreen> {
4545
// home screen
4646
//TODO: Check for Customer or Seller Account
4747
Navigator.of(context).pushNamedAndRemoveUntil(
48-
CustomerBottomNav.routeName,
49-
// SellerBottomNav.routeName,
48+
// CustomerBottomNav.routeName,
49+
SellerBottomNav.routeName,
5050
(route) => false,
5151
);
5252
} else {

0 commit comments

Comments
 (0)