cpd_2024_todo/lib/presentation/ToDoListScreen.dart

200 lines
6.6 KiB
Dart
Raw Normal View History

2024-10-22 10:38:22 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../buisness/ToDoItem.dart';
import '../buisness/ToDoProvider.dart';
import 'ToDoExpandableWidget.dart';
class ToDoListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
2024-10-30 23:41:56 +01:00
backgroundColor: Colors.grey[900],
2024-10-22 10:38:22 +02:00
appBar: AppBar(
2024-10-30 23:41:56 +01:00
backgroundColor: Colors.deepOrange[800],
title: const Text('To-Do List'),
titleSpacing: 150.0,
2024-10-22 10:38:22 +02:00
),
body: Consumer<ToDoProvider>(
builder: (context, toDoProvider, child) {
2024-10-30 23:41:56 +01:00
return Padding(
padding: const EdgeInsets.all(16),
child: ListView(
children: toDoProvider.toDoList.map((todo) {
return ToDoExpandableWidget(
toDoItem: todo,
id: todo.id!,
);
},
).toList()
),
2024-10-22 10:38:22 +02:00
);
2024-10-29 13:13:10 +01:00
}
2024-10-22 10:38:22 +02:00
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Sortier-Button unten links
FloatingActionButton(
onPressed: () => _showSortOptions(context),
backgroundColor: Colors.orange,
heroTag: null,
2024-10-30 23:41:56 +01:00
child: const Icon(Icons.sort), // Damit beide Floating Buttons keine Konflikte haben
2024-10-22 10:38:22 +02:00
),
// Hinzufügen-Button unten rechts
FloatingActionButton(
onPressed: () => _showAddToDoDialog(context),
2024-10-30 23:41:56 +01:00
child: const Icon(Icons.add),
2024-10-22 10:38:22 +02:00
backgroundColor: Colors.orange,
heroTag: null, // Damit beide Floating Buttons keine Konflikte haben
),
],
),
),
);
}
// Sortier-Menü anzeigen
void _showSortOptions(BuildContext context) {
showModalBottomSheet(
2024-10-30 23:41:56 +01:00
backgroundColor: Colors.orange,
2024-10-22 10:38:22 +02:00
context: context,
builder: (context) {
return Wrap(
children: [
ListTile(
2024-10-30 23:41:56 +01:00
leading: const Icon(Icons.text_fields),
title: const Text('Sort by Name'),
2024-10-22 10:38:22 +02:00
onTap: () {
Provider.of<ToDoProvider>(context, listen: false).sortByName();
Navigator.pop(context);
},
),
ListTile(
2024-10-30 23:41:56 +01:00
leading: const Icon(Icons.date_range),
title: const Text('Sort by Due Date'),
2024-10-22 10:38:22 +02:00
onTap: () {
Provider.of<ToDoProvider>(context, listen: false).sortByDueDate();
Navigator.pop(context);
},
),
ListTile(
2024-10-30 23:41:56 +01:00
leading: const Icon(Icons.flag),
title: const Text('Sort by Status'),
2024-10-22 10:38:22 +02:00
onTap: () {
Provider.of<ToDoProvider>(context, listen: false).sortByStatus();
Navigator.pop(context);
},
),
],
);
},
);
}
// Hinzufügen eines neuen ToDos (Dialog)
void _showAddToDoDialog(BuildContext context) {
String name = '';
String description = '';
DateTime? dueDate;
showDialog(
context: context,
builder: (context) {
return AlertDialog(
2024-10-30 23:41:56 +01:00
backgroundColor: Colors.grey[700],
insetPadding: EdgeInsets.zero,
title: const Text('Add To-Do'),
2024-10-22 10:38:22 +02:00
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
2024-10-30 23:41:56 +01:00
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: TextField(
decoration: const InputDecoration(labelText: 'Name', filled: true),
onChanged: (value) {
name = value;
},
),
2024-10-22 10:38:22 +02:00
),
TextField(
2024-10-30 23:41:56 +01:00
decoration: const InputDecoration(labelText: 'Description', filled: true),
2024-10-22 10:38:22 +02:00
onChanged: (value) {
description = value;
},
),
2024-10-30 23:41:56 +01:00
const SizedBox(height: 10),
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 16.0),
child: ElevatedButton(
style: const ButtonStyle(
backgroundColor: WidgetStatePropertyAll<Color>(Colors.orange),
),
onPressed: () async {
DateTime? picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2101),
);
if (picked != null) dueDate = picked;
},
child: const Text('Pick Due Date'),
),
),
),
],
2024-10-22 10:38:22 +02:00
),
],
),
actions: [
2024-10-30 23:41:56 +01:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: TextButton(
style: const ButtonStyle(
backgroundColor: WidgetStatePropertyAll<Color>(Colors.red),
),
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
),
const SizedBox(
width: 16,
),
Expanded(
child: ElevatedButton(
style: const ButtonStyle(
backgroundColor: WidgetStatePropertyAll<Color>(Colors.green),
),
onPressed: () {
if (name.isNotEmpty && description.isNotEmpty && dueDate != null) {
final newToDo = ToDoItem(
name: name,
description: description,
dueDate: dueDate!,
);
Provider.of<ToDoProvider>(context, listen: false).addToDo(newToDo);
Navigator.of(context).pop();
}
},
child: const Text('Add'),
),
),
],
)
2024-10-22 10:38:22 +02:00
],
);
},
);
}
}