85 lines
2.9 KiB
Dart
85 lines
2.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:cpd/database/habit.dart';
|
|
import 'package:cpd/database/todo_db.dart';
|
|
import 'package:cpd/database/db.dart';
|
|
|
|
class MyListView extends StatelessWidget {
|
|
final List<Habit> habits;
|
|
final todoDB = TodoDB();
|
|
MyListView({super.key, required this.habits});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListView.builder(
|
|
shrinkWrap: true,
|
|
padding: EdgeInsets.zero,
|
|
itemCount: habits.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
final todo = habits[index];
|
|
|
|
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"))
|
|
);
|
|
// Hier wird das Element aus der Liste entfernt
|
|
habits.removeAt(index);
|
|
//await todoDB.delete(todoDB.id);
|
|
|
|
// Ausgabe der aktualisierten Liste zur Kontrolle
|
|
print('Current list:');
|
|
habits.forEach((habit) {
|
|
print('${habit.title}: ${habit.subtitle}');
|
|
});
|
|
}
|
|
// Bearbeiten
|
|
else if(direction == DismissDirection.startToEnd) {
|
|
//muss noch implementiert werden
|
|
}
|
|
},
|
|
child: ListTile(
|
|
title: Text(todo.title),
|
|
subtitle: Text(todo.subtitle ?? ''),
|
|
trailing: Text(todo.icon as String),
|
|
leading: Checkbox(
|
|
value: todo.isComplete,
|
|
onChanged: (value) {
|
|
if (value == false) {
|
|
print("decrease counter");
|
|
} else {
|
|
print("increase counter");
|
|
}
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} |