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.

318 lines
9.3 KiB

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. }
  201. _colorPallete() {
  202. return Column(
  203. crossAxisAlignment: CrossAxisAlignment.start,
  204. children: [
  205. Text(
  206. "Color",
  207. style: titleStyle,
  208. ),
  209. SizedBox(
  210. height: 8,
  211. ),
  212. Wrap(
  213. children: List<Widget>.generate(3, (int index) {
  214. return GestureDetector(
  215. onTap: () {
  216. setState(() {
  217. _selectedColor = index;
  218. });
  219. },
  220. child: Padding(
  221. padding: const EdgeInsets.only(right: 8.0),
  222. child: CircleAvatar(
  223. child: _selectedColor == index
  224. ? Icon(
  225. Icons.done,
  226. color: Colors.white,
  227. size: 16,
  228. )
  229. : Container(),
  230. radius: 14,
  231. backgroundColor: index == 0
  232. ? primaryClr
  233. : index == 1
  234. ? pinkClr
  235. : yellowClr,
  236. ),
  237. ),
  238. );
  239. }),
  240. )
  241. ],
  242. );
  243. }
  244. _appBar(BuildContext context) {
  245. return AppBar(
  246. elevation: 0,
  247. backgroundColor: context.theme.backgroundColor,
  248. leading: GestureDetector(
  249. onTap: () {
  250. Get.back();
  251. },
  252. child: Icon(Icons.arrow_back_ios,
  253. size: 20, color: Get.isDarkMode ? Colors.white : Colors.black),
  254. ),
  255. actions: [
  256. CircleAvatar(
  257. backgroundImage: AssetImage(profile),
  258. ),
  259. SizedBox(
  260. width: 20,
  261. ),
  262. ],
  263. );
  264. }
  265. _getDateFromUser() async {
  266. DateTime? _pickerDate = await showDatePicker(
  267. context: context,
  268. initialDate: DateTime.now(),
  269. firstDate: DateTime(2015),
  270. lastDate: DateTime(3000));
  271. if (_pickerDate != null) {
  272. setState(() {
  273. _selectedDate = _pickerDate;
  274. });
  275. } else {
  276. setState(() {
  277. _selectedDate = _selectedDate;
  278. });
  279. }
  280. }
  281. _getTimeFromUser({required bool isStartTime}) async {
  282. var pickedTime = await _showTimePicker();
  283. String _formatedTime = pickedTime.format(context);
  284. if (pickedTime == null) {
  285. } else if (isStartTime == true) {
  286. setState(() {
  287. _startTime = _formatedTime;
  288. });
  289. } else if (isStartTime == false) {
  290. setState(() {
  291. _endTime = _formatedTime;
  292. });
  293. }
  294. }
  295. _showTimePicker() {
  296. return showTimePicker(
  297. context: context,
  298. initialEntryMode: TimePickerEntryMode.input,
  299. initialTime: TimeOfDay(
  300. //_startTime ---->
  301. hour: int.parse(_startTime.split(":")[0]),
  302. minute: int.parse(_startTime.split(":")[1].split(" ")[0])));
  303. }
  304. }