cpd_2024_todo/lib/presentation/ToDoListScreen.dart

158 lines
4.8 KiB
Dart

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(
appBar: AppBar(
title: Text('To-Do List'),
),
body: Consumer<ToDoProvider>(
builder: (context, toDoProvider, child) {
return ListView(
children: toDoProvider.toDoList.map((todo) {
return ToDoExpandableWidget(
toDoItem: todo,
id: todo.id!,
);
},
).toList()
);
}
),
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,
child: Icon(Icons.sort), // Damit beide Floating Buttons keine Konflikte haben
),
// Hinzufügen-Button unten rechts
FloatingActionButton(
onPressed: () => _showAddToDoDialog(context),
child: Icon(Icons.add),
backgroundColor: Colors.orange,
heroTag: null, // Damit beide Floating Buttons keine Konflikte haben
),
],
),
),
);
}
// Sortier-Menü anzeigen
void _showSortOptions(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) {
return Wrap(
children: [
ListTile(
leading: Icon(Icons.text_fields),
title: Text('Sort by Name'),
onTap: () {
Provider.of<ToDoProvider>(context, listen: false).sortByName();
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.date_range),
title: Text('Sort by Due Date'),
onTap: () {
Provider.of<ToDoProvider>(context, listen: false).sortByDueDate();
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.flag),
title: Text('Sort by Status'),
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(
title: Text('Add To-Do'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
decoration: InputDecoration(labelText: 'Name'),
onChanged: (value) {
name = value;
},
),
TextField(
decoration: InputDecoration(labelText: 'Description'),
onChanged: (value) {
description = value;
},
),
SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
DateTime? picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2101),
);
if (picked != null) dueDate = picked;
},
child: Text('Pick Due Date'),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Cancel'),
),
ElevatedButton(
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: Text('Add'),
),
],
);
},
);
}
}