cpd/lib/widgets/edithabitDialog.dart

134 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:cpd/database/habit.dart';
import 'package:cpd/database/todo_db.dart';
import '../pages/iconpage.dart';
class EditHabitDialog extends StatefulWidget {
final Habit todo;
final TodoDB todoDB;
final Function fetchTodos;
EditHabitDialog({
required this.todo,
required this.todoDB,
required this.fetchTodos,
});
@override
_EditHabitDialogState createState() => _EditHabitDialogState();
}
class _EditHabitDialogState extends State<EditHabitDialog> {
late String newTitle;
2024-05-24 00:01:08 +02:00
late String newSubtitle;
late IconData newIcon;
@override
void initState() {
super.initState();
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;
2024-05-24 00:01:08 +02:00
print("Titel geändert");
},
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
TextFormField(
initialValue: newSubtitle ?? '',
onChanged: (value) {
newSubtitle = value;
2024-05-24 00:01:08 +02:00
print("Untertitel geändert");
},
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(
builder: (context) => IconPage(selectedIcon: newIcon,
onIconSelected: (icon) {
setState(() {
2024-05-24 00:01:08 +02:00
print("Icon geändert");
newIcon = icon;
});
},)
),
);
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,
);
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 {
print('New Title: $newTitle');
print('New Subtitle: $newSubtitle');
print('New Icon: $newIcon');
await widget.todoDB.update(
id: todo.id,
title: newTitle,
subtitle: newSubtitle,
icon: newIcon,
);
widget.fetchTodos();
Navigator.of(context).pop();
}
}