-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood_detail.dart
276 lines (263 loc) · 8.82 KB
/
food_detail.dart
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:well_bin01/http_request.dart';
class FoodDetail extends StatefulWidget {
final Map<String, dynamic>? foodList;
const FoodDetail({Key? key, this.foodList}) : super(key: key);
@override
_FoodDetailState createState() => _FoodDetailState();
}
class _FoodDetailState extends State<FoodDetail> {
String formattedDate = DateFormat('MMM dd, yyyy').format(DateTime.now());
String selectedFood = '';
late Future<Map<String, dynamic>>? nutrientDataFuture;
// _FoodDetailState({this.foodList});
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
Map<String, dynamic>? foodList = widget.foodList;
List<dynamic> data = foodList!['data'];
List<dynamic> foods = data[0];
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
automaticallyImplyLeading: true,
backgroundColor: Colors.white,
surfaceTintColor: Colors.white,
title: Align(
alignment: Alignment.centerLeft,
child: Row(
children: [
const SizedBox(
width: 60,
),
Text(
formattedDate,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
],
),
),
),
body: Center(child: SingleChildScrollView(child: nutrientInfo(foods))),
);
}
Widget nutrientInfo(List<dynamic> foods) {
return Column(
children: [
// Text('Food List: $foods'),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: foods.map((food) {
return Padding(
padding: const EdgeInsets.only(right: 20),
child: buildFoodButton(food),
);
}).toList(),
),
),
),
const SizedBox(height: 15),
selectedFood.isNotEmpty
? buildNutrientInfo(selectedFood)
: Container(
height: 600,
width: 360,
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 30,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
color: Colors.black.withOpacity(0.075),
),
child: const Center(
child: Text(
"Click the button for nutritional information.\nNutrients are displayed based on 100 grams.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
),
),
),
const SizedBox(height: 50),
],
);
}
Widget buildFoodButton(String food) {
return TextButton(
onPressed: () {
setState(() {
selectedFood = food;
nutrientDataFuture = fetchNutrientData(selectedFood);
});
},
style: TextButton.styleFrom(
backgroundColor:
selectedFood == food ? const Color(0xff49c14e) : Colors.white,
side: BorderSide(
color:
selectedFood == food ? const Color(0xff49c14e) : Colors.black),
),
child: Text(food,
style: TextStyle(
color: selectedFood == food ? Colors.white : Colors.black,
)),
);
}
// 영양 정보를 가져옴.
Future<Map<String, dynamic>> fetchNutrientData(String foodName) async {
try {
Map<String, dynamic> result =
await httpGetNutrient(foodName: foodName) as Map<String, dynamic>;
return result['data'];
} catch (e) {
debugPrint('Error fetching nutrient data: $e');
rethrow;
}
}
// 선택된 음식에 따라 다른 widget를 렌더링
Widget buildNutrientInfo(String selectedFood) {
return FutureBuilder<Map<String, dynamic>>(
future: nutrientDataFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container(
width: 360,
height: 600,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
color: Colors.black.withOpacity(0.075),
),
child: const Center(
child: SizedBox(
width: 50,
height: 50,
child: CircularProgressIndicator(),
),
),
);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (!snapshot.hasData || snapshot.data == null) {
return const Text('No data available');
} else {
return buildNutrientInfoWidget(snapshot.data!);
}
},
);
}
Container buildNutrientInfoWidget(Map<String, dynamic> nutrientData) {
return Container(
width: 360,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
color: Colors.black.withOpacity(0.075),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 30,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, // Text 위젯은 왼쪽 정렬
children: [
Text(
'Calories: ${double.parse(nutrientData['calories'])} kcal',
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
),
),
const SizedBox(height: 20),
singleBar('Calcium', double.parse(nutrientData['calcium'] ?? '0'),
700, 'mg'),
const SizedBox(height: 20),
singleBar('Carbohydrate',
double.parse(nutrientData['carbohydrate'] ?? '0'), 324, 'g'),
const SizedBox(height: 20),
singleBar('Cholesterol',
double.parse(nutrientData['cholesterol'] ?? '0'), 300, 'mg'),
const SizedBox(height: 20),
singleBar('Fat', double.parse(nutrientData['fat'] ?? '0'), 54, 'g'),
const SizedBox(height: 20),
singleBar(
'Fiber', double.parse(nutrientData['fiber'] ?? '0'), 25, 'g'),
const SizedBox(height: 20),
singleBar(
'Iron', double.parse(nutrientData['iron'] ?? '0'), 12, 'mg'),
const SizedBox(height: 20),
singleBar('Potassium',
double.parse(nutrientData['potassium'] ?? '0'), 3500, 'mg'),
const SizedBox(height: 20),
singleBar('Protein', double.parse(nutrientData['protein'] ?? '0'),
55, 'g'),
const SizedBox(height: 20),
singleBar('Saturated Fat',
double.parse(nutrientData['saturated_fat'] ?? '0'), 15, 'g'),
const SizedBox(height: 20),
singleBar('Sodium', double.parse(nutrientData['sodium'] ?? '0'),
2000, 'mg'),
const SizedBox(height: 20),
singleBar(
'Sugar', double.parse(nutrientData['sugar'] ?? '0'), 100, 'g'),
const SizedBox(height: 20),
singleBar('Vitamin A',
double.parse(nutrientData['vitamin_a'] ?? '0'), 700, '㎍ RAE'),
const SizedBox(height: 20),
singleBar('Vitamin C',
double.parse(nutrientData['vitamin_c'] ?? '0'), 100, 'mg'),
],
),
),
);
}
Column singleBar(
String nutrient, double given, double suggested, String unit) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
nutrient, // calories, protein
style: const TextStyle(
fontSize: 15,
color: Color(0xff49c14e),
),
),
const Spacer(),
Text('$given / $suggested $unit'), // 1200 / 1686 kcal
],
),
const SizedBox(height: 4),
SizedBox(
width: 320,
child: LinearProgressIndicator(
value: given / suggested, // 1200 / 1686
backgroundColor: Colors.black.withOpacity(0.15),
minHeight: 14,
borderRadius: BorderRadius.circular(6),
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xff49c14e)),
),
),
],
);
}
// Future getNutrientData(String foodName) async {
// try {
// apiResult = await httpGetNutrient(foodName: foodName)
// }
// }
}