email.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import 'package:flutter/material.dart';
  2. import 'package:http/http.dart' as http;
  3. import 'dart:convert';
  4. // import 'package:flutter_html/flutter_html.dart';
  5. class SerializableMessage {
  6. final String name;
  7. final String from;
  8. final String path;
  9. final String subject;
  10. final String date;
  11. SerializableMessage(
  12. {required this.name,
  13. required this.from,
  14. required this.path,
  15. required this.subject,
  16. required this.date});
  17. factory SerializableMessage.fromJson(Map<String, dynamic> json) {
  18. return SerializableMessage(
  19. name: json['name'],
  20. from: json['from'],
  21. path: json['path'],
  22. subject: json['subject'],
  23. date: json['date']);
  24. }
  25. }
  26. class EmailPage extends StatefulWidget {
  27. const EmailPage({super.key});
  28. final String title = 'cars';
  29. @override
  30. State<EmailPage> createState() => _EmailPageState();
  31. }
  32. class _EmailPageState extends State<EmailPage> {
  33. List emails = [];
  34. void _displayEmailsFromFolder(String folder) async {
  35. Map<String, List<SerializableMessage>> messagesMap = {};
  36. try {
  37. var url = Uri.http(
  38. '127.0.0.1:3001', 'sorted_threads_by_date', {'folder': folder});
  39. var response = await http.get(url);
  40. Map<String, dynamic> json = jsonDecode(response.body);
  41. json.forEach((key, value) {
  42. List<SerializableMessage> messages = (value as List)
  43. .map((item) => SerializableMessage.fromJson(item))
  44. .toList();
  45. messagesMap[key] = messages;
  46. });
  47. } catch (e) {
  48. print('_displayEmailsFromFolder caught error: $e');
  49. }
  50. setState(() {
  51. emails.clear();
  52. emails = messagesMap.values.toList().expand((list) => list).toList();
  53. ;
  54. });
  55. }
  56. Future<List<Widget>> _getDrawerItems() async {
  57. List<String> drawerItems = [];
  58. try {
  59. var url = Uri.http('127.0.0.1:3001', 'folders');
  60. var response = await http.get(url);
  61. drawerItems = List<String>.from(json.decode(response.body));
  62. } catch (e) {
  63. print('_getDrawerItems caught error: $e');
  64. }
  65. List<Widget> drawerWidgets = [];
  66. for (String item in drawerItems) {
  67. drawerWidgets.add(
  68. ListTile(
  69. leading: Icon(Icons.mail),
  70. title: Text(item),
  71. onTap: () {
  72. _displayEmailsFromFolder(item);
  73. Navigator.pop(context);
  74. },
  75. ),
  76. );
  77. }
  78. return drawerWidgets;
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. return Scaffold(
  83. appBar: AppBar(
  84. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  85. title: Text(widget.title),
  86. ),
  87. drawer: Drawer(
  88. child: FutureBuilder<List<Widget>>(
  89. future:
  90. _getDrawerItems(), // call the async function to get the future
  91. builder:
  92. (BuildContext context, AsyncSnapshot<List<Widget>> snapshot) {
  93. if (snapshot.connectionState == ConnectionState.waiting) {
  94. // While data is loading, show a progress indicator
  95. return Center(child: CircularProgressIndicator());
  96. } else if (snapshot.hasError) {
  97. // If something went wrong, show an error message
  98. return Center(child: Text('Error: ${snapshot.error}'));
  99. } else {
  100. // When data is fetched successfully, display the items
  101. return ListView(
  102. padding: EdgeInsets.zero,
  103. children:
  104. snapshot.data!, // Unwrap the data once confirmed it's there
  105. );
  106. }
  107. },
  108. ),
  109. ),
  110. body: EmailListScreen(
  111. emails: emails,
  112. ),
  113. );
  114. }
  115. }
  116. class EmailListScreen extends StatelessWidget {
  117. List emails;
  118. EmailListScreen({required this.emails});
  119. @override
  120. Widget build(BuildContext context) {
  121. print(emails);
  122. return Scaffold(
  123. appBar: AppBar(
  124. title: Text('Emails'),
  125. ),
  126. body: ListView.separated(
  127. itemCount: emails.length,
  128. itemBuilder: (context, index) {
  129. return ListTile(
  130. title: Text(emails[index].from,
  131. style: TextStyle(fontWeight: FontWeight.bold)),
  132. subtitle: Column(
  133. crossAxisAlignment: CrossAxisAlignment.start,
  134. children: [
  135. Text(emails[index].subject),
  136. ],
  137. ),
  138. trailing: Text(emails[index].date.toString()),
  139. onTap: () {
  140. Navigator.push(
  141. context,
  142. MaterialPageRoute(
  143. builder: (context) => EmailView(emailContent: "")),
  144. );
  145. });
  146. },
  147. separatorBuilder: (context, index) {
  148. return Divider();
  149. },
  150. ),
  151. );
  152. }
  153. }
  154. class EmailView extends StatelessWidget {
  155. final String emailContent;
  156. EmailView({required this.emailContent});
  157. @override
  158. Widget build(BuildContext context) {
  159. return Scaffold(
  160. appBar: AppBar(
  161. title: Text("HTML Content"),
  162. ),
  163. body: Text(
  164. """
  165. <h1>Heading</h1>
  166. <p>This is a <strong>simple</strong> HTML example.</p>
  167. """,
  168. ),
  169. );
  170. }
  171. }