import 'package:flutter/material.dart'; import 'api_service.dart'; import 'structs.dart'; class SerializableMessageListScreen extends StatefulWidget { @override _SerializableMessageListScreenState createState() => _SerializableMessageListScreenState(); } class _SerializableMessageListScreenState extends State { List? messages; bool isLoading = true; bool hasError = false; final ApiService apiService = ApiService(); @override void initState() { super.initState(); _fetchMessages(); } Future _fetchMessages() async { try { List fetchedMessages = await apiService.sonicSearch("INBOX", 10, 0, "searchQuery"); setState(() { messages = fetchedMessages; isLoading = false; }); } catch (e) { setState(() { hasError = true; isLoading = false; }); } } @override Widget build(BuildContext context) { if (isLoading) { return Center(child: CircularProgressIndicator()); } if (hasError) { return Center(child: Text("Error fetching messages.")); } if (messages == null || messages!.isEmpty) { return Center(child: Text("No messages found.")); } return ListView.separated( itemCount: messages!.length, itemBuilder: (context, index) { final message = messages![index]; return ListTile( title: Text(message.name, style: TextStyle(fontWeight: FontWeight.bold)), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(message.subject)], ), trailing: Text(message.date), onTap: () async { String emailContent = await apiService.fetchEmailContent([message.id]); Navigator.push( context, MaterialPageRoute( builder: (context) => EmailView( emailContent: emailContent, from: message.from, name: message.name, to: message.to.toString(), subject: message.subject, date: message.date, id: message.id, ), ), ); }, ); }, separatorBuilder: (context, index) => Divider(), ); } }