1
+ import 'package:cloud_firestore/cloud_firestore.dart' ;
2
+ import 'package:firebase_auth/firebase_auth.dart' ;
1
3
import 'package:flutter/material.dart' ;
2
4
import 'package:flutter/services.dart' ;
5
+ import '../../../../components/loading.dart' ;
6
+ import '../../../../components/search_box.dart' ;
3
7
import '../../../../constants/colors.dart' ;
8
+ import '../../../../utilities/products_stream_builder.dart' ;
9
+ import '../../product/details.dart' ;
4
10
5
11
class ManageProductsScreen extends StatefulWidget {
6
12
static const routeName = '/manage_products' ;
13
+
7
14
const ManageProductsScreen ({Key ? key}) : super (key: key);
8
15
9
16
@override
@@ -13,6 +20,19 @@ class ManageProductsScreen extends StatefulWidget {
13
20
class _ManageProductsScreenState extends State <ManageProductsScreen > {
14
21
@override
15
22
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
+
16
36
SystemChrome .setSystemUIOverlayStyle (
17
37
SystemUiOverlayStyle (
18
38
statusBarColor: Colors .transparent,
@@ -33,6 +53,158 @@ class _ManageProductsScreenState extends State<ManageProductsScreen> {
33
53
),
34
54
),
35
55
),
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
+ ),
36
208
);
37
209
}
38
210
}
0 commit comments