folder_drawer.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //drawer with the folders for emails a.k.a mailboxes
  2. import 'package:flutter/material.dart';
  3. import 'api_service.dart';
  4. // ignore: must_be_immutable
  5. class FolderDrawer extends StatefulWidget {
  6. ApiService apiService;
  7. Function(String) onFolderTap;
  8. FolderDrawer({required this.apiService, required this.onFolderTap});
  9. @override
  10. _FolderDrawerState createState() => _FolderDrawerState();
  11. }
  12. class _FolderDrawerState extends State<FolderDrawer> {
  13. List<String> folders = [];
  14. @override
  15. void initState() {
  16. super.initState();
  17. _fetchFolders();
  18. }
  19. Future<void> _fetchFolders() async {
  20. try {
  21. List<String> fetchedFolders = await widget.apiService.fetchFolders();
  22. setState(() {
  23. folders = fetchedFolders;
  24. });
  25. } catch (e) {
  26. print('Error fetching folders: $e');
  27. }
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Drawer(
  32. child: ListView(
  33. padding: EdgeInsets.zero,
  34. children: [
  35. ...folders.map((folder) {
  36. return ListTile(
  37. leading: Icon(Icons.mail),
  38. title: Text(folder),
  39. trailing: IconButton(
  40. icon: Icon(Icons.more_vert),
  41. onPressed: () => {
  42. ///show options
  43. _showOptions(context)
  44. },
  45. ),
  46. onTap: () {
  47. widget.onFolderTap(folder);
  48. Navigator.pop(
  49. context); // Close the drawer after selecting a folder
  50. },
  51. );
  52. }).toList(),
  53. ListTile(
  54. leading: Icon(Icons.add),
  55. title: Text("New Mailbox"),
  56. onTap: () {
  57. showDialog(
  58. context: context,
  59. builder: (BuildContext context) {
  60. return NewMailbox(apiService: widget.apiService);
  61. },
  62. );
  63. // Navigator.of(context).pop();
  64. },
  65. ),
  66. ListTile(
  67. leading: Icon(Icons.refresh),
  68. title: Text('Refresh Folders'),
  69. onTap: () {
  70. _fetchFolders(); // Refresh folders when this tile is tapped
  71. Navigator.pop(context); // Close the drawer after refresh
  72. },
  73. ),
  74. ],
  75. ),
  76. );
  77. }
  78. void _showOptions(BuildContext context) async {
  79. final RenderBox overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
  80. await showMenu<String>(
  81. context: context,
  82. position: RelativeRect.fromLTRB(100, 100, overlay.size.width, overlay.size.height),
  83. items: <PopupMenuEntry<String>>[
  84. PopupMenuItem<String>(
  85. value: 'Rename',
  86. child: Text('Rename Folder'),
  87. ),
  88. PopupMenuItem<String>(
  89. value: 'Delete',
  90. child: Text('Delete Folder'),
  91. ),
  92. ],
  93. ).then((value) {
  94. // Handle the action based on the selected menu item
  95. if (value == 'Rename') {
  96. // Logic for renaming the folder
  97. print('Rename folder');
  98. } else if (value == 'Delete') {
  99. // Logic for deleting the folder
  100. print('Delete folder');
  101. }
  102. });
  103. }
  104. }
  105. class NewMailbox extends StatelessWidget {
  106. final ApiService apiService;
  107. // final Function(String) onFolderCreated;
  108. final TextEditingController _textFieldController = TextEditingController();
  109. NewMailbox({required this.apiService});
  110. @override
  111. Widget build(BuildContext context) {
  112. return AlertDialog(
  113. title: const Text("Create a new Mailbox"),
  114. content: TextField(
  115. controller: _textFieldController,
  116. decoration: const InputDecoration(
  117. hintText: "EPIC FOLDER", // Your custom hint text here
  118. ),
  119. ),
  120. actions: <Widget>[
  121. TextButton(
  122. onPressed: () {
  123. print("Folder name: ${_textFieldController.text}");
  124. String folderName = _textFieldController.text;
  125. if (folderName.isNotEmpty) {
  126. apiService.createFolder(folderName);
  127. // onFolderCreated(folderName);
  128. }
  129. // apiService.createFolder(_textFieldController.text);
  130. Navigator.of(context).pop();
  131. },
  132. child: const Text("Approve"),
  133. ),
  134. ],
  135. );
  136. }
  137. }