From aeffa106bec5e5c67889b2a729ed3db84b28ca11 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Wed, 10 Jan 2024 23:12:29 +0300 Subject: [PATCH] fix: Loading task manager page without tasks --- lib/controllers/taskmanager_controller.dart | 4 +- lib/pages/tasks_pages/taskmanager_page.dart | 139 ++++++++++---------- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/lib/controllers/taskmanager_controller.dart b/lib/controllers/taskmanager_controller.dart index 77385b9..4204851 100644 --- a/lib/controllers/taskmanager_controller.dart +++ b/lib/controllers/taskmanager_controller.dart @@ -24,9 +24,9 @@ class TaskManagerController extends GetxController { var isloading = false.obs; var tasks = [].obs; - void getTasks() { + Future getTasks() async { tasks.clear(); - final tasksList = appDB.get("tasks") ?? []; + final tasksList = await appDB.get("tasks") ?? []; for (final task in tasksList) { tasks.add(task); } diff --git a/lib/pages/tasks_pages/taskmanager_page.dart b/lib/pages/tasks_pages/taskmanager_page.dart index 095ea5d..fed80d8 100644 --- a/lib/pages/tasks_pages/taskmanager_page.dart +++ b/lib/pages/tasks_pages/taskmanager_page.dart @@ -1,4 +1,5 @@ import 'package:academia/controllers/taskmanager_controller.dart'; +import 'package:academia/models/tasks.dart'; import 'package:academia/pages/tasks_pages/newtask_page.dart'; import 'package:academia/pages/tasks_pages/taskinfo_page.dart'; import 'package:flutter/material.dart'; @@ -11,78 +12,76 @@ class TaskManagerPage extends StatelessWidget { @override Widget build(BuildContext context) { - return Obx(() { - taskManagerController.getTasks(); - final tasks = taskManagerController.tasks; + taskManagerController.getTasks(); + final tasks = taskManagerController.tasks; - return Scaffold( - appBar: AppBar(title: const Text("Task Manager"), actions: [ - IconButton( - onPressed: () { - Get.defaultDialog( - title: "Task Manager", - content: const Text( - "This is the task manager. You can add tasks here and view them in the calendar."), - textConfirm: "Got it!", - confirmTextColor: Colors.white, - onConfirm: () => Get.back(), - ); - }, - icon: const Icon(Icons.info_rounded), - ), - ]), - body: // Check if there are any tasks - tasks.isEmpty - ? const Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Icon(Icons.task, size: 100), - Text("No tasks yet"), - ], - ), - ) - : ListView.builder( - itemCount: tasks.length, - itemBuilder: (context, index) { - return Card( - child: ListTile( - title: Text(tasks[index].title ?? ''), - subtitle: Text(tasks[index].unit ?? ''), - trailing: Column( - children: [ - Text( - DateFormat("dd/MM/yyyy").format( - (tasks[index].deadline ?? DateTime.now())), - ), - const SizedBox(height: 5), - Text( - "${tasks[index].progress}%", - ) - ], - ), - onTap: () { - taskManagerController.updateTask( - tasks[index].type ?? '', - tasks[index].title ?? '', - tasks[index].description ?? '', - tasks[index].unit ?? '', - tasks[index].deadline ?? DateTime.now(), - tasks[index].progress ?? 0); - Get.to(() => TaskInformationPage( - task: tasks[index], - )); - }, - )); - }, - ), - floatingActionButton: FloatingActionButton( + return Obx(() => Scaffold( + appBar: AppBar(title: const Text("Task Manager"), actions: [ + IconButton( onPressed: () { - taskManagerController.clearTaskManager(); - Get.to(() => NewTaskPage()); + Get.defaultDialog( + title: "Task Manager", + content: const Text( + "This is the task manager. You can add tasks here and view them in the calendar."), + textConfirm: "Got it!", + confirmTextColor: Colors.white, + onConfirm: () => Get.back(), + ); }, - child: const Icon(Icons.add), - )); - }); + icon: const Icon(Icons.info_rounded), + ), + ]), + body: // Check if there are any tasks + tasks.isEmpty + ? const Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon(Icons.task, size: 100), + Text("No tasks yet"), + ], + ), + ) + : ListView.builder( + itemCount: tasks.length, + itemBuilder: (context, index) { + return Card( + child: ListTile( + title: Text(tasks[index].title ?? ''), + subtitle: Text(tasks[index].unit ?? ''), + trailing: Column( + children: [ + Text( + DateFormat("dd/MM/yyyy").format( + (tasks[index].deadline ?? DateTime.now())), + ), + const SizedBox(height: 5), + Text( + "${tasks[index].progress}%", + ) + ], + ), + onTap: () { + taskManagerController.updateTask( + tasks[index].type ?? '', + tasks[index].title ?? '', + tasks[index].description ?? '', + tasks[index].unit ?? '', + tasks[index].deadline ?? DateTime.now(), + tasks[index].progress ?? 0); + Get.to(() => TaskInformationPage( + task: tasks[index], + )); + }, + )); + }, + ), + floatingActionButton: FloatingActionButton( + onPressed: () { + taskManagerController.clearTaskManager(); + Get.to(() => NewTaskPage()); + }, + child: const Icon(Icons.add), + ))); } }