import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../models/todo.dart'; import '../providers/todo_provider.dart'; class ToDoForm extends StatefulWidget { @override _ToDoFormState createState() => _ToDoFormState(); } class _ToDoFormState extends State { final _titleController = TextEditingController(); final _descriptionController = TextEditingController(); DateTime? _selectedDate; String _selectedPriority = 'Low'; bool get _isFormValid => _titleController.text.isNotEmpty && _selectedDate != null; void _submitForm() { if (!_isFormValid) return; var newTodo = ToDo( title: _titleController.text, description: _descriptionController.text.trim().isEmpty ? '' : _descriptionController.text, deadline: _selectedDate!, priority: _selectedPriority, ); Provider.of(context, listen: false).addTask(newTodo); Navigator.of(context).pop(); } @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ // Title input TextField( controller: _titleController, decoration: InputDecoration( labelText: 'Title *', hintText: 'Enter task title', ), onChanged: (_) { setState(() {}); }, ), TextField( controller: _descriptionController, decoration: InputDecoration( labelText: 'Description', hintText: 'Enter task description (optional)', ), ), // Deadline picker Row( children: [ TextButton( onPressed: () async { DateTime? picked = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime.now(), lastDate: DateTime(2101), ); if (picked != null) { setState(() { _selectedDate = picked; }); } }, child: Text( _selectedDate == null ? 'Select Deadline *' : '${_selectedDate!.day}/${_selectedDate!.month}/${_selectedDate!.year}', ), ), Spacer(), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Priority'), DropdownButton( value: _selectedPriority, onChanged: (newValue) { setState(() { _selectedPriority = newValue!; }); }, items: ['Low', 'Medium', 'High'] .map>((String value) { return DropdownMenuItem( value: value, child: Text(value), ); }).toList(), ), ], ), ], ), // "Create" and "Cancel" buttons SizedBox(height: 8), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ ElevatedButton( onPressed: () => Navigator.of(context).pop(), child: Text('Cancel'), ), ElevatedButton( onPressed: _isFormValid ? _submitForm : null, style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (Set states) { return _isFormValid ? Colors.blue : Colors.grey; }, ), ), child: Text('Create Task'), ), ], ), // Pflichtfelder-Hinweis Padding( padding: const EdgeInsets.only(top: 16.0), child: Text( '* required fields', style: TextStyle(fontSize: 12, color: Colors.grey), ), ), ], ), ); } }