import 'package:flutter/material.dart'; import 'api_service.dart'; import 'structs.dart'; class EmailListScreen extends StatelessWidget { final List emails; final Future Function(List) getEmailContent; EmailListScreen({required this.emails, required this.getEmailContent}); @override Widget build(BuildContext context) { return Scaffold( body: ListView.separated( itemCount: emails.length, itemBuilder: (context, index) { final email = emails[index]; return ListTile( title: Text(email.from_name, style: TextStyle(fontWeight: FontWeight.bold)), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(email.subject)], ), trailing: Text(email.date.toString()), onTap: () async { String emailContent = await getEmailContent(email.messages); Navigator.push( context, MaterialPageRoute( builder: (context) => EmailView( emailContent: emailContent, from: email.from_address, name: email.from_name, to: email.to.toString(), subject: email.subject, date: email.date.toString(), id: email.id.toString(), ), ), ); }, ); }, separatorBuilder: (context, index) => Divider(), ), ); } } // ignore: must_be_immutable class EmailPage extends StatefulWidget { EmailPage({Key? key}) : super(key: key); String selectedFolder = "INBOX"; int offset = 0; int page = 1; @override EmailPageState createState() => EmailPageState(); } class EmailPageState extends State { final ApiService apiService = ApiService(); List emails = []; @override void initState() { super.initState(); widget.page = widget.page; } void updateSelectedFolder(String folder) { setState(() { widget.selectedFolder = folder; }); print(folder); _fetchEmails(); } String getPage() { return widget.page.toString(); } void updatePagenation(String option) { if (option == "next") { setState(() { widget.offset += 50; widget.page += 1; }); } else if (option == "back") { setState(() { widget.offset -= 50; widget.page -= 1; }); } // print(currentPage); _fetchEmails(); } void _fetchEmails() async { // print(selectedFolder) try { List fetchedEmails = await apiService .fetchEmailsFromFolder(widget.selectedFolder, widget.offset); if (!mounted) return; setState(() { emails = fetchedEmails; // Update the list of emails }); } catch (e) { print('Error fetching emails: $e'); } } @override Widget build(BuildContext context) { _fetchEmails(); return Scaffold( body: EmailListScreen( emails: emails, getEmailContent: apiService.fetchEmailContent, ), ); } }