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.

333 lines
9.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:intl/intl.dart';
  4. import 'package:sqflite_pust_local_notification/controllers/task_controller.dart';
  5. import 'package:sqflite_pust_local_notification/models/task_model.dart';
  6. import 'package:sqflite_pust_local_notification/utils/colors.dart';
  7. import 'package:sqflite_pust_local_notification/utils/textStyles.dart';
  8. import 'package:sqflite_pust_local_notification/widgets/my_button.dart';
  9. import '../services/notification_service.dart';
  10. import '../utils/assets_string.dart';
  11. import '../widgets/my_input_field.dart';
  12. class AddTaskScreen extends StatefulWidget {
  13. const AddTaskScreen({Key? key}) : super(key: key);
  14. @override
  15. State<AddTaskScreen> createState() => _AddTaskScreenState();
  16. }
  17. class _AddTaskScreenState extends State<AddTaskScreen> {
  18. final TaskController _taskController = Get.put(TaskController());
  19. final TextEditingController _titleController = TextEditingController();
  20. final TextEditingController _noteController = TextEditingController();
  21. DateTime _selectedDate = DateTime.now();
  22. String _endTime = "9:30 PM";
  23. String _startTime = DateFormat("hh:mm a").format(DateTime.now()).toString();
  24. int _selectedRemind = 5;
  25. List<int> remindList = [
  26. 5,
  27. 10,
  28. 15,
  29. 20,
  30. ];
  31. String _selectedRepeat = "None";
  32. List<String> repeatList = [
  33. "None",
  34. "Daily",
  35. "Weekly",
  36. "Monthly",
  37. ];
  38. int _selectedColor = 0;
  39. var notifyHelper;
  40. @override
  41. void initState() {
  42. // TODO: implement initState
  43. super.initState();
  44. notifyHelper = NotifyHelper();
  45. notifyHelper.initializeNotification();
  46. notifyHelper.requestIOSPermissions();
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return Scaffold(
  51. backgroundColor: context.theme.backgroundColor,
  52. appBar: _appBar(context),
  53. body: Container(
  54. padding: const EdgeInsets.only(left: 20, right: 20),
  55. child: SingleChildScrollView(
  56. child: Column(children: [
  57. Text(
  58. "Add Task",
  59. style: headingStyle,
  60. ),
  61. MyInputField(
  62. title: "Title",
  63. hint: "Enter your title",
  64. controller: _titleController,
  65. ),
  66. MyInputField(
  67. title: "Note",
  68. hint: "Enter your note",
  69. controller: _noteController,
  70. ),
  71. MyInputField(
  72. title: "Date",
  73. hint: DateFormat.yMd().format(_selectedDate),
  74. widget: IconButton(
  75. icon: Icon(Icons.calendar_today_outlined),
  76. color: Colors.grey,
  77. onPressed: () {
  78. _getDateFromUser();
  79. },
  80. ),
  81. ),
  82. Row(
  83. children: [
  84. Expanded(
  85. child: MyInputField(
  86. title: "Start Time",
  87. hint: _startTime,
  88. widget: IconButton(
  89. icon: Icon(Icons.access_time_outlined),
  90. onPressed: () {
  91. _getTimeFromUser(isStartTime: true);
  92. },
  93. ),
  94. )),
  95. SizedBox(
  96. width: 10,
  97. ),
  98. Expanded(
  99. child: MyInputField(
  100. title: "End Time",
  101. hint: _endTime,
  102. widget: IconButton(
  103. icon: Icon(Icons.access_time_outlined),
  104. onPressed: () {
  105. _getTimeFromUser(isStartTime: false);
  106. },
  107. ),
  108. )),
  109. ],
  110. ),
  111. MyInputField(
  112. title: "Remind",
  113. hint: "$_selectedRemind minutes early",
  114. widget: DropdownButton(
  115. icon: Icon(
  116. Icons.keyboard_arrow_down,
  117. color: Colors.grey,
  118. ),
  119. iconSize: 31,
  120. elevation: 4,
  121. style: subTitleStyle,
  122. underline: Container(height: 0),
  123. onChanged: (String? newValue) {
  124. setState(() {
  125. _selectedRemind = int.parse(newValue!);
  126. });
  127. },
  128. items: remindList.map<DropdownMenuItem<String>>((int value) {
  129. return DropdownMenuItem<String>(
  130. value: value.toString(),
  131. child: Text(value.toString()),
  132. );
  133. }).toList(),
  134. ),
  135. ),
  136. MyInputField(
  137. title: "Repeat",
  138. hint: "$_selectedRepeat",
  139. widget: DropdownButton(
  140. icon: Icon(
  141. Icons.keyboard_arrow_down,
  142. color: Colors.grey,
  143. ),
  144. iconSize: 31,
  145. elevation: 4,
  146. style: subTitleStyle,
  147. underline: Container(height: 0),
  148. onChanged: (String? newValue) {
  149. setState(() {
  150. _selectedRepeat = newValue!;
  151. });
  152. },
  153. items: repeatList.map<DropdownMenuItem<String>>((String value) {
  154. return DropdownMenuItem<String>(
  155. value: value.toString(),
  156. child: Text(
  157. value.toString(),
  158. style: TextStyle(color: Colors.grey),
  159. ),
  160. );
  161. }).toList(),
  162. ),
  163. ),
  164. SizedBox(
  165. height: 10,
  166. ),
  167. Row(
  168. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  169. crossAxisAlignment: CrossAxisAlignment.center,
  170. children: [
  171. _colorPallete(),
  172. MyButton(label: "Create Task", onTap: () => _validateDate())
  173. ],
  174. )
  175. ]),
  176. ),
  177. ),
  178. );
  179. }
  180. _validateDate() {
  181. if (_titleController.text.isNotEmpty && _noteController.text.isNotEmpty) {
  182. _addTaskToDb();
  183. notifyHelper.displayNotification(
  184. title: "Add Task", body: "A new task is added");
  185. Get.back();
  186. } else if (_titleController.text.isEmpty || _noteController.text.isEmpty) {
  187. Get.snackbar(
  188. "Required",
  189. "All fields are required",
  190. snackPosition: SnackPosition.BOTTOM,
  191. backgroundColor: Colors.white,
  192. colorText: pinkClr,
  193. icon: Icon(
  194. Icons.warning_amber_rounded,
  195. color: Colors.red,
  196. ),
  197. );
  198. }
  199. }
  200. _addTaskToDb() async {
  201. int value = await _taskController.addtask(
  202. task: TaskModel(
  203. note: _noteController.text,
  204. title: _titleController.text,
  205. date: DateFormat.yMd().format(_selectedDate),
  206. startTime: _startTime,
  207. endTime: _endTime,
  208. remind: _selectedRemind,
  209. repeat: _selectedRepeat,
  210. color: _selectedColor,
  211. isCompleted: 0));
  212. print("Task Added :" + value.toString());
  213. }
  214. _colorPallete() {
  215. return Column(
  216. crossAxisAlignment: CrossAxisAlignment.start,
  217. children: [
  218. Text(
  219. "Color",
  220. style: titleStyle,
  221. ),
  222. SizedBox(
  223. height: 8,
  224. ),
  225. Wrap(
  226. children: List<Widget>.generate(3, (int index) {
  227. return GestureDetector(
  228. onTap: () {
  229. setState(() {
  230. _selectedColor = index;
  231. });
  232. },
  233. child: Padding(
  234. padding: const EdgeInsets.only(right: 8.0),
  235. child: CircleAvatar(
  236. child: _selectedColor == index
  237. ? Icon(
  238. Icons.done,
  239. color: Colors.white,
  240. size: 16,
  241. )
  242. : Container(),
  243. radius: 14,
  244. backgroundColor: index == 0
  245. ? primaryClr
  246. : index == 1
  247. ? pinkClr
  248. : yellowClr,
  249. ),
  250. ),
  251. );
  252. }),
  253. )
  254. ],
  255. );
  256. }
  257. _appBar(BuildContext context) {
  258. return AppBar(
  259. elevation: 0,
  260. backgroundColor: context.theme.backgroundColor,
  261. leading: GestureDetector(
  262. onTap: () {
  263. Get.back();
  264. },
  265. child: Icon(Icons.arrow_back_ios,
  266. size: 20, color: Get.isDarkMode ? Colors.white : Colors.black),
  267. ),
  268. actions: [
  269. CircleAvatar(
  270. backgroundImage: AssetImage(profile),
  271. ),
  272. SizedBox(
  273. width: 20,
  274. ),
  275. ],
  276. );
  277. }
  278. _getDateFromUser() async {
  279. DateTime? _pickerDate = await showDatePicker(
  280. context: context,
  281. initialDate: DateTime.now(),
  282. firstDate: DateTime(2015),
  283. lastDate: DateTime(3000));
  284. if (_pickerDate != null) {
  285. setState(() {
  286. _selectedDate = _pickerDate;
  287. });
  288. } else {
  289. setState(() {
  290. _selectedDate = _selectedDate;
  291. });
  292. }
  293. }
  294. _getTimeFromUser({required bool isStartTime}) async {
  295. var pickedTime = await _showTimePicker();
  296. String _formatedTime = pickedTime.format(context);
  297. if (pickedTime == null) {
  298. } else if (isStartTime == true) {
  299. setState(() {
  300. _startTime = _formatedTime;
  301. });
  302. } else if (isStartTime == false) {
  303. setState(() {
  304. _endTime = _formatedTime;
  305. });
  306. }
  307. }
  308. _showTimePicker() {
  309. return showTimePicker(
  310. context: context,
  311. initialEntryMode: TimePickerEntryMode.input,
  312. initialTime: TimeOfDay(
  313. //_startTime ---->
  314. hour: int.parse(_startTime.split(":")[0]),
  315. minute: int.parse(_startTime.split(":")[1].split(" ")[0])));
  316. }
  317. }