diff options
Diffstat (limited to 'lib/widgets/recipe_card_widget.dart')
-rw-r--r-- | lib/widgets/recipe_card_widget.dart | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/lib/widgets/recipe_card_widget.dart b/lib/widgets/recipe_card_widget.dart new file mode 100644 index 0000000..d96b004 --- /dev/null +++ b/lib/widgets/recipe_card_widget.dart @@ -0,0 +1,178 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; + +import 'package:kulinar_app/constants.dart'; +import 'package:kulinar_app/models/data/recipe_data_class.dart'; +import 'package:kulinar_app/models/data/settings_data_class.dart'; +import 'package:kulinar_app/views/recipe_view.dart'; +import 'package:kulinar_app/models/recipe_class.dart'; +import 'package:kulinar_app/widgets/page_route_transitions.dart'; + +class RecipeCard extends StatefulWidget { + const RecipeCard({Key? key, this.remote = false, this.inSearch = false, required this.recipe, this.redrawCallback, this.showToastCallback}) : super(key: key); + + final bool remote; + final bool inSearch; + final Recipe recipe; + final Function? redrawCallback; + final Function? showToastCallback; + + @override + _RecipeCardState createState() => _RecipeCardState(); +} + +class _RecipeCardState extends State<RecipeCard> { + @override + void dispose() { + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return GestureDetector( + onTap: _recipeCardPressed, + child: Padding( + padding: const EdgeInsets.only(top: 5.0, right: 5.0, left: 5.0), + child: Card( + elevation: 30.0, + child: Padding( + padding: const EdgeInsets.all(10.0), + child: Column( + children: [ + _RecipeInfo(recipe: widget.recipe), + _buildImage(), + ], + ), + ), + ), + ), + ); + } + + Future<void> _recipeCardPressed() async { + await Navigator.push( + context, + SlideFromBottomRoute( + child: RecipeView( + remote: widget.remote, + readonly: true, + recipe: widget.recipe, + redrawCallback: widget.redrawCallback, + showToastCallback: widget.showToastCallback, + ), + ), + ); + + try { + if (!mounted || widget.redrawCallback == null) return; + + widget.redrawCallback!(); + } catch (e) { + print(e); + } + } + + Widget _buildImage() { + if (widget.recipe.image == null) return Container(); + if (SettingsData.settings["showPhotos"] == "3") return Container(); + if (SettingsData.settings["showPhotos"] == "1" && !widget.inSearch) return Container(); + if (SettingsData.settings["showPhotos"] == "2" && widget.inSearch) return Container(); + + return Padding( + padding: const EdgeInsets.only(top: 10.0), + child: Container( + height: 200.0, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(5.0), + image: DecorationImage( + image: FileImage( + File(widget.recipe.image!), + ), + fit: BoxFit.cover, + ), + ), + ), + ); + } +} + +class _RecipeInfo extends StatefulWidget { + _RecipeInfo({Key? key, required this.recipe}) : super(key: key); + + final Recipe recipe; + + @override + __RecipeInfoState createState() => __RecipeInfoState(); +} + +class __RecipeInfoState extends State<_RecipeInfo> { + @override + Widget build(BuildContext context) { + return Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + _buildRecipeInfo(), + _buildFavortiteIcon(), + ], + ); + } + + List<Widget> _buildRating(int rating) { + List<Widget> _result = []; + + if (rating == 0) return _result; + + for (int i = 0; i < 5; i++) { + _result.add(rating >= 1 ? Icon(Icons.star_rounded) : Icon(Icons.star_border_rounded)); + rating--; + } + + return _result; + } + + Widget _buildRecipeInfo() { + return Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.all(2.0), + child: Text( + widget.recipe.title!, + style: cRecipeTextStyle, + overflow: TextOverflow.ellipsis, + ), + ), + Padding( + padding: const EdgeInsets.only(bottom: 2.0, left: 2.0), + child: Text( + widget.recipe.description!, + style: cRecipeDescriptionStyle, + overflow: TextOverflow.ellipsis, + maxLines: 1, + ), + ), + Padding( + padding: const EdgeInsets.only(bottom: 2.0), + child: Row( + children: widget.recipe.rating != null ? _buildRating(widget.recipe.rating) : [], + ), + ), + ], + ), + ); + } + + Widget _buildFavortiteIcon() { + return IconButton( + icon: widget.recipe.favorite ? Icon(Icons.favorite_rounded, color: Colors.red) : Icon(Icons.favorite_border_rounded), + onPressed: () { + widget.recipe.toggleFavorite(); + RecipeData.save(); + + setState(() {}); + }, + ); + } +} |