Browse Source

added pages

juan 1 month ago
parent
commit
336f7e8788
3 changed files with 47 additions and 14 deletions
  1. 1 7
      lib/api_service.dart
  2. 31 3
      lib/email.dart
  3. 15 4
      lib/home_page.dart

+ 1 - 7
lib/api_service.dart

@@ -11,7 +11,6 @@ import 'dart:html' as html;
 
 
 class ApiService {
-  // List emails = [];
 
   Future<List<GetThreadResponse>> fetchEmailsFromFolder(
       String folder, int pagenitaion) async {
@@ -44,11 +43,6 @@ class ApiService {
       print('_displayEmailsFromFolder caught error: $e');
       return [];
     }
-    print("Done");
-
-    // setState(() {
-    //   emails = allEmails;
-    // });
   }
 
   Future<void> fetchThreads(
@@ -88,7 +82,7 @@ class ApiService {
 
         if (response.statusCode == 200) {
           content += response.body;
-          content += "<p>end of mail</p><br><br><br><hr>";
+          content += "<hr>";
         }
       }
     } catch (e) {

+ 31 - 3
lib/email.dart

@@ -51,7 +51,9 @@ class EmailListScreen extends StatelessWidget {
 class EmailPage extends StatefulWidget {
   EmailPage({Key? key}) : super(key: key);
   String selectedFolder = "INBOX";
-  
+  int offset = 0;
+  int page = 1;
+
   @override
   EmailPageState createState() => EmailPageState();
 }
@@ -60,6 +62,12 @@ 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;
@@ -68,11 +76,31 @@ class EmailPageState extends State<EmailPage> {
     _fetchEmails();
   }
 
+  String getPage() {
+    return widget.page.toString();
+  }
+
+  void updatePagenation(String option) {
+    if (option == "next") {
+      setState(() {
+        widget.offset += 20;
+        widget.page += 1;
+      });
+    } else if (option == "back") {
+      setState(() {
+        widget.offset -= 20;
+        widget.page -= 1;
+      });
+    }
+    // print(currentPage);
+    _fetchEmails();
+  }
+
   void _fetchEmails() async {
     // print(selectedFolder)
     try {
-      List<GetThreadResponse> fetchedEmails =
-          await apiService.fetchEmailsFromFolder(widget.selectedFolder, 0);
+      List<GetThreadResponse> fetchedEmails = await apiService
+          .fetchEmailsFromFolder(widget.selectedFolder, widget.offset);
       setState(() {
         emails = fetchedEmails; // Update the list of emails
       });

+ 15 - 4
lib/home_page.dart

@@ -9,7 +9,6 @@ class HomeScreen extends StatefulWidget {
   _HomeScreenState createState() => _HomeScreenState();
 }
 
-
 class _HomeScreenState extends State<HomeScreen> {
   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
   final GlobalKey<EmailPageState> _emailPageKey = GlobalKey<EmailPageState>();
@@ -21,7 +20,6 @@ class _HomeScreenState extends State<HomeScreen> {
     super.initState();
   }
 
-
   @override
   Widget build(BuildContext context) {
     return Scaffold(
@@ -116,12 +114,25 @@ class _HomeScreenState extends State<HomeScreen> {
                       ),
                     ),
                     Container(
+                      // alignment: Alignment.topLeft,
                       padding: EdgeInsets.all(8.0),
                       color: Colors.white,
                       child: Row(
                         children: [
-                          Container(
-                            child: Text('hiiiiiii'),
+                          ElevatedButton(
+                            onPressed: () {
+                              _emailPageKey.currentState
+                                  ?.updatePagenation('back');
+                            },
+                            child: Icon(Icons.navigate_before),
+                          ),
+                          Text(_emailPageKey.currentState?.getPage() ?? '1'),
+                          ElevatedButton(
+                            onPressed: () {
+                              _emailPageKey.currentState
+                                  ?.updatePagenation('next');
+                            },
+                            child: Icon(Icons.navigate_next),
                           ),
                         ],
                       ),