From 13fb26be6d6aeefa8022a9c9cd1de50d10a32ef8 Mon Sep 17 00:00:00 2001 From: Apalak Dutta Date: Sun, 26 Jun 2022 13:44:12 +0530 Subject: [PATCH] Update and Delete task --- lib/controllers/task_controller.dart | 6 ++ lib/db/db_helper.dart | 10 +++- lib/ui/my_home_screen.dart | 89 +++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/lib/controllers/task_controller.dart b/lib/controllers/task_controller.dart index 0eaaa51..6b1bf16 100644 --- a/lib/controllers/task_controller.dart +++ b/lib/controllers/task_controller.dart @@ -22,5 +22,11 @@ class TaskController extends GetxController { void delete(TaskModel taskModel) { DBHelper.delete(taskModel); + getTask(); + } + + void updateTaskIsComplete(int id) { + DBHelper.update(id); + getTask(); } } diff --git a/lib/db/db_helper.dart b/lib/db/db_helper.dart index 7a417cd..f98f83b 100644 --- a/lib/db/db_helper.dart +++ b/lib/db/db_helper.dart @@ -43,6 +43,14 @@ class DBHelper { static delete(TaskModel taskModel) async { return await _db! - .delete(_tableName, where: 'id?', whereArgs: [taskModel.id]); + .delete(_tableName, where: 'id=?', whereArgs: [taskModel.id]); + } + + static update(int id) async { + return await _db!.rawUpdate(''' + UPDATE tbtask + SET isCompleted = ? + WHERE id = ? + ''', [1, id]); } } diff --git a/lib/ui/my_home_screen.dart b/lib/ui/my_home_screen.dart index 492d9b5..f0741eb 100644 --- a/lib/ui/my_home_screen.dart +++ b/lib/ui/my_home_screen.dart @@ -158,7 +158,8 @@ class _MyHomeScreenState extends State { children: [ GestureDetector( onTap: () { - print("Tapped"); + _showBottomSheet( + context, _taskController.taskList[index]); }, child: TaskTile(_taskController.taskList[index]), ) @@ -169,4 +170,90 @@ class _MyHomeScreenState extends State { }, )); } + + _showBottomSheet(BuildContext context, TaskModel taskModel) { + Get.bottomSheet(Container( + padding: const EdgeInsets.only(top: 4), + height: taskModel.isCompleted == 1 + ? MediaQuery.of(context).size.height * 0.24 + : MediaQuery.of(context).size.height * 0.32, + color: Get.isDarkMode ? darkgratClr : Colors.white, + child: Column(children: [ + Container( + height: 6, + width: 120, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(10), + color: Get.isDarkMode ? Colors.grey[600] : Colors.grey[300]), + ), + Spacer(), + taskModel.isCompleted == 1 + ? Container() + : _bottomSheetButton( + label: "Task Completed", + onTap: () { + _taskController.updateTaskIsComplete(taskModel.id!); + Get.back(); + }, + clr: primaryClr, + context: context), + _bottomSheetButton( + label: "Delete Task", + onTap: () { + _taskController.delete(taskModel); + Get.back(); + }, + clr: Colors.red[600]!, + context: context), + SizedBox( + height: 10, + ), + _bottomSheetButton( + label: "Close", + isClose: true, + onTap: () { + Get.back(); + }, + clr: Colors.white, + context: context), + SizedBox( + height: 20, + ) + ]), + )); + } + + _bottomSheetButton( + {required String label, + required Function()? onTap, + required Color clr, + bool isClose = false, + required BuildContext context}) { + return GestureDetector( + onTap: onTap, + child: Container( + margin: const EdgeInsets.symmetric(vertical: 4), + height: 55, + width: MediaQuery.of(context).size.width * 0.9, + decoration: BoxDecoration( + border: Border.all( + width: 2, + color: isClose == true + ? Get.isDarkMode + ? Colors.grey[600]! + : Colors.grey[300]! + : clr), + borderRadius: BorderRadius.circular(20), + color: isClose == true ? Colors.transparent : clr, + ), + child: Center( + child: Text( + label, + style: + isClose ? titleStyle : titleStyle.copyWith(color: Colors.white), + ), + ), + ), + ); + } }