aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/recipe_card_widget.dart
blob: fab88aade162a943aba3586ef24725d9cbb417e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import 'dart:io';

import 'package:flutter/material.dart';

import 'package:kulinar_app/constants.dart';
import 'package:kulinar_app/models/recipe_class.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/widgets/page_route_transitions.dart';
import 'package:kulinar_app/views/recipe_subview.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: RecipeSubview(
          remote: widget.remote,
          recipe: widget.recipe,
          redrawCallback: widget.redrawCallback,
          showToastCallback: widget.showToastCallback,
        ),
      ),
    );

    try {
      if (!mounted || widget.redrawCallback == null) return;

      widget.redrawCallback!();
    } catch (e) {
      debugPrint("$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 {
  const _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(),
        _buildFavoriteIcon(),
      ],
    );
  }

  List<Widget> _buildRating(int rating) {
    List<Widget> result = [];

    if (rating == 0) return result;

    for (int i = 0; i < 5; i++) {
      result.add(rating >= 1 ? const Icon(Icons.star_rounded) : const 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: cRecipeTitleStyle,
              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: _buildRating(widget.recipe.rating),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFavoriteIcon() {
    return IconButton(
      icon: widget.recipe.favorite ? const Icon(Icons.favorite_rounded, color: Colors.red) : const Icon(Icons.favorite_border_rounded),
      onPressed: () {
        widget.recipe.toggleFavorite();
        RecipeData.save();

        setState(() {});
      },
    );
  }
}