import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; // import 'package:flutter_html/flutter_html.dart'; class SerializableMessage { final String name; final String from; final String path; final String subject; final String date; SerializableMessage( {required this.name, required this.from, required this.path, required this.subject, required this.date}); factory SerializableMessage.fromJson(Map json) { return SerializableMessage( name: json['name'], from: json['from'], path: json['path'], subject: json['subject'], date: json['date']); } } class EmailPage extends StatefulWidget { const EmailPage({super.key}); final String title = 'cars'; @override State createState() => _EmailPageState(); } class _EmailPageState extends State { List emails = []; void _displayEmailsFromFolder(String folder) async { Map> messagesMap = {}; try { var url = Uri.http( '127.0.0.1:3001', 'sorted_threads_by_date', {'folder': folder}); var response = await http.get(url); Map json = jsonDecode(response.body); json.forEach((key, value) { List messages = (value as List) .map((item) => SerializableMessage.fromJson(item)) .toList(); messagesMap[key] = messages; }); } catch (e) { print('_displayEmailsFromFolder caught error: $e'); } setState(() { emails.clear(); emails = messagesMap.values.toList().expand((list) => list).toList(); ; }); } Future> _getDrawerItems() async { List drawerItems = []; try { var url = Uri.http('127.0.0.1:3001', 'folders'); var response = await http.get(url); drawerItems = List.from(json.decode(response.body)); } catch (e) { print('_getDrawerItems caught error: $e'); } List drawerWidgets = []; for (String item in drawerItems) { drawerWidgets.add( ListTile( leading: Icon(Icons.mail), title: Text(item), onTap: () { _displayEmailsFromFolder(item); Navigator.pop(context); }, ), ); } return drawerWidgets; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), drawer: Drawer( child: FutureBuilder>( future: _getDrawerItems(), // call the async function to get the future builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { // While data is loading, show a progress indicator return Center(child: CircularProgressIndicator()); } else if (snapshot.hasError) { // If something went wrong, show an error message return Center(child: Text('Error: ${snapshot.error}')); } else { // When data is fetched successfully, display the items return ListView( padding: EdgeInsets.zero, children: snapshot.data!, // Unwrap the data once confirmed it's there ); } }, ), ), body: EmailListScreen( emails: emails, ), ); } } class EmailListScreen extends StatelessWidget { List emails; EmailListScreen({required this.emails}); @override Widget build(BuildContext context) { print(emails); return Scaffold( appBar: AppBar( title: Text('Emails'), ), body: ListView.separated( itemCount: emails.length, itemBuilder: (context, index) { return ListTile( title: Text(emails[index].from, style: TextStyle(fontWeight: FontWeight.bold)), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(emails[index].subject), ], ), trailing: Text(emails[index].date.toString()), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => EmailView(emailContent: "")), ); }); }, separatorBuilder: (context, index) { return Divider(); }, ), ); } } class EmailView extends StatelessWidget { final String emailContent; EmailView({required this.emailContent}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("HTML Content"), ), body: Text( """

Heading

This is a simple HTML example.

""", ), ); } }