import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cpd/controller/habitmanager.dart'; import 'package:cpd/model/habit_list.dart'; // save button + speichern durch enter class AddHabitPopup extends StatefulWidget { final Function? onHabitAdded; TextEditingController titleController = TextEditingController(); TextEditingController subtitleController = TextEditingController(); AddHabitPopup({this.onHabitAdded}); @override _AddHabitPopupState createState() => _AddHabitPopupState(); } class _AddHabitPopupState extends State { //habitmanager aufrufen Habitmanager habitmanager = Habitmanager(); String title = ''; String subtitle = ''; 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.of(context).pop(); }, ), const Expanded(child: Text( 'Add task', textAlign: TextAlign.center, ) ) ], ), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Divider(thickness: 3), const Text( 'Tasks start each day as incomplete.\n' 'Mark a task as done to keep your progress up.\n' 'CREATE YOUR OWN:', textAlign: TextAlign.start, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontFamily: 'Arial', fontSize: 14, ), ), Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, children: [ TextFormField( controller: widget.titleController, decoration: const InputDecoration( icon: Icon(Icons.title), labelText: "Title", hintText: "Enter task title", ), keyboardType: TextInputType.text, validator: (String? value) { return (value == null || value.trim().isEmpty) ? "Please enter a title" : null; }, ), TextFormField( controller: widget.subtitleController, decoration: const InputDecoration( icon: Icon(Icons.description), labelText: "Description", hintText: "Describe how you plan to achieve your goal", ), keyboardType: TextInputType.text, validator: (String? value) { return (value == null || value.trim().isEmpty) ? "Please enter a title" : null; }, ), // auswahl an icons zur verfügung stellen const SizedBox(height: 16.0), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { String title = widget.titleController.text; String subtitle = widget.subtitleController.text; habitmanager.addNewHabit(false, title, subtitle, const Icon(Icons.label_important)); print('Current list after add:'); habits.forEach((habit) { print('${habit.title}: ${habit.subtitle}'); }); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Form saved!'), ), ); widget.onHabitAdded?.call(); //fester schließen Navigator.of(context).pop(); // ui wird neu gezeichnet } }, child: const Text('Save'), ) ], ), ), ], ), ); } }