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';
|
|
|
|
import 'package:cpd/database/todo_db.dart';
|
2024-05-09 09:46:23 +02:00
|
|
|
|
|
|
|
class MyListView extends StatelessWidget {
|
2024-05-12 00:08:54 +02:00
|
|
|
final List<Habit> habits;
|
2024-05-12 22:55:54 +02:00
|
|
|
final TodoDB todoDB;
|
|
|
|
final Function fetchTodos;
|
2024-05-14 23:51:15 +02:00
|
|
|
final Function(int) updateCounter;
|
2024-05-12 22:55:54 +02:00
|
|
|
|
2024-05-13 23:31:03 +02:00
|
|
|
MyListView({super.key,
|
|
|
|
required this.habits,
|
|
|
|
required this.todoDB,
|
|
|
|
required this.fetchTodos,
|
|
|
|
required this.updateCounter});
|
2024-05-09 23:13:43 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-16 21:27:52 +02:00
|
|
|
int completedCount = 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,
|
|
|
|
itemCount: habits.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
2024-05-12 00:08:54 +02:00
|
|
|
final todo = habits[index];
|
2024-05-12 22:55:54 +02:00
|
|
|
String newTitle = todo.title;
|
|
|
|
String? newSubtitle = todo.subtitle;
|
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-05-09 17:37:41 +02:00
|
|
|
key: Key(habits[index].title),
|
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
|
|
|
// Löschen
|
|
|
|
if (direction == DismissDirection.endToStart) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2024-05-13 23:31:03 +02:00
|
|
|
SnackBar(
|
|
|
|
content: Text("${habits[index].title} was dismissed"))
|
2024-05-09 23:13:43 +02:00
|
|
|
);
|
2024-05-12 22:55:54 +02:00
|
|
|
await todoDB.delete(todo.id);
|
|
|
|
fetchTodos();
|
2024-05-09 23:13:43 +02:00
|
|
|
}
|
|
|
|
// Bearbeiten
|
2024-05-12 22:55:54 +02:00
|
|
|
else if (direction == DismissDirection.startToEnd) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
2024-05-13 23:31:03 +02:00
|
|
|
builder: (context) =>
|
|
|
|
AlertDialog(
|
|
|
|
title: const Text('Edit Habit'),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
TextFormField(
|
|
|
|
initialValue: todo.title,
|
|
|
|
onChanged: (value) {
|
|
|
|
newTitle = value;
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.title),
|
|
|
|
labelText: "Title",
|
|
|
|
hintText: "Enter task title",
|
|
|
|
),
|
|
|
|
onFieldSubmitted: (_) =>
|
|
|
|
_submitForm(
|
|
|
|
context, todo, newTitle, newSubtitle),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
initialValue: todo.subtitle ?? '',
|
|
|
|
onChanged: (value) {
|
|
|
|
newSubtitle = value;
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.title),
|
|
|
|
labelText: "Subtitle",
|
|
|
|
hintText: "Enter task subtitle",
|
|
|
|
),
|
|
|
|
onFieldSubmitted: (_) =>
|
|
|
|
_submitForm(
|
|
|
|
context, todo, newTitle, newSubtitle),
|
|
|
|
)
|
|
|
|
],
|
2024-05-12 22:55:54 +02:00
|
|
|
),
|
2024-05-13 23:31:03 +02:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
fetchTodos();
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: const Text('Cancel'),
|
2024-05-12 22:55:54 +02:00
|
|
|
),
|
|
|
|
|
2024-05-13 23:31:03 +02:00
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
await todoDB.update(id: todo.id,
|
|
|
|
title: newTitle,
|
|
|
|
subtitle: newSubtitle);
|
|
|
|
fetchTodos();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
],
|
2024-05-12 22:55:54 +02:00
|
|
|
),
|
|
|
|
);
|
2024-05-09 23:13:43 +02:00
|
|
|
}
|
2024-05-09 09:46:23 +02:00
|
|
|
},
|
|
|
|
child: ListTile(
|
2024-05-12 00:08:54 +02:00
|
|
|
title: Text(todo.title),
|
|
|
|
subtitle: Text(todo.subtitle ?? ''),
|
2024-05-09 09:46:23 +02:00
|
|
|
leading: Checkbox(
|
2024-05-12 00:08:54 +02:00
|
|
|
value: todo.isComplete,
|
2024-05-09 09:46:23 +02:00
|
|
|
onChanged: (value) {
|
2024-05-16 21:27:52 +02:00
|
|
|
if (value != null) {
|
|
|
|
todoDB.updateCompletionStatus(todo.id, value);
|
|
|
|
fetchTodos();
|
|
|
|
completedCount += value ? 1 : -1;
|
|
|
|
updateCounter(completedCount);
|
|
|
|
}
|
2024-05-09 09:46:23 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-05-13 23:31:03 +02:00
|
|
|
|
|
|
|
void _submitForm(BuildContext context, Habit todo, String newTitle,
|
|
|
|
String? newSubtitle) async {
|
2024-05-13 20:08:39 +02:00
|
|
|
await todoDB.update(id: todo.id, title: newTitle, subtitle: newSubtitle);
|
|
|
|
fetchTodos();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
2024-05-09 09:46:23 +02:00
|
|
|
}
|