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; const EditHabitDialog({super.key, 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; }, 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; }, 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(() { 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, ); if (!context.mounted) return; widget.todo.updateIcon(newIcon.codePoint, newIcon.fontFamily!); widget.fetchTodos(); Navigator.of(context).pop(); }, child: const Text('Save'), ), ], ); } void _submitForm(BuildContext context, Habit todo, String newTitle, String newSubtitle) async { await widget.todoDB.update( id: todo.id, title: newTitle, subtitle: newSubtitle, icon: newIcon, ); if (!context.mounted) return; widget.todo.updateIcon(newIcon.codePoint, newIcon.fontFamily!); widget.fetchTodos(); Navigator.of(context).pop(); } }