cpd/lib/widgets/listview.dart

100 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:cpd/database/habit.dart';
import 'package:cpd/widgets/edithabitDialog.dart';
import '../database/todo_interface.dart';
class MyListView extends StatelessWidget {
final List<Habit> habits;
final ToDoInterface todoDB;
final Function fetchTodos;
final Function(int) updateCounter;
MyListView({super.key,
required this.habits,
required this.todoDB,
required this.fetchTodos,
required this.updateCounter});
@override
Widget build(BuildContext context) {
int completedCount = habits.where((habit) => habit.isComplete).length;
return ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.zero,
itemCount: habits.length,
itemBuilder: (BuildContext context, int index) {
final todo = 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: Key(habits[index].title),
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 {
// Löschen
if (direction == DismissDirection.endToStart) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("${habits[index].title} was dismissed"))
);
await todoDB.delete(todo.id);
fetchTodos();
}
// Bearbeiten
else if (direction == DismissDirection.startToEnd) {
showDialog(
context: context,
builder: (context) => EditHabitDialog(
todo: todo,
todoDB: todoDB,
fetchTodos: fetchTodos,
)
);
}
},
child: ListTile(
title: Text(newTitle),
subtitle: Text(newSubtitle),
trailing: Icon(newIcon),
leading: Checkbox(
value: todo.isComplete,
onChanged: (value) {
if (value != null) {
todoDB.updateCompletionStatus(todo.id, value);
fetchTodos();
completedCount += value ? 1 : -1;
updateCounter(completedCount);
}
},
),
),
),
),
);
},
);
}
}