CPD_2024_todo/lib/widgets/todo_form.dart

137 lines
4.3 KiB
Dart
Raw Normal View History

2024-10-22 20:42:08 +02:00
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;
2024-10-22 20:42:08 +02:00
var newTodo = ToDo(
title: _titleController.text,
description: _descriptionController.text.trim().isEmpty ? '' : _descriptionController.text,
2024-10-22 20:42:08 +02:00
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(() {});
2024-10-22 20:42:08 +02:00
},
),
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'),
2024-10-22 20:42:08 +02:00
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;
2024-10-22 20:42:08 +02:00
},
),
),
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),
),
),
],
),
);
}
}