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