2024-05-19 22:08:04 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:cpd/database/habit.dart';
|
2024-05-28 21:59:44 +02:00
|
|
|
import '../database/todo_interface.dart';
|
2024-05-19 22:08:04 +02:00
|
|
|
import '../pages/iconpage.dart';
|
|
|
|
|
|
|
|
class EditHabitDialog extends StatefulWidget {
|
|
|
|
final Habit todo;
|
2024-05-28 21:59:44 +02:00
|
|
|
final ToDoInterface todoDB;
|
2024-05-19 22:08:04 +02:00
|
|
|
final Function fetchTodos;
|
|
|
|
|
2024-06-16 17:12:28 +02:00
|
|
|
const EditHabitDialog({
|
|
|
|
super.key,
|
2024-05-19 22:08:04 +02:00
|
|
|
required this.todo,
|
|
|
|
required this.todoDB,
|
|
|
|
required this.fetchTodos,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
2024-06-08 14:22:18 +02:00
|
|
|
EditHabitDialogState createState() => EditHabitDialogState();
|
2024-05-19 22:08:04 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:22:18 +02:00
|
|
|
class EditHabitDialogState extends State<EditHabitDialog> {
|
2024-05-19 22:08:04 +02:00
|
|
|
late String newTitle;
|
2024-05-24 00:01:08 +02:00
|
|
|
late String newSubtitle;
|
2024-05-19 22:08:04 +02:00
|
|
|
late IconData newIcon;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-06-16 17:12:28 +02:00
|
|
|
// Initialisierung mit den aktuellen Daten der Gewohnheit
|
2024-05-19 22:08:04 +02:00
|
|
|
newTitle = widget.todo.title;
|
|
|
|
newSubtitle = widget.todo.subtitle;
|
|
|
|
newIcon = widget.todo.icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: const Text('Edit Habit'),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
TextFormField(
|
|
|
|
initialValue: newTitle,
|
|
|
|
onChanged: (value) {
|
|
|
|
newTitle = value;
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.title),
|
|
|
|
labelText: "Title",
|
|
|
|
hintText: "Enter task title",
|
|
|
|
),
|
|
|
|
onFieldSubmitted: (_) =>
|
|
|
|
_submitForm(context, widget.todo, newTitle, newSubtitle),
|
|
|
|
),
|
2024-05-24 00:01:08 +02:00
|
|
|
|
2024-05-19 22:08:04 +02:00
|
|
|
TextFormField(
|
2024-05-28 21:59:44 +02:00
|
|
|
initialValue: newSubtitle,
|
2024-05-19 22:08:04 +02:00
|
|
|
onChanged: (value) {
|
|
|
|
newSubtitle = value;
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.title),
|
|
|
|
labelText: "Subtitle",
|
|
|
|
hintText: "Enter task subtitle",
|
|
|
|
),
|
|
|
|
onFieldSubmitted: (_) =>
|
|
|
|
_submitForm(context, widget.todo, newTitle, newSubtitle),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(newIcon),
|
|
|
|
onPressed: () async {
|
|
|
|
IconData? selectedIcon = await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
2024-06-16 17:12:28 +02:00
|
|
|
builder: (context) => IconPage(selectedIcon: newIcon,
|
|
|
|
onIconSelected: (icon) {
|
|
|
|
setState(() {
|
|
|
|
newIcon = icon;
|
|
|
|
});
|
|
|
|
},)
|
2024-05-19 22:08:04 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if (selectedIcon != null) {
|
|
|
|
setState(() {
|
|
|
|
newIcon = selectedIcon;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
widget.fetchTodos();
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
await widget.todoDB.update(
|
|
|
|
id: widget.todo.id,
|
|
|
|
title: newTitle,
|
|
|
|
subtitle: newSubtitle,
|
|
|
|
icon: newIcon,
|
|
|
|
);
|
2024-06-08 14:22:18 +02:00
|
|
|
if (!context.mounted) return;
|
2024-06-09 17:45:17 +02:00
|
|
|
widget.todo.updateIcon(newIcon.codePoint, newIcon.fontFamily!);
|
2024-05-19 22:08:04 +02:00
|
|
|
widget.fetchTodos();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _submitForm(BuildContext context, Habit todo, String newTitle,
|
2024-05-24 00:01:08 +02:00
|
|
|
String newSubtitle) async {
|
|
|
|
|
2024-05-19 22:08:04 +02:00
|
|
|
await widget.todoDB.update(
|
|
|
|
id: todo.id,
|
|
|
|
title: newTitle,
|
|
|
|
subtitle: newSubtitle,
|
|
|
|
icon: newIcon,
|
|
|
|
);
|
2024-06-08 14:22:18 +02:00
|
|
|
if (!context.mounted) return;
|
2024-06-09 17:45:17 +02:00
|
|
|
widget.todo.updateIcon(newIcon.codePoint, newIcon.fontFamily!);
|
2024-05-19 22:08:04 +02:00
|
|
|
widget.fetchTodos();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
}
|