hym_ui/lib/folder_drawer.dart
2024-10-22 01:23:49 -04:00

117 lines
3.1 KiB
Dart

//drawer with the folders for emails a.k.a mailboxes
import 'package:flutter/material.dart';
import 'package:flutter/services.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.add),
title: Text("New Mailbox"),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return NewMailbox(apiService: widget.apiService);
},
);
// Navigator.of(context).pop();
},
),
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
},
),
],
),
);
}
}
class NewMailbox extends StatelessWidget {
final ApiService apiService;
// final Function(String) onFolderCreated;
final TextEditingController _textFieldController = TextEditingController();
NewMailbox({required this.apiService});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Create a new Mailbox"),
content: TextField(
controller: _textFieldController,
decoration: const InputDecoration(
hintText: "EPIC FOLDER", // Your custom hint text here
),
),
actions: <Widget>[
TextButton(
onPressed: () {
print("Folder name: ${_textFieldController.text}");
String folderName = _textFieldController.text;
if (folderName.isNotEmpty) {
apiService.createFolder(folderName);
// onFolderCreated(folderName);
}
// apiService.createFolder(_textFieldController.text);
Navigator.of(context).pop();
},
child: const Text("Approve"),
),
],
);
}
}