CPD_2024_todo/lib/screens/todo_list_screen.dart

104 lines
3.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 '../providers/todo_provider.dart';
import '../widgets/todo_form.dart';
class ToDoListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
var todoProvider = Provider.of<ToDoProvider>(context);
// Helper function to format DateTime
String formatDate(DateTime date) {
return '${date.day.toString().padLeft(2, '0')}/${date.month.toString().padLeft(2, '0')}/${date.year}';
}
return Scaffold(
appBar: AppBar(
title: Text('To-Do list'),
actions: [
// Filter Dropdown Menü
PopupMenuButton<String>(
onSelected: (value) {
switch (value) {
case 'Deadline':
todoProvider.sortByDeadline();
break;
case 'Priority':
todoProvider.sortByPriority();
break;
case 'Status':
todoProvider.sortByStatus();
break;
case 'None':
break;
}
},
itemBuilder: (BuildContext context) {
return {'Deadline', 'Priority', 'Status', 'None'}.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: todoProvider.todos.length,
itemBuilder: (context, index) {
var todo = todoProvider.todos[index];
String formattedDate = formatDate(todo.deadline); // date formatting
2024-10-22 20:42:08 +02:00
return ListTile(
title: Text(
todo.title,
style: TextStyle(
decoration: todo.isCompleted ? TextDecoration.lineThrough : TextDecoration.none,
),
),
subtitle: Text('Due: $formattedDate | ${todo.priority} priority'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Checkbox(
value: todo.isCompleted,
onChanged: (value) {
todoProvider.toggleCompletion(index);
},
),
IconButton(
icon: Icon(Icons.close),
onPressed: () => todoProvider.removeTask(index),
),
],
),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (BuildContext context) {
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
child: ToDoForm(),
);
},
);
},
child: Icon(Icons.add),
),
);
}
}