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 AddTodoModal { static void add(BuildContext context) { showModalBottomSheet( context: context, showDragHandle: true, useSafeArea: true, scrollControlDisabledMaxHeightRatio: 1, builder: (context) => const ModalCreateTodo(), ); } } class ModalCreateTodo extends StatefulWidget { const ModalCreateTodo({super.key}); @override State createState() => _ModalCreateTodoState(); } class _ModalCreateTodoState extends State { String tempValue = ""; DateTime tempDateSelect = DateTime.now(); TimeOfDay tempTimeSelect = TimeOfDay.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); } @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(left: 30, right: 30), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ TextFormField( autofocus: true, decoration: const InputDecoration(labelText: "Title"), onChanged: (value) => tempValue = value, onFieldSubmitted: (value) { context.read().addTodo(value, combineTime()); Navigator.pop(context); }, ), const SizedBox(height: 20), FilledButton.tonal( onPressed: () async { tempDateSelect = (await DatePickerModal.add(context, 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().addTodo(tempValue, combineTime()); } Navigator.pop(context); }, ) ], ), ); } }