import 'package:flutter/material.dart'; import 'package:cpd/database/habit.dart'; import '../database/todo_interface.dart'; import '../pages/iconpage.dart'; class EditHabitDialog extends StatefulWidget { final Habit todo; final ToDoInterface todoDB; final Function fetchTodos; EditHabitDialog({ required this.todo, required this.todoDB, required this.fetchTodos, }); @override _EditHabitDialogState createState() => _EditHabitDialogState(); } class _EditHabitDialogState extends State { late String newTitle; 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; print("Titel geändert"); }, decoration: const InputDecoration( icon: Icon(Icons.title), labelText: "Title", hintText: "Enter task title", ), onFieldSubmitted: (_) => _submitForm(context, widget.todo, newTitle, newSubtitle), ), TextFormField( initialValue: newSubtitle, onChanged: (value) { newSubtitle = value; 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(() { 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, 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(); } }