123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import 'package:flutter/material.dart';
- import 'api_service.dart';
- import 'structs.dart';
- class EmailListScreen extends StatelessWidget {
- final List<GetThreadResponse> emails;
- final Future<String> Function(List<String>) getEmailContent;
- EmailListScreen({required this.emails, required this.getEmailContent});
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: ListView.separated(
- itemCount: emails.length,
- itemBuilder: (context, index) {
- final email = emails[index];
- return ListTile(
- title: Text(email.from_name,
- style: TextStyle(fontWeight: FontWeight.bold)),
- subtitle: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [Text(email.subject)],
- ),
- trailing: Text(email.date.toString()),
- onTap: () async {
- String emailContent = await getEmailContent(email.messages);
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => EmailView(
- emailContent: emailContent,
- from: email.from_address,
- name: email.from_name,
- to: email.to.toString(),
- subject: email.subject,
- date: email.date.toString(),
- id: email.id.toString(),
- ),
- ),
- );
- },
- );
- },
- separatorBuilder: (context, index) => Divider(),
- ),
- );
- }
- }
- // ignore: must_be_immutable
- class EmailPage extends StatefulWidget {
- EmailPage({Key? key}) : super(key: key);
- String selectedFolder = "INBOX";
- int offset = 0;
- int page = 1;
- @override
- EmailPageState createState() => EmailPageState();
- }
- class EmailPageState extends State<EmailPage> {
- final ApiService apiService = ApiService();
- List<GetThreadResponse> emails = [];
- @override
- void initState() {
- super.initState();
- widget.page = widget.page;
- }
- void updateSelectedFolder(String folder) {
- setState(() {
- widget.selectedFolder = folder;
- });
- print(folder);
- _fetchEmails();
- }
- String getPage() {
- return widget.page.toString();
- }
- void updatePagenation(String option) {
- if (option == "next") {
- setState(() {
- widget.offset += 50;
- widget.page += 1;
- });
- } else if (option == "back") {
- setState(() {
- widget.offset -= 50;
- widget.page -= 1;
- });
- }
- // print(currentPage);
- _fetchEmails();
- }
- void _fetchEmails() async {
- // print(selectedFolder)
- try {
- List<GetThreadResponse> fetchedEmails = await apiService
- .fetchEmailsFromFolder(widget.selectedFolder, widget.offset);
- if (!mounted) return;
- setState(() {
- emails = fetchedEmails; // Update the list of emails
- });
- } catch (e) {
- print('Error fetching emails: $e');
- }
- }
- @override
- Widget build(BuildContext context) {
- _fetchEmails();
- return Scaffold(
- body: EmailListScreen(
- emails: emails,
- getEmailContent: apiService.fetchEmailContent,
- ),
- );
- }
- }
|