cpd/lib/widgets/listview.dart

152 lines
5.7 KiB
Dart
Raw Normal View History

2024-05-09 09:46:23 +02:00
import 'package:flutter/material.dart';
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 {
final List<Habit> habits;
final TodoDB todoDB;
final Function fetchTodos;
final Function(int) updateCounter;
MyListView({super.key,
required this.habits,
required this.todoDB,
required this.fetchTodos,
required this.updateCounter});
2024-05-09 09:46:23 +02:00
@override
Widget build(BuildContext context) {
int counter = 0;
2024-05-09 09:46:23 +02:00
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;
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(
key: Key(habits[index].title),
2024-05-09 09:46:23 +02:00
background: Container(
color: Colors.blue,
2024-05-09 09:46:23 +02:00
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),
2024-05-09 09:46:23 +02:00
),
),
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) =>
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),
)
],
),
actions: [
TextButton(
onPressed: () {
fetchTodos();
Navigator.pop(context);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
await todoDB.update(id: todo.id,
title: newTitle,
subtitle: newSubtitle);
fetchTodos();
Navigator.of(context).pop();
},
child: const Text('Save'),
),
],
),
);
}
2024-05-09 09:46:23 +02:00
},
child: ListTile(
title: Text(todo.title),
subtitle: Text(todo.subtitle ?? ''),
2024-05-09 09:46:23 +02:00
leading: Checkbox(
value: todo.isComplete,
2024-05-09 09:46:23 +02:00
onChanged: (value) {
todoDB.updateCompletionStatus(todo.id, value ?? false);
fetchTodos();
updateCounter(value == false ? counter - 1 : counter + 1);
2024-05-09 09:46:23 +02:00
},
),
),
),
),
);
},
);
}
void _submitForm(BuildContext context, Habit todo, String newTitle,
String? newSubtitle) async {
await todoDB.update(id: todo.id, title: newTitle, subtitle: newSubtitle);
fetchTodos();
Navigator.of(context).pop();
}
2024-05-09 09:46:23 +02:00
}