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.

259 lines
7.6 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
  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. var notifyHelper;
  26. @override
  27. void initState() {
  28. // TODO: implement initState
  29. super.initState();
  30. notifyHelper = NotifyHelper();
  31. notifyHelper.initializeNotification();
  32. notifyHelper.requestIOSPermissions();
  33. //_getTask();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return Scaffold(
  38. appBar: _appBar(),
  39. backgroundColor: context.theme.backgroundColor,
  40. body: Column(children: [
  41. _appTaskBar(),
  42. _addDateBar(),
  43. SizedBox(
  44. height: 20,
  45. ),
  46. _showTsks()
  47. ]),
  48. );
  49. }
  50. // _getTask() async {
  51. // var tasks = await _taskController.getTask();
  52. // taskList.assignAll(tasks.map((data) => TaskModel.fromJson(data)).toList());
  53. // }
  54. _appBar() {
  55. return AppBar(
  56. elevation: 0,
  57. backgroundColor: context.theme.backgroundColor,
  58. leading: GestureDetector(
  59. onTap: () {
  60. ThemeService().switchTheme();
  61. notifyHelper.displayNotification(
  62. title: "Theme Change",
  63. body: Get.isDarkMode
  64. ? "Activated Light Mode"
  65. : "Activated Dark Mode");
  66. notifyHelper.scheduledNotification();
  67. },
  68. child: Icon(
  69. Get.isDarkMode ? Icons.wb_sunny_outlined : Icons.nightlight_round,
  70. size: 20,
  71. color: Get.isDarkMode ? Colors.white : Colors.black),
  72. ),
  73. actions: const [
  74. CircleAvatar(
  75. backgroundImage: AssetImage(profile),
  76. ),
  77. SizedBox(
  78. width: 20,
  79. ),
  80. ],
  81. );
  82. }
  83. _appTaskBar() {
  84. return Container(
  85. margin: const EdgeInsets.only(left: 20, right: 20, top: 10),
  86. child: Row(
  87. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  88. children: [
  89. Container(
  90. child: Column(
  91. crossAxisAlignment: CrossAxisAlignment.start,
  92. children: [
  93. Text(
  94. DateFormat.yMMMd().format(DateTime.now()),
  95. style: headingStyle,
  96. ),
  97. Text(
  98. "Today",
  99. style: subHeadingStyle,
  100. )
  101. ],
  102. ),
  103. ),
  104. MyButton(
  105. label: "+ Add Task",
  106. onTap: () async {
  107. await Get.to(AddTaskScreen());
  108. _taskController.getTask();
  109. },
  110. )
  111. ],
  112. ),
  113. );
  114. }
  115. _addDateBar() {
  116. return Container(
  117. margin: const EdgeInsets.only(top: 20, left: 20),
  118. child: DatePicker(
  119. DateTime.now(),
  120. height: 100,
  121. width: 80,
  122. initialSelectedDate: DateTime.now(),
  123. selectionColor: primaryClr,
  124. selectedTextColor: Colors.white,
  125. dateTextStyle: datePickerTextStyle,
  126. dayTextStyle: datePickerTextStyle,
  127. monthTextStyle: datePickerTextStyle,
  128. onDateChange: (Date) {
  129. _selectedDate = Date;
  130. },
  131. ),
  132. );
  133. }
  134. _showTsks() {
  135. return Expanded(child: Obx(
  136. () {
  137. return ListView.builder(
  138. itemCount: _taskController.taskList.length,
  139. itemBuilder: (_, index) {
  140. print(
  141. "Task Count :" + _taskController.taskList.length.toString());
  142. return AnimationConfiguration.staggeredList(
  143. position: index,
  144. child: SlideAnimation(
  145. child: FadeInAnimation(
  146. child: Row(
  147. children: [
  148. GestureDetector(
  149. onTap: () {
  150. _showBottomSheet(
  151. context, _taskController.taskList[index]);
  152. },
  153. child: TaskTile(_taskController.taskList[index]),
  154. )
  155. ],
  156. )),
  157. ));
  158. });
  159. },
  160. ));
  161. }
  162. _showBottomSheet(BuildContext context, TaskModel taskModel) {
  163. Get.bottomSheet(Container(
  164. padding: const EdgeInsets.only(top: 4),
  165. height: taskModel.isCompleted == 1
  166. ? MediaQuery.of(context).size.height * 0.24
  167. : MediaQuery.of(context).size.height * 0.32,
  168. color: Get.isDarkMode ? darkgratClr : Colors.white,
  169. child: Column(children: [
  170. Container(
  171. height: 6,
  172. width: 120,
  173. decoration: BoxDecoration(
  174. borderRadius: BorderRadius.circular(10),
  175. color: Get.isDarkMode ? Colors.grey[600] : Colors.grey[300]),
  176. ),
  177. Spacer(),
  178. taskModel.isCompleted == 1
  179. ? Container()
  180. : _bottomSheetButton(
  181. label: "Task Completed",
  182. onTap: () {
  183. _taskController.updateTaskIsComplete(taskModel.id!);
  184. Get.back();
  185. },
  186. clr: primaryClr,
  187. context: context),
  188. _bottomSheetButton(
  189. label: "Delete Task",
  190. onTap: () {
  191. _taskController.delete(taskModel);
  192. Get.back();
  193. },
  194. clr: Colors.red[600]!,
  195. context: context),
  196. SizedBox(
  197. height: 10,
  198. ),
  199. _bottomSheetButton(
  200. label: "Close",
  201. isClose: true,
  202. onTap: () {
  203. Get.back();
  204. },
  205. clr: Colors.white,
  206. context: context),
  207. SizedBox(
  208. height: 20,
  209. )
  210. ]),
  211. ));
  212. }
  213. _bottomSheetButton(
  214. {required String label,
  215. required Function()? onTap,
  216. required Color clr,
  217. bool isClose = false,
  218. required BuildContext context}) {
  219. return GestureDetector(
  220. onTap: onTap,
  221. child: Container(
  222. margin: const EdgeInsets.symmetric(vertical: 4),
  223. height: 55,
  224. width: MediaQuery.of(context).size.width * 0.9,
  225. decoration: BoxDecoration(
  226. border: Border.all(
  227. width: 2,
  228. color: isClose == true
  229. ? Get.isDarkMode
  230. ? Colors.grey[600]!
  231. : Colors.grey[300]!
  232. : clr),
  233. borderRadius: BorderRadius.circular(20),
  234. color: isClose == true ? Colors.transparent : clr,
  235. ),
  236. child: Center(
  237. child: Text(
  238. label,
  239. style:
  240. isClose ? titleStyle : titleStyle.copyWith(color: Colors.white),
  241. ),
  242. ),
  243. ),
  244. );
  245. }
  246. }