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';
|
|
|
|
|
2024-10-29 17:37:12 +01:00
|
|
|
class ToDoListScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_ToDoListScreenState createState() => _ToDoListScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ToDoListScreenState extends State<ToDoListScreen> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
Provider.of<ToDoProvider>(context, listen: false).loadTasks();
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:42:08 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
var todoProvider = Provider.of<ToDoProvider>(context);
|
|
|
|
|
|
|
|
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: [
|
|
|
|
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];
|
2024-10-22 20:46:33 +02:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|