123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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<String, dynamic> 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<EmailPage> createState() => _EmailPageState();
- }
- class _EmailPageState extends State<EmailPage> {
- List emails = [];
- void _displayEmailsFromFolder(String folder) async {
- Map<String, List<SerializableMessage>> messagesMap = {};
- try {
- var url = Uri.http(
- '127.0.0.1:3001', 'sorted_threads_by_date', {'folder': folder});
- var response = await http.get(url);
- Map<String, dynamic> json = jsonDecode(response.body);
- json.forEach((key, value) {
- List<SerializableMessage> 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<List<Widget>> _getDrawerItems() async {
- List<String> drawerItems = [];
- try {
- var url = Uri.http('127.0.0.1:3001', 'folders');
- var response = await http.get(url);
- drawerItems = List<String>.from(json.decode(response.body));
- } catch (e) {
- print('_getDrawerItems caught error: $e');
- }
- List<Widget> 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<List<Widget>>(
- future:
- _getDrawerItems(), // call the async function to get the future
- builder:
- (BuildContext context, AsyncSnapshot<List<Widget>> 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(
- """
- <h1>Heading</h1>
- <p>This is a <strong>simple</strong> HTML example.</p>
- """,
- ),
- );
- }
- }
|