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.

320 lines
9.4 KiB

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