email.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'package:flutter/material.dart';
  2. import 'api_service.dart';
  3. import 'structs.dart';
  4. class EmailListScreen extends StatelessWidget {
  5. final List<GetThreadResponse> emails;
  6. final Future<String> Function(List<String>) getEmailContent;
  7. EmailListScreen({required this.emails, required this.getEmailContent});
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. body: ListView.separated(
  12. itemCount: emails.length,
  13. itemBuilder: (context, index) {
  14. final email = emails[index];
  15. return ListTile(
  16. title: Text(email.from_name,
  17. style: TextStyle(fontWeight: FontWeight.bold)),
  18. subtitle: Column(
  19. crossAxisAlignment: CrossAxisAlignment.start,
  20. children: [Text(email.subject)],
  21. ),
  22. trailing: Text(email.date.toString()),
  23. onTap: () async {
  24. String emailContent = await getEmailContent(email.messages);
  25. Navigator.push(
  26. context,
  27. MaterialPageRoute(
  28. builder: (context) => EmailView(
  29. emailContent: emailContent,
  30. from: email.from_address,
  31. name: email.from_name,
  32. to: email.to.toString(),
  33. subject: email.subject,
  34. date: email.date.toString(),
  35. id: email.id.toString(),
  36. ),
  37. ),
  38. );
  39. },
  40. );
  41. },
  42. separatorBuilder: (context, index) => Divider(),
  43. ),
  44. );
  45. }
  46. }
  47. // ignore: must_be_immutable
  48. class EmailPage extends StatefulWidget {
  49. EmailPage({Key? key}) : super(key: key);
  50. String selectedFolder = "INBOX";
  51. int offset = 0;
  52. int page = 1;
  53. @override
  54. EmailPageState createState() => EmailPageState();
  55. }
  56. class EmailPageState extends State<EmailPage> {
  57. final ApiService apiService = ApiService();
  58. List<GetThreadResponse> emails = [];
  59. @override
  60. void initState() {
  61. super.initState();
  62. widget.page = widget.page;
  63. }
  64. void updateSelectedFolder(String folder) {
  65. setState(() {
  66. widget.selectedFolder = folder;
  67. });
  68. print(folder);
  69. _fetchEmails();
  70. }
  71. String getPage() {
  72. return widget.page.toString();
  73. }
  74. void updatePagenation(String option) {
  75. if (option == "next") {
  76. setState(() {
  77. widget.offset += 50;
  78. widget.page += 1;
  79. });
  80. } else if (option == "back") {
  81. setState(() {
  82. widget.offset -= 50;
  83. widget.page -= 1;
  84. });
  85. }
  86. // print(currentPage);
  87. _fetchEmails();
  88. }
  89. void _fetchEmails() async {
  90. // print(selectedFolder)
  91. try {
  92. List<GetThreadResponse> fetchedEmails = await apiService
  93. .fetchEmailsFromFolder(widget.selectedFolder, widget.offset);
  94. if (!mounted) return;
  95. setState(() {
  96. emails = fetchedEmails; // Update the list of emails
  97. });
  98. } catch (e) {
  99. print('Error fetching emails: $e');
  100. }
  101. }
  102. @override
  103. Widget build(BuildContext context) {
  104. _fetchEmails();
  105. return Scaffold(
  106. body: EmailListScreen(
  107. emails: emails,
  108. getEmailContent: apiService.fetchEmailContent,
  109. ),
  110. );
  111. }
  112. }