138 lines
4.5 KiB
Dart
138 lines
4.5 KiB
Dart
|
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<ToDoForm> {
|
||
|
final _titleController = TextEditingController();
|
||
|
final _descriptionController = TextEditingController();
|
||
|
DateTime? _selectedDate;
|
||
|
String _selectedPriority = 'Low';
|
||
|
|
||
|
bool get _isFormValid => _titleController.text.isNotEmpty && _selectedDate != null;
|
||
|
|
||
|
void _submitForm() {
|
||
|
if (!_isFormValid) return; // Ensure the form is valid before submitting
|
||
|
|
||
|
var newTodo = ToDo(
|
||
|
title: _titleController.text,
|
||
|
description: _descriptionController.text.trim().isEmpty ? '' : _descriptionController.text, // Korrektur hier
|
||
|
deadline: _selectedDate!,
|
||
|
priority: _selectedPriority,
|
||
|
);
|
||
|
|
||
|
Provider.of<ToDoProvider>(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(() {}); // Trigger UI update when title changes
|
||
|
},
|
||
|
),
|
||
|
// Description input (optional)
|
||
|
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'), // Label for Priority
|
||
|
DropdownButton<String>(
|
||
|
value: _selectedPriority,
|
||
|
onChanged: (newValue) {
|
||
|
setState(() {
|
||
|
_selectedPriority = newValue!;
|
||
|
});
|
||
|
},
|
||
|
items: ['Low', 'Medium', 'High']
|
||
|
.map<DropdownMenuItem<String>>((String value) {
|
||
|
return DropdownMenuItem<String>(
|
||
|
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<Color>(
|
||
|
(Set<MaterialState> states) {
|
||
|
return _isFormValid ? Colors.blue : Colors.grey; // Button enabled only if form is valid
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
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),
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|