Aplikacja_mobilna/cookbook/lib/screens/recipe_search/recipe_screen.dart
2024-02-18 14:30:02 +01:00

96 lines
2.5 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:cookbook/data/models/recipe_model.dart';
class RecipeDetailsScreen extends StatefulWidget {
final Map<String, dynamic> recipe;
const RecipeDetailsScreen({Key? key, required this.recipe}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_RecipeDetailsScreenState createState() => _RecipeDetailsScreenState();
}
class _RecipeDetailsScreenState extends State<RecipeDetailsScreen> {
bool isFavorite = false;
final db = FirebaseFirestore.instance;
void _toggleFavorite() async {
setState(() {
isFavorite = !isFavorite;
});
String? docId;
if (isFavorite) {
Recipe recipe = Recipe(
label: widget.recipe['label'],
ingredientLines: List<String>.from(widget.recipe['ingredientLines']),
imageUrl: widget.recipe['image'],
);
DocumentReference docRef = await db.collection('recipes').add(recipe.toMap());
docId = docRef.id;
} else {
await db.collection('recipes').doc(docId).delete();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orange,
appBar: AppBar(
title: Text(widget.recipe['label'], style: const TextStyle(color: Colors.white)),
iconTheme: const IconThemeData(color: Colors.white),
),
body: SingleChildScrollView(
child: Column(
children: [
Image.network(widget.recipe['image']),
const Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Ingredients:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold,color: Colors.white),
),
),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: widget.recipe['ingredientLines'].length,
itemBuilder: (context, index) {
final ingredient = widget.recipe['ingredientLines'][index];
return ListTile(
title: Text(ingredient, style: const TextStyle(color: Colors.white),),
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_toggleFavorite();
},
child: Icon(
isFavorite ? Icons.favorite : Icons.favorite_border,
color: isFavorite ? Colors.red : Colors.white,
),
),
);
}
}