import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:todoapp/elems/todo.dart'; import 'package:todoapp/modals/datepickermodal.dart'; import 'package:todoapp/modals/timepickermodal.dart'; import 'package:todoapp/providers/todoprovider.dart'; class EditTodoModal { static void add(BuildContext context, String title, DateTime dueTime) { showModalBottomSheet( context: context, showDragHandle: true, useSafeArea: true, scrollControlDisabledMaxHeightRatio: 1, builder: (context) => ModalEditTodo( oldValue: title, initDate: DateTime(dueTime.year, dueTime.month, dueTime.day), initTime: TimeOfDay(hour: dueTime.hour, minute: dueTime.minute)), ); } } class ModalEditTodo extends StatefulWidget { const ModalEditTodo( {super.key, required this.oldValue, required this.initDate, required this.initTime}); final String oldValue; final DateTime initDate; final TimeOfDay initTime; @override State createState() => _ModalEditTodoState(); } class _ModalEditTodoState extends State { String tempValue = ""; TimeOfDay tempTimeSelect = TimeOfDay.now(); DateTime tempDateSelect = DateTime.now(); String formatNumber(int number) { if (number < 10) { return "0$number"; } return number.toString(); } DateTime combineTime() { return DateTime(tempDateSelect.year, tempDateSelect.month, tempDateSelect.day, tempTimeSelect.hour, tempTimeSelect.minute); } bool firstBuild = true; @override Widget build(BuildContext context) { if (firstBuild) { tempValue = widget.oldValue; tempDateSelect = widget.initDate; tempTimeSelect = widget.initTime; firstBuild = false; } return Padding( padding: const EdgeInsets.only(left: 30, right: 30), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ TextFormField( autofocus: true, initialValue: widget.oldValue, decoration: const InputDecoration(labelText: "Title"), onChanged: (value) => tempValue = value, onFieldSubmitted: (value) { context.read().updateTodo(widget.oldValue, value, combineTime()); Navigator.pop(context); }, ), const SizedBox(height: 20), FilledButton.tonal( onPressed: () async { tempDateSelect = (await DatePickerModal.add(context, tempDateSelect))!; print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); print(tempDateSelect); setState(() {}); }, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ const Icon(Icons.date_range), const SizedBox(width: 20), Text( "${tempDateSelect.year}-${formatNumber(tempDateSelect.month)}-${formatNumber(tempDateSelect.day)}") ], ), ), const SizedBox(height: 10), FilledButton.tonal( onPressed: () async { tempTimeSelect = (await TimePickerModal.add(context, tempTimeSelect))!; setState(() {}); }, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ const Icon(Icons.timer), const SizedBox(width: 20), Text( "${formatNumber(tempTimeSelect.hour)}:${formatNumber(tempTimeSelect.minute)}") ], ), ), const SizedBox(height: 20), SegmentedButton( segments: const [ ButtonSegment( value: "Cancel", label: Text("Cancel"), icon: Icon(Icons.cancel)), ButtonSegment( value: "Save", label: Text("Save"), icon: Icon(Icons.save)), ], selected: const {"None"}, style: ButtonStyle( iconColor: WidgetStatePropertyAll( Theme.of(context).colorScheme.primary)), onSelectionChanged: (p0) { if (p0.last == "Save") { context.read().updateTodo(widget.oldValue, tempValue, combineTime()); } Navigator.pop(context); }, ) ], ), ); } }