-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.dart
355 lines (339 loc) · 10.6 KB
/
app.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:ncba_news/app_screens/publishing_portal.dart';
import 'package:ncba_news/app_sections/categories.dart';
import 'package:ncba_news/app_screens/saved.dart';
import 'app_screens/news_editor.dart';
import 'app_screens/signin.dart';
import 'app_sections/feed.dart';
import 'app_sections/profile.dart';
import 'app_sections/admin.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
bool _isAdmin = false;
bool _searchBarToggled = false;
int currentPageIndex = 0;
bool _loading = true;
String _searchQuery = '';
late List<Widget> _pages;
late AnimationController _iconAnimationController;
final TextEditingController _searchController = TextEditingController();
void handleScreenChanged(int selectedScreen) {
setState(() {
currentPageIndex = selectedScreen;
});
Navigator.of(context).pop(); // Close the drawer
}
void openDrawer() {
scaffoldKey.currentState!.openDrawer();
}
@override
void initState() {
super.initState();
_iconAnimationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 300),
);
_checkAdminStatus();
}
@override
void dispose() {
_iconAnimationController.dispose();
super.dispose();
}
void _checkAdminStatus() async {
try {
User? user = _auth.currentUser;
if (user != null) {
// Check if the user has admin custom claim
final idTokenResult = await user.getIdTokenResult();
setState(() {
_isAdmin = idTokenResult.claims?['admin'] ?? false;
_pages = _buildPages();
});
}
} catch (e) {
if (kDebugMode) {
print("Error checking admin status: $e");
}
} finally {
setState(() {
_loading =
false; // Set loading state to false once done checking admin status
});
}
}
_getPageLabel(int index) {
switch (index) {
case 0:
return const Text(
"Home",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
case 1:
return const Text(
"Saved",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
case 2:
return const Text(
"Categories",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
case 3:
return const Text(
"Profile",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
case 4:
return const Text(
"Publishing Portal",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
case 5:
return const Text(
"Admin",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
default:
return const Text(
"Unknown",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 20),
);
}
}
List<Widget> _buildPages() {
List<Widget> pages = [
Feed(searchQuery: _searchQuery),
const Saved(),
const Categories(),
const Profile(),
const PublishingPortal(),
const AdminTabSection(),
];
if (_isAdmin) {
pages.add(const AdminTabSection());
}
return pages;
}
void _signOut() async {
try {
await FirebaseAuth.instance.signOut().then((value) => {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const SignIn()),
(route) => false,
)
});
} catch (e) {
// Handle sign-out errors if necessary
if (kDebugMode) {
print('Error signing out: $e');
}
}
}
void _toggleSearchBar() {
setState(() {
_searchBarToggled = !_searchBarToggled;
if (_searchBarToggled) {
_iconAnimationController.forward();
} else {
_iconAnimationController.reverse();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
elevation: 5,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(30),
),
),
centerTitle: true,
leading: DrawerButton(
onPressed: openDrawer,
),
title: Row(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
'assets/images/ncba_logo.png',
height: 30,
),
const SizedBox(width: 10),
const Text('NCBA&E News'),
],
),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(60),
child: Container(
padding: const EdgeInsets.all(10),
child: _searchBarToggled
? TextFormField(
controller: _searchController,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: const Icon(Icons.search),
onPressed: () {
setState(() {
_searchQuery = _searchController.text;
});
},
),
prefix: const Text("Search: "),
prefixIcon: IconButton(
icon: const Icon(Icons.cancel_outlined),
onPressed: () {
setState(() {
_searchBarToggled = false;
});
},
),
filled: true,
contentPadding: const EdgeInsets.all(9),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none,
),
),
)
: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[400]!),
borderRadius: BorderRadius.circular(30),
),
padding:
const EdgeInsets.symmetric(vertical: 8, horizontal: 30),
child: PopupMenuButton(
itemBuilder: (context) {
return [
const PopupMenuItem(
value: 0,
child: Text("Home"),
),
const PopupMenuItem(
value: 1,
child: Text("Saved"),
),
const PopupMenuItem(
value: 2,
child: Text("Categories"),
),
const PopupMenuItem(
value: 3,
child: Text("Profile"),
),
const PopupMenuItem(
value: 4,
child: Text("Publishing Portal"),
),
if (_isAdmin)
const PopupMenuItem(
value: 5,
child: Text("Admin"),
),
];
},
onSelected: (int value) {
setState(() {
currentPageIndex = value;
});
},
child: _getPageLabel(currentPageIndex),
),
),
),
),
actions: [
IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.search_ellipsis,
progress: _iconAnimationController,
),
onPressed: _toggleSearchBar,
),
],
),
body: Center(
child: _loading
? const CircularProgressIndicator()
: _pages.elementAt(currentPageIndex),
),
drawer: NavigationDrawer(
onDestinationSelected: (int index) {
handleScreenChanged(index);
},
selectedIndex: currentPageIndex,
elevation: 1,
tilePadding: const EdgeInsets.all(10),
children: [
const NavigationDrawerDestination(
icon: Icon(Icons.home_outlined),
label: Text("Home"),
),
const NavigationDrawerDestination(
icon: Icon(Icons.bookmark_outline),
label: Text("Saved"),
),
const NavigationDrawerDestination(
icon: Icon(Icons.category_outlined),
label: Text("Categories"),
),
const NavigationDrawerDestination(
icon: Icon(Icons.person_outline),
label: Text("Profile"),
),
const NavigationDrawerDestination(
icon: Icon(Icons.publish_outlined),
label: Text("Publishing Portal")),
if (_isAdmin)
const NavigationDrawerDestination(
icon: Icon(Icons.admin_panel_settings_outlined),
label: Text("Admin"),
),
const Divider(
indent: 10,
endIndent: 10,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
OutlinedButton.icon(
onPressed: () {
_signOut();
},
label: const Text("Sign Out"),
icon: const Icon(Icons.logout_outlined),
)
],
)
],
),
floatingActionButton: currentPageIndex == 4
? FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NewsEditor()));
},
child: const Icon(Icons.add),
)
: null,
);
}
}