110 lines
3.9 KiB
Dart
110 lines
3.9 KiB
Dart
|
// ignore_for_file: use_build_context_synchronously
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:firebase_database/firebase_database.dart';
|
||
|
import 'package:firebase_storage/firebase_storage.dart';
|
||
|
|
||
|
class RecipeDetailPage extends StatelessWidget {
|
||
|
final String recipeId;
|
||
|
|
||
|
const RecipeDetailPage({Key? key, required this.recipeId, required Map recipe}) : super(key: key);
|
||
|
|
||
|
Future<void> _showDeleteDialog(BuildContext context) async {
|
||
|
bool confirmDelete = await showDialog(
|
||
|
context: context,
|
||
|
builder: (BuildContext context) {
|
||
|
return AlertDialog(
|
||
|
title: const Text('Rezept löschen'),
|
||
|
content: const Text('Sind Sie sicher, dass Sie dieses Rezept löschen möchten?'),
|
||
|
actions: <Widget>[
|
||
|
TextButton(
|
||
|
onPressed: () => Navigator.of(context).pop(false),
|
||
|
child: const Text('Abbrechen'),
|
||
|
),
|
||
|
TextButton(
|
||
|
onPressed: () => Navigator.of(context).pop(true),
|
||
|
child: const Text('Löschen'),
|
||
|
),
|
||
|
],
|
||
|
);
|
||
|
},
|
||
|
) ?? false; // Dialog-Abbruch auch als "Nicht löschen" werten
|
||
|
|
||
|
if (confirmDelete) {
|
||
|
await _deleteRecipe(context);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> _deleteRecipe(BuildContext context) async {
|
||
|
DatabaseReference recipeRef = FirebaseDatabase.instance.ref('rezepte/$recipeId');
|
||
|
DataSnapshot snapshot = await recipeRef.get();
|
||
|
if (snapshot.exists) {
|
||
|
Map<dynamic, dynamic> recipe = snapshot.value as Map<dynamic, dynamic>;
|
||
|
|
||
|
// Lösche die Bilder aus dem Storage, wenn URLs vorhanden sind
|
||
|
for (final key in ['bild1', 'bild2', 'bild3']) {
|
||
|
final String? imageUrl = recipe[key];
|
||
|
if (imageUrl != null && imageUrl.isNotEmpty) {
|
||
|
try {
|
||
|
await FirebaseStorage.instance.refFromURL(imageUrl).delete();
|
||
|
} catch (e) {
|
||
|
debugPrint("Fehler beim Löschen von $imageUrl: $e");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Lösche den Rezepteintrag aus der Datenbank
|
||
|
await recipeRef.remove();
|
||
|
Navigator.pop(context);
|
||
|
} else {
|
||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||
|
const SnackBar(content: Text('Rezept konnte nicht gefunden werden.')),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text('Rezept Details'),
|
||
|
),
|
||
|
body: FutureBuilder<DataSnapshot>(
|
||
|
future: FirebaseDatabase.instance.ref('rezepte/$recipeId').get(),
|
||
|
builder: (BuildContext context, AsyncSnapshot<DataSnapshot> snapshot) {
|
||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||
|
return const Center(child: CircularProgressIndicator());
|
||
|
}
|
||
|
|
||
|
if (!snapshot.hasData || snapshot.data?.value == null) {
|
||
|
return const Center(child: Text('Rezept nicht gefunden'));
|
||
|
}
|
||
|
|
||
|
var recipeData = snapshot.data!.value as Map<dynamic, dynamic>;
|
||
|
|
||
|
return SingleChildScrollView(
|
||
|
child: Column(
|
||
|
children: [
|
||
|
if (recipeData['bild1'] != null)
|
||
|
Image.network(recipeData['bild1']),
|
||
|
Text(recipeData['titel'] ?? 'Kein Titel', style: Theme.of(context).textTheme.titleLarge),
|
||
|
Text(recipeData['beschreibung1'] ?? 'Keine Beschreibung'),
|
||
|
Text(recipeData['beschreibung2'] ?? 'Keine Beschreibung'),
|
||
|
Text(recipeData['beschreibung3'] ?? 'Keine Beschreibung'),
|
||
|
if (recipeData['bild2'] != null && recipeData['bild2'].isNotEmpty)
|
||
|
Image.network(recipeData['bild2']),
|
||
|
if (recipeData['bild3'] != null && recipeData['bild3'].isNotEmpty)
|
||
|
Image.network(recipeData['bild3']),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
|
onPressed: () => _showDeleteDialog(context),
|
||
|
tooltip: 'Löschen',
|
||
|
child: const Icon(Icons.delete),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|