import 'package:date_picker_timeline/date_picker_timeline.dart'; import 'package:flutter/material.dart'; import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:sqflite_pust_local_notification/models/task_model.dart'; import 'package:sqflite_pust_local_notification/services/notification_service.dart'; import 'package:sqflite_pust_local_notification/services/theme_service.dart'; import 'package:sqflite_pust_local_notification/ui/add_task_screen.dart'; import 'package:sqflite_pust_local_notification/utils/assets_string.dart'; import 'package:sqflite_pust_local_notification/utils/colors.dart'; import 'package:sqflite_pust_local_notification/widgets/my_button.dart'; import 'package:sqflite_pust_local_notification/widgets/task_tile.dart'; import '../controllers/task_controller.dart'; import '../utils/textStyles.dart'; class MyHomeScreen extends StatefulWidget { const MyHomeScreen({Key? key}) : super(key: key); @override State createState() => _MyHomeScreenState(); } class _MyHomeScreenState extends State { final TaskController _taskController = Get.put(TaskController()); var taskList = [].obs; DateTime _selectedDate = DateTime.now(); DateTime _today = DateTime.now(); var notifyHelper; @override void initState() { // TODO: implement initState super.initState(); notifyHelper = NotifyHelper(); notifyHelper.initializeNotification(); notifyHelper.requestIOSPermissions(); _taskController.getTask(); //_getTask(); } @override Widget build(BuildContext context) { return Scaffold( appBar: _appBar(), backgroundColor: context.theme.backgroundColor, body: Column(children: [ _appTaskBar(), _addDateBar(), SizedBox( height: 20, ), _showTsks() ]), ); } // _getTask() async { // var tasks = await _taskController.getTask(); // taskList.assignAll(tasks.map((data) => TaskModel.fromJson(data)).toList()); // } _appBar() { return AppBar( elevation: 0, backgroundColor: context.theme.backgroundColor, leading: GestureDetector( onTap: () { ThemeService().switchTheme(); notifyHelper.displayNotification( title: "Theme Change", body: Get.isDarkMode ? "Activated Light Mode" : "Activated Dark Mode"); }, child: Icon( Get.isDarkMode ? Icons.wb_sunny_outlined : Icons.nightlight_round, size: 20, color: Get.isDarkMode ? Colors.white : Colors.black), ), actions: const [ CircleAvatar( backgroundImage: AssetImage(profile), ), SizedBox( width: 20, ), ], ); } _appTaskBar() { return Container( margin: const EdgeInsets.only(left: 20, right: 20, top: 10), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( DateFormat.yMMMd().format(DateTime.now()), style: headingStyle, ), Text( "Today", style: subHeadingStyle, ) ], ), ), MyButton( label: "+ Add Task", onTap: () async { await Get.to(AddTaskScreen()); _taskController.getTask(); }, ) ], ), ); } _addDateBar() { return Container( margin: const EdgeInsets.only(top: 20, left: 20), child: DatePicker( DateTime.now(), height: 100, width: 80, initialSelectedDate: DateTime.now(), selectionColor: primaryClr, selectedTextColor: Colors.white, dateTextStyle: datePickerTextStyle, dayTextStyle: datePickerTextStyle, monthTextStyle: datePickerTextStyle, onDateChange: (Date) { setState(() { _selectedDate = Date; }); }, ), ); } _showTsks() { return Expanded(child: Obx( () { return ListView.builder( itemCount: _taskController.taskList.length, itemBuilder: (_, index) { TaskModel taskModel = _taskController.taskList[index]; print(taskModel.toJson()); if (taskModel.repeat == "Daily") { DateTime date = DateFormat.jm().parse(taskModel.startTime.toString()); var myTime = DateFormat("HH:mm").format(date); print(myTime); notifyHelper.scheduledNotification( int.parse(myTime.toString().split(":")[0]), int.parse(myTime.toString().split(":")[1]), taskModel); return _setTaskView(index, taskModel); } // else if (taskModel.repeat == "None" && // taskModel.date == DateFormat.yMd().format(_today)) { // print("Show task notification " + // isShowTaskNotification.toString()); // if (isShowTaskNotification) { // notifyHelper.displayNotification( // id: taskModel.id, // title: taskModel.title, // body: taskModel.note); // } // } if (taskModel.date == DateFormat.yMd().format(_selectedDate)) { return _setTaskView(index, taskModel); } else { return Container(); } }); }, )); } _setTaskView(int index, TaskModel taskModel) { return AnimationConfiguration.staggeredList( position: index, child: SlideAnimation( child: FlipAnimation( delay: Duration(milliseconds: 200), child: Row( children: [ GestureDetector( onTap: () { _showBottomSheet(context, taskModel); }, child: TaskTile(taskModel), ) ], )), )); } _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), ), ), ), ); } }