Forráskód Böngészése

make the folder drawer a widget by itself

juan 1 hónapja
szülő
commit
afcb58b152
1 módosított fájl, 64 hozzáadás és 0 törlés
  1. 64 0
      lib/folder_drawer.dart

+ 64 - 0
lib/folder_drawer.dart

@@ -0,0 +1,64 @@
+//drawer with the folders for emails a.k.a mailboxes
+
+import 'package:flutter/material.dart';
+import 'api_service.dart';
+
+class FolderDrawer extends StatefulWidget {
+  ApiService apiService;
+  Function(String) onFolderTap;
+
+  FolderDrawer({required this.apiService, required this.onFolderTap});
+
+  @override
+  _FolderDrawerState createState() => _FolderDrawerState();
+}
+
+class _FolderDrawerState extends State<FolderDrawer> {
+  List<String> folders = [];
+
+  @override
+  void initState() {
+    super.initState();
+    _fetchFolders();
+  }
+
+  Future<void> _fetchFolders() async {
+    try {
+      List<String> fetchedFolders = await widget.apiService.fetchFolders();
+      setState(() {
+        folders = fetchedFolders;
+      });
+    } catch (e) {
+      print('Error fetching folders: $e');
+    }
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return Drawer(
+      child: ListView(
+        padding: EdgeInsets.zero,
+        children: [
+          ...folders.map((folder) {
+            return ListTile(
+              leading: Icon(Icons.mail),
+              title: Text(folder),
+              onTap: () {
+                widget.onFolderTap(folder);
+                Navigator.pop(context); // Close the drawer after selecting a folder
+              },
+            );
+          }).toList(),
+          ListTile(
+            leading: Icon(Icons.refresh),
+            title: Text('Refresh Folders'),
+            onTap: () {
+              _fetchFolders();  // Refresh folders when this tile is tapped
+              Navigator.pop(context); // Close the drawer after refresh
+            },
+          ),
+        ],
+      ),
+    );
+  }
+}