import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:sqflite_pust_local_notification/controllers/task_controller.dart'; import 'package:sqflite_pust_local_notification/models/task_model.dart'; import 'package:sqflite_pust_local_notification/utils/colors.dart'; import 'package:sqflite_pust_local_notification/utils/textStyles.dart'; import 'package:sqflite_pust_local_notification/widgets/my_button.dart'; import '../services/notification_service.dart'; import '../utils/assets_string.dart'; import '../widgets/my_input_field.dart'; class AddTaskScreen extends StatefulWidget { const AddTaskScreen({Key? key}) : super(key: key); @override State createState() => _AddTaskScreenState(); } class _AddTaskScreenState extends State { final TaskController _taskController = Get.put(TaskController()); final TextEditingController _titleController = TextEditingController(); final TextEditingController _noteController = TextEditingController(); DateTime _selectedDate = DateTime.now(); String _endTime = "9:30 PM"; String _startTime = DateFormat("hh:mm a").format(DateTime.now()).toString(); int _selectedRemind = 5; List remindList = [ 5, 10, 15, 20, ]; String _selectedRepeat = "None"; List repeatList = [ "None", "Daily", "Weekly", "Monthly", ]; int _selectedColor = 0; var notifyHelper; @override void initState() { // TODO: implement initState super.initState(); notifyHelper = NotifyHelper(); notifyHelper.initializeNotification(); notifyHelper.requestIOSPermissions(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: context.theme.backgroundColor, appBar: _appBar(context), body: Container( padding: const EdgeInsets.only(left: 20, right: 20), child: SingleChildScrollView( child: Column(children: [ Text( "Add Task", style: headingStyle, ), MyInputField( title: "Title", hint: "Enter your title", controller: _titleController, ), MyInputField( title: "Note", hint: "Enter your note", controller: _noteController, ), MyInputField( title: "Date", hint: DateFormat.yMd().format(_selectedDate), widget: IconButton( icon: Icon(Icons.calendar_today_outlined), color: Colors.grey, onPressed: () { _getDateFromUser(); }, ), ), Row( children: [ Expanded( child: MyInputField( title: "Start Time", hint: _startTime, widget: IconButton( icon: Icon(Icons.access_time_outlined), onPressed: () { _getTimeFromUser(isStartTime: true); }, ), )), SizedBox( width: 10, ), Expanded( child: MyInputField( title: "End Time", hint: _endTime, widget: IconButton( icon: Icon(Icons.access_time_outlined), onPressed: () { _getTimeFromUser(isStartTime: false); }, ), )), ], ), MyInputField( title: "Remind", hint: "$_selectedRemind minutes early", widget: DropdownButton( icon: Icon( Icons.keyboard_arrow_down, color: Colors.grey, ), iconSize: 31, elevation: 4, style: subTitleStyle, underline: Container(height: 0), onChanged: (String? newValue) { setState(() { _selectedRemind = int.parse(newValue!); }); }, items: remindList.map>((int value) { return DropdownMenuItem( value: value.toString(), child: Text(value.toString()), ); }).toList(), ), ), MyInputField( title: "Repeat", hint: "$_selectedRepeat", widget: DropdownButton( icon: Icon( Icons.keyboard_arrow_down, color: Colors.grey, ), iconSize: 31, elevation: 4, style: subTitleStyle, underline: Container(height: 0), onChanged: (String? newValue) { setState(() { _selectedRepeat = newValue!; }); }, items: repeatList.map>((String value) { return DropdownMenuItem( value: value.toString(), child: Text( value.toString(), style: TextStyle(color: Colors.grey), ), ); }).toList(), ), ), SizedBox( height: 10, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ _colorPallete(), MyButton(label: "Create Task", onTap: () => _validateDate()) ], ) ]), ), ), ); } _validateDate() { if (_titleController.text.isNotEmpty && _noteController.text.isNotEmpty) { _addTaskToDb(); notifyHelper.displayNotification( title: "Add Task", body: "A new task is added"); Get.back(); } else if (_titleController.text.isEmpty || _noteController.text.isEmpty) { Get.snackbar( "Required", "All fields are required", snackPosition: SnackPosition.BOTTOM, backgroundColor: Colors.white, colorText: pinkClr, icon: Icon( Icons.warning_amber_rounded, color: Colors.red, ), ); } } _addTaskToDb() async { int value = await _taskController.addtask( task: TaskModel( note: _noteController.text, title: _titleController.text, date: DateFormat.yMd().format(_selectedDate), startTime: _startTime, endTime: _endTime, remind: _selectedRemind, repeat: _selectedRepeat, color: _selectedColor, isCompleted: 0)); print("Task Added :" + value.toString()); } _colorPallete() { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Color", style: titleStyle, ), SizedBox( height: 8, ), Wrap( children: List.generate(3, (int index) { return GestureDetector( onTap: () { setState(() { _selectedColor = index; }); }, child: Padding( padding: const EdgeInsets.only(right: 8.0), child: CircleAvatar( child: _selectedColor == index ? Icon( Icons.done, color: Colors.white, size: 16, ) : Container(), radius: 14, backgroundColor: index == 0 ? primaryClr : index == 1 ? pinkClr : yellowClr, ), ), ); }), ) ], ); } _appBar(BuildContext context) { return AppBar( elevation: 0, backgroundColor: context.theme.backgroundColor, leading: GestureDetector( onTap: () { Get.back(); }, child: Icon(Icons.arrow_back_ios, size: 20, color: Get.isDarkMode ? Colors.white : Colors.black), ), actions: [ CircleAvatar( backgroundImage: AssetImage(profile), ), SizedBox( width: 20, ), ], ); } _getDateFromUser() async { DateTime? _pickerDate = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(2015), lastDate: DateTime(3000)); if (_pickerDate != null) { setState(() { _selectedDate = _pickerDate; }); } else { setState(() { _selectedDate = _selectedDate; }); } } _getTimeFromUser({required bool isStartTime}) async { var pickedTime = await _showTimePicker(); String _formatedTime = pickedTime.format(context); if (pickedTime == null) { } else if (isStartTime == true) { setState(() { _startTime = _formatedTime; }); } else if (isStartTime == false) { setState(() { _endTime = _formatedTime; }); } } _showTimePicker() { return showTimePicker( context: context, initialEntryMode: TimePickerEntryMode.input, initialTime: TimeOfDay( //_startTime ----> hour: int.parse(_startTime.split(":")[0]), minute: int.parse(_startTime.split(":")[1].split(" ")[0]))); } }