Files
todo-flutter/lib/modals/edittodomodal.dart
T
2024-09-15 23:40:00 +02:00

149 lines
4.8 KiB
Dart

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<ModalEditTodo> createState() => _ModalEditTodoState();
}
class _ModalEditTodoState extends State<ModalEditTodo> {
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<TodoProvider>().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: "Delete",
label: Text("Delete"),
icon: Icon(Icons.delete)),
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<TodoProvider>().updateTodo(widget.oldValue, tempValue, combineTime());
} else if (p0.last == "Delete") {
context.read<TodoProvider>().removeTodo(widget.oldValue);
}
Navigator.pop(context);
},
)
],
),
);
}
}