This is flutter sqlite local notification project. User can add task.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import 'package:date_picker_timeline/date_picker_timeline.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
  4. import 'package:get/get.dart';
  5. import 'package:intl/intl.dart';
  6. import 'package:sqflite_pust_local_notification/models/task_model.dart';
  7. import 'package:sqflite_pust_local_notification/services/notification_service.dart';
  8. import 'package:sqflite_pust_local_notification/services/theme_service.dart';
  9. import 'package:sqflite_pust_local_notification/ui/add_task_screen.dart';
  10. import 'package:sqflite_pust_local_notification/utils/assets_string.dart';
  11. import 'package:sqflite_pust_local_notification/utils/colors.dart';
  12. import 'package:sqflite_pust_local_notification/widgets/my_button.dart';
  13. import 'package:sqflite_pust_local_notification/widgets/task_tile.dart';
  14. import '../controllers/task_controller.dart';
  15. import '../utils/textStyles.dart';
  16. class MyHomeScreen extends StatefulWidget {
  17. const MyHomeScreen({Key? key}) : super(key: key);
  18. @override
  19. State<MyHomeScreen> createState() => _MyHomeScreenState();
  20. }
  21. class _MyHomeScreenState extends State<MyHomeScreen> {
  22. final TaskController _taskController = Get.put(TaskController());
  23. var taskList = <TaskModel>[].obs;
  24. DateTime _selectedDate = DateTime.now();
  25. DateTime _today = DateTime.now();
  26. var notifyHelper;
  27. @override
  28. void initState() {
  29. // TODO: implement initState
  30. super.initState();
  31. notifyHelper = NotifyHelper();
  32. notifyHelper.initializeNotification();
  33. notifyHelper.requestIOSPermissions();
  34. _taskController.getTask();
  35. //_getTask();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return Scaffold(
  40. appBar: _appBar(),
  41. backgroundColor: context.theme.backgroundColor,
  42. body: Column(children: [
  43. _appTaskBar(),
  44. _addDateBar(),
  45. SizedBox(
  46. height: 20,
  47. ),
  48. _showTsks()
  49. ]),
  50. );
  51. }
  52. // _getTask() async {
  53. // var tasks = await _taskController.getTask();
  54. // taskList.assignAll(tasks.map((data) => TaskModel.fromJson(data)).toList());
  55. // }
  56. _appBar() {
  57. return AppBar(
  58. elevation: 0,
  59. backgroundColor: context.theme.backgroundColor,
  60. leading: GestureDetector(
  61. onTap: () {
  62. ThemeService().switchTheme();
  63. notifyHelper.displayNotification(
  64. title: "Theme Change",
  65. body: Get.isDarkMode
  66. ? "Activated Light Mode"
  67. : "Activated Dark Mode");
  68. },
  69. child: Icon(
  70. Get.isDarkMode ? Icons.wb_sunny_outlined : Icons.nightlight_round,
  71. size: 20,
  72. color: Get.isDarkMode ? Colors.white : Colors.black),
  73. ),
  74. actions: const [
  75. CircleAvatar(
  76. backgroundImage: AssetImage(profile),
  77. ),
  78. SizedBox(
  79. width: 20,
  80. ),
  81. ],
  82. );
  83. }
  84. _appTaskBar() {
  85. return Container(
  86. margin: const EdgeInsets.only(left: 20, right: 20, top: 10),
  87. child: Row(
  88. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  89. children: [
  90. Container(
  91. child: Column(
  92. crossAxisAlignment: CrossAxisAlignment.start,
  93. children: [
  94. Text(
  95. DateFormat.yMMMd().format(DateTime.now()),
  96. style: headingStyle,
  97. ),
  98. Text(
  99. "Today",
  100. style: subHeadingStyle,
  101. )
  102. ],
  103. ),
  104. ),
  105. MyButton(
  106. label: "+ Add Task",
  107. onTap: () async {
  108. await Get.to(AddTaskScreen());
  109. _taskController.getTask();
  110. },
  111. )
  112. ],
  113. ),
  114. );
  115. }
  116. _addDateBar() {
  117. return Container(
  118. margin: const EdgeInsets.only(top: 20, left: 20),
  119. child: DatePicker(
  120. DateTime.now(),
  121. height: 100,
  122. width: 80,
  123. initialSelectedDate: DateTime.now(),
  124. selectionColor: primaryClr,
  125. selectedTextColor: Colors.white,
  126. dateTextStyle: datePickerTextStyle,
  127. dayTextStyle: datePickerTextStyle,
  128. monthTextStyle: datePickerTextStyle,
  129. onDateChange: (Date) {
  130. setState(() {
  131. _selectedDate = Date;
  132. });
  133. },
  134. ),
  135. );
  136. }
  137. _showTsks() {
  138. return Expanded(child: Obx(
  139. () {
  140. return ListView.builder(
  141. itemCount: _taskController.taskList.length,
  142. itemBuilder: (_, index) {
  143. TaskModel taskModel = _taskController.taskList[index];
  144. print(taskModel.toJson());
  145. if (taskModel.repeat == "Daily") {
  146. DateTime date =
  147. DateFormat.jm().parse(taskModel.startTime.toString());
  148. var myTime = DateFormat("HH:mm").format(date);
  149. print(myTime);
  150. notifyHelper.scheduledNotification(
  151. int.parse(myTime.toString().split(":")[0]),
  152. int.parse(myTime.toString().split(":")[1]),
  153. taskModel);
  154. return _setTaskView(index, taskModel);
  155. }
  156. // else if (taskModel.repeat == "None" &&
  157. // taskModel.date == DateFormat.yMd().format(_today)) {
  158. // print("Show task notification " +
  159. // isShowTaskNotification.toString());
  160. // if (isShowTaskNotification) {
  161. // notifyHelper.displayNotification(
  162. // id: taskModel.id,
  163. // title: taskModel.title,
  164. // body: taskModel.note);
  165. // }
  166. // }
  167. if (taskModel.date == DateFormat.yMd().format(_selectedDate)) {
  168. return _setTaskView(index, taskModel);
  169. } else {
  170. return Container();
  171. }
  172. });
  173. },
  174. ));
  175. }
  176. _setTaskView(int index, TaskModel taskModel) {
  177. return AnimationConfiguration.staggeredList(
  178. position: index,
  179. child: SlideAnimation(
  180. child: FlipAnimation(
  181. delay: Duration(milliseconds: 200),
  182. child: Row(
  183. children: [
  184. GestureDetector(
  185. onTap: () {
  186. _showBottomSheet(context, taskModel);
  187. },
  188. child: TaskTile(taskModel),
  189. )
  190. ],
  191. )),
  192. ));
  193. }
  194. _showBottomSheet(BuildContext context, TaskModel taskModel) {
  195. Get.bottomSheet(Container(
  196. padding: const EdgeInsets.only(top: 4),
  197. height: taskModel.isCompleted == 1
  198. ? MediaQuery.of(context).size.height * 0.24
  199. : MediaQuery.of(context).size.height * 0.32,
  200. color: Get.isDarkMode ? darkgratClr : Colors.white,
  201. child: Column(children: [
  202. Container(
  203. height: 6,
  204. width: 120,
  205. decoration: BoxDecoration(
  206. borderRadius: BorderRadius.circular(10),
  207. color: Get.isDarkMode ? Colors.grey[600] : Colors.grey[300]),
  208. ),
  209. Spacer(),
  210. taskModel.isCompleted == 1
  211. ? Container()
  212. : _bottomSheetButton(
  213. label: "Task Completed",
  214. onTap: () {
  215. _taskController.updateTaskIsComplete(taskModel.id!);
  216. Get.back();
  217. },
  218. clr: primaryClr,
  219. context: context),
  220. _bottomSheetButton(
  221. label: "Delete Task",
  222. onTap: () {
  223. _taskController.delete(taskModel);
  224. Get.back();
  225. },
  226. clr: Colors.red[600]!,
  227. context: context),
  228. SizedBox(
  229. height: 10,
  230. ),
  231. _bottomSheetButton(
  232. label: "Close",
  233. isClose: true,
  234. onTap: () {
  235. Get.back();
  236. },
  237. clr: Colors.white,
  238. context: context),
  239. SizedBox(
  240. height: 20,
  241. )
  242. ]),
  243. ));
  244. }
  245. _bottomSheetButton(
  246. {required String label,
  247. required Function()? onTap,
  248. required Color clr,
  249. bool isClose = false,
  250. required BuildContext context}) {
  251. return GestureDetector(
  252. onTap: onTap,
  253. child: Container(
  254. margin: const EdgeInsets.symmetric(vertical: 4),
  255. height: 55,
  256. width: MediaQuery.of(context).size.width * 0.9,
  257. decoration: BoxDecoration(
  258. border: Border.all(
  259. width: 2,
  260. color: isClose == true
  261. ? Get.isDarkMode
  262. ? Colors.grey[600]!
  263. : Colors.grey[300]!
  264. : clr),
  265. borderRadius: BorderRadius.circular(20),
  266. color: isClose == true ? Colors.transparent : clr,
  267. ),
  268. child: Center(
  269. child: Text(
  270. label,
  271. style:
  272. isClose ? titleStyle : titleStyle.copyWith(color: Colors.white),
  273. ),
  274. ),
  275. ),
  276. );
  277. }
  278. }