2024-05-09 09:46:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-12 00:08:54 +02:00
|
|
|
import 'package:cpd/database/habit.dart';
|
2024-06-08 14:22:18 +02:00
|
|
|
import 'package:cpd/widgets/edithabit_dialog.dart';
|
2024-05-28 21:59:44 +02:00
|
|
|
import '../database/todo_interface.dart';
|
|
|
|
|
2024-06-16 17:12:28 +02:00
|
|
|
|
2024-06-08 14:22:18 +02:00
|
|
|
class MyListView extends StatefulWidget {
|
2024-05-12 00:08:54 +02:00
|
|
|
final List<Habit> habits;
|
2024-05-28 21:59:44 +02:00
|
|
|
final ToDoInterface todoDB;
|
2024-05-12 22:55:54 +02:00
|
|
|
final Function fetchTodos;
|
2024-06-16 17:12:28 +02:00
|
|
|
//Eine Funktion zum Aktualisieren des Zählers für abgeschlossene Gewohnheiten
|
2024-05-14 23:51:15 +02:00
|
|
|
final Function(int) updateCounter;
|
2024-05-12 22:55:54 +02:00
|
|
|
|
2024-06-08 14:22:18 +02:00
|
|
|
const MyListView({
|
|
|
|
super.key,
|
2024-05-13 23:31:03 +02:00
|
|
|
required this.habits,
|
|
|
|
required this.todoDB,
|
|
|
|
required this.fetchTodos,
|
2024-06-08 14:22:18 +02:00
|
|
|
required this.updateCounter,
|
|
|
|
});
|
2024-05-09 23:13:43 +02:00
|
|
|
|
2024-06-08 14:22:18 +02:00
|
|
|
@override
|
|
|
|
MyListViewState createState() => MyListViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyListViewState extends State<MyListView> {
|
2024-05-09 09:46:23 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-08 14:22:18 +02:00
|
|
|
int completedCount = widget.habits.where((habit) => habit.isComplete).length;
|
2024-05-12 22:55:54 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
return ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
padding: EdgeInsets.zero,
|
2024-06-08 14:22:18 +02:00
|
|
|
itemCount: widget.habits.length,
|
2024-05-09 09:46:23 +02:00
|
|
|
itemBuilder: (BuildContext context, int index) {
|
2024-06-08 14:22:18 +02:00
|
|
|
final todo = widget.habits[index];
|
2024-05-12 22:55:54 +02:00
|
|
|
String newTitle = todo.title;
|
|
|
|
String? newSubtitle = todo.subtitle;
|
2024-05-19 22:08:04 +02:00
|
|
|
IconData newIcon = todo.icon;
|
2024-05-12 00:08:54 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
|
|
|
|
child: Card(
|
|
|
|
color: index % 2 == 0 ? Colors.green[300] : Colors.green[100],
|
|
|
|
child: Dismissible(
|
2024-06-09 12:25:38 +02:00
|
|
|
key: UniqueKey(),
|
2024-05-09 09:46:23 +02:00
|
|
|
background: Container(
|
2024-05-09 23:13:43 +02:00
|
|
|
color: Colors.blue,
|
2024-05-09 09:46:23 +02:00
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: const Padding(
|
|
|
|
padding: EdgeInsets.only(left: 16),
|
2024-05-09 23:13:43 +02:00
|
|
|
child: Icon(Icons.edit),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
secondaryBackground: Container(
|
|
|
|
color: Colors.red,
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: const Padding(
|
|
|
|
padding: EdgeInsets.only(right: 16),
|
|
|
|
child: Icon(Icons.delete),
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
onDismissed: (direction) async {
|
2024-05-09 23:13:43 +02:00
|
|
|
if (direction == DismissDirection.endToStart) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2024-05-13 23:31:03 +02:00
|
|
|
SnackBar(
|
2024-06-08 14:22:18 +02:00
|
|
|
content: Text("${widget.habits[index].title} was dismissed"))
|
2024-05-09 23:13:43 +02:00
|
|
|
);
|
2024-06-08 14:22:18 +02:00
|
|
|
await widget.todoDB.delete(todo.id);
|
2024-06-09 12:25:38 +02:00
|
|
|
setState(() {
|
|
|
|
widget.habits.removeAt(index);
|
|
|
|
});
|
2024-06-08 14:22:18 +02:00
|
|
|
widget.fetchTodos();
|
2024-05-09 23:13:43 +02:00
|
|
|
}
|
2024-05-12 22:55:54 +02:00
|
|
|
else if (direction == DismissDirection.startToEnd) {
|
|
|
|
showDialog(
|
2024-06-08 14:22:18 +02:00
|
|
|
context: context,
|
|
|
|
builder: (context) => EditHabitDialog(
|
|
|
|
todo: todo,
|
|
|
|
todoDB: widget.todoDB,
|
|
|
|
fetchTodos: widget.fetchTodos,
|
|
|
|
)
|
2024-05-12 22:55:54 +02:00
|
|
|
);
|
2024-06-09 12:25:38 +02:00
|
|
|
setState(() {
|
|
|
|
widget.habits.removeAt(index);
|
|
|
|
});
|
2024-05-09 23:13:43 +02:00
|
|
|
}
|
2024-05-09 09:46:23 +02:00
|
|
|
},
|
|
|
|
child: ListTile(
|
2024-05-19 22:08:04 +02:00
|
|
|
title: Text(newTitle),
|
2024-05-28 21:59:44 +02:00
|
|
|
subtitle: Text(newSubtitle),
|
2024-05-19 22:08:04 +02:00
|
|
|
trailing: Icon(newIcon),
|
2024-05-09 09:46:23 +02:00
|
|
|
leading: Checkbox(
|
2024-05-12 00:08:54 +02:00
|
|
|
value: todo.isComplete,
|
2024-06-09 12:25:38 +02:00
|
|
|
onChanged: (value) async {
|
2024-05-16 21:27:52 +02:00
|
|
|
if (value != null) {
|
2024-06-08 14:22:18 +02:00
|
|
|
widget.todoDB.updateCompletionStatus(todo.id, value);
|
|
|
|
setState(() {
|
2024-06-09 12:25:38 +02:00
|
|
|
todo.isComplete = value;
|
2024-06-08 14:22:18 +02:00
|
|
|
completedCount += value ? 1 : -1;
|
|
|
|
});
|
|
|
|
widget.updateCounter(completedCount);
|
2024-06-09 12:25:38 +02:00
|
|
|
widget.fetchTodos();
|
2024-05-16 21:27:52 +02:00
|
|
|
}
|
2024-06-08 14:22:18 +02:00
|
|
|
},
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-06-08 14:22:18 +02:00
|
|
|
},
|
2024-05-09 09:46:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|