serialize.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/material.dart';
  2. import 'api_service.dart';
  3. import 'structs.dart';
  4. class SerializableMessageListScreen extends StatefulWidget {
  5. @override
  6. _SerializableMessageListScreenState createState() => _SerializableMessageListScreenState();
  7. }
  8. class _SerializableMessageListScreenState extends State<SerializableMessageListScreen> {
  9. List<SerializableMessage>? messages;
  10. bool isLoading = true;
  11. bool hasError = false;
  12. final ApiService apiService = ApiService();
  13. @override
  14. void initState() {
  15. super.initState();
  16. _fetchMessages();
  17. }
  18. Future<void> _fetchMessages() async {
  19. try {
  20. List<SerializableMessage> fetchedMessages = await apiService.sonicSearch("INBOX", 10, 0, "searchQuery");
  21. setState(() {
  22. messages = fetchedMessages;
  23. isLoading = false;
  24. });
  25. } catch (e) {
  26. setState(() {
  27. hasError = true;
  28. isLoading = false;
  29. });
  30. }
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. if (isLoading) {
  35. return Center(child: CircularProgressIndicator());
  36. }
  37. if (hasError) {
  38. return Center(child: Text("Error fetching messages."));
  39. }
  40. if (messages == null || messages!.isEmpty) {
  41. return Center(child: Text("No messages found."));
  42. }
  43. return ListView.separated(
  44. itemCount: messages!.length,
  45. itemBuilder: (context, index) {
  46. final message = messages![index];
  47. return ListTile(
  48. title: Text(message.name, style: TextStyle(fontWeight: FontWeight.bold)),
  49. subtitle: Column(
  50. crossAxisAlignment: CrossAxisAlignment.start,
  51. children: [Text(message.subject)],
  52. ),
  53. trailing: Text(message.date),
  54. onTap: () async {
  55. String emailContent = await apiService.fetchEmailContent([message.id]);
  56. Navigator.push(
  57. context,
  58. MaterialPageRoute(
  59. builder: (context) => EmailView(
  60. emailContent: emailContent,
  61. from: message.from,
  62. name: message.name,
  63. to: message.to.toString(),
  64. subject: message.subject,
  65. date: message.date,
  66. id: message.id,
  67. ),
  68. ),
  69. );
  70. },
  71. );
  72. },
  73. separatorBuilder: (context, index) => Divider(),
  74. );
  75. }
  76. }