import 'package:flutter/material.dart'; import 'package:cpd/pages/iconpage.dart'; class AddHabitPopup extends StatefulWidget { //Callback nimmt Titel, Untertitel und Icon als Parameter der neuen Gewohnheit an final void Function(String, String, IconData) onSubmit; @override AddHabitPopupState createState() => AddHabitPopupState(); const AddHabitPopup({ super.key, required this.onSubmit, }); } class AddHabitPopupState extends State { final titleController = TextEditingController(); final subtitleController = TextEditingController(); late IconData selectedIcon; @override void initState() { super.initState(); titleController.text = ""; subtitleController.text = ""; selectedIcon = Icons.favorite; } final _formKey = GlobalKey(); @override Widget build(BuildContext context) { return AlertDialog( title: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ IconButton( icon: const Icon(Icons.close, color: Colors.red), onPressed: () => Navigator.pop(context), ), const Expanded( child: Text( 'Add task', textAlign: TextAlign.center, ), ) ], ), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Divider(thickness: 3), Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, children: [ TextFormField( autofocus: true, controller: titleController, decoration: const InputDecoration( icon: Icon(Icons.title), labelText: "Title", hintText: "Enter task title", ), validator: (value) => value != null && value.isEmpty ? "Please enter a title" : null, onFieldSubmitted: (_) => _submitForm(), ), TextFormField( controller: subtitleController, decoration: const InputDecoration( icon: Icon(Icons.description), labelText: "Description", hintText: "Describe how you plan to achieve your goal", ), validator: (value) => value != null && value.isEmpty ? "Please enter a subtitle" : null, onFieldSubmitted: (_) => _submitForm(), ), ], ), ), const SizedBox(height: 10,), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( onPressed: _submitForm, key: const Key('Save'), child: const Text('Save'), ), ElevatedButton( onPressed: () async { selectedIcon= await Navigator.push( context, MaterialPageRoute( builder: (context) => IconPage(selectedIcon: selectedIcon, onIconSelected: (icon) { setState(() { selectedIcon = icon; }); }, ) ), ); }, child: const Text('Select Icon'), ), ] ), ], ), ); } void _submitForm() { if (_formKey.currentState!.validate()) { widget.onSubmit(titleController.text, subtitleController.text, selectedIcon); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${titleController.text} saved!'), ), ); } } }