Flutter-Ernaehrungsapp/lib/android/components/card_component.dart

153 lines
4.7 KiB
Dart
Raw Normal View History

import 'package:basic_utils/basic_utils.dart';
2023-05-29 12:08:46 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
2023-05-30 17:12:42 +02:00
import '../config/format_helper.dart';
2023-05-29 12:08:46 +02:00
import '../models/food.dart';
import '../pages/nav_pages/search_food.dart';
class CardComponent extends StatefulWidget {
final String eatingMealName;
final List<Food> selectedMeal;
final bool addButtonVisible;
2023-05-29 12:08:46 +02:00
const CardComponent(
2023-06-02 01:42:56 +02:00
{Key? key,
required this.eatingMealName,
required this.selectedMeal,
this.addButtonVisible = true})
2023-05-29 12:08:46 +02:00
: super(key: key);
@override
State<CardComponent> createState() => _CardComponentState();
}
class _CardComponentState extends State<CardComponent> {
Route createRoute(String cardName) {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
SearchFoodPage(cardName),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
const curve = Curves.ease;
var tween =
Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
);
}
getImageOfMeal() {
2023-06-02 01:42:56 +02:00
if (widget.eatingMealName.toLowerCase() ==
dotenv.env['BREAKFAST_FIELD']!.toLowerCase()) {
2023-05-29 12:08:46 +02:00
return const Image(image: AssetImage('assets/images/tea.png'));
2023-06-02 01:42:56 +02:00
} else if (widget.eatingMealName.toLowerCase() ==
dotenv.env['LUNCH_FIELD']!.toLowerCase()) {
2023-05-29 12:08:46 +02:00
return const Image(image: AssetImage('assets/images/fries.png'));
2023-06-02 01:42:56 +02:00
} else if (widget.eatingMealName.toLowerCase() ==
dotenv.env['DINNER_FIELD']!.toLowerCase()) {
2023-05-29 12:08:46 +02:00
return const Image(image: AssetImage('assets/images/ice.png'));
}
}
String capitalize(String s) => s[0].toUpperCase() + s.substring(1);
String listFoodAsString() {
String mealAsString = "";
for (final food in widget.selectedMeal) {
mealAsString = "${food.name} ";
}
return mealAsString;
}
Widget getElevatedButton() {
2023-06-02 01:42:56 +02:00
if (widget.addButtonVisible) {
return ElevatedButton(
onPressed: () async {
Navigator.of(context).push(createRoute(widget.eatingMealName));
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(0),
),
child: const Text(
'+',
style: TextStyle(fontSize: 28),
),
);
2023-06-02 01:42:56 +02:00
} else {
return const SizedBox.shrink();
}
2023-05-29 12:08:46 +02:00
}
int getCountedCalories() {
double calories = 0;
for (Food food in widget.selectedMeal) {
2023-06-02 01:42:56 +02:00
if (food.calories is int) {
2023-05-29 12:08:46 +02:00
calories += food.calories as int;
2023-06-02 01:42:56 +02:00
} else if (food.calories is double) {
2023-05-29 12:08:46 +02:00
calories += food.calories as double;
}
}
return calories.round();
}
@override
Widget build(BuildContext context) {
2023-06-02 01:42:56 +02:00
return Container(
margin: const EdgeInsets.fromLTRB(0, 16, 0, 0),
2023-05-29 12:08:46 +02:00
child: Column(
children: [
Row(
children: [
2023-06-02 01:42:56 +02:00
getImageOfMeal(),
Text(
StringUtils.capitalize(widget.eatingMealName),
maxLines: 1,
overflow: TextOverflow.ellipsis,
softWrap: false,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 14),
2023-05-29 12:08:46 +02:00
),
getElevatedButton(),
],
),
SizedBox(
2023-06-02 01:42:56 +02:00
width: MediaQuery.of(context).size.width * 1,
child: ListView.builder(
2023-05-29 12:08:46 +02:00
primary: false,
shrinkWrap: true,
2023-06-02 01:42:56 +02:00
itemCount: getMapOfDistinctElementsWithCounterAndCalories(
widget.selectedMeal)
.keys
.length,
2023-05-29 12:08:46 +02:00
itemBuilder: (context, i) {
2023-06-02 01:42:56 +02:00
Map<String, List<int>> map =
getMapOfDistinctElementsWithCounterAndCalories(
widget.selectedMeal);
String foodName = map.keys.elementAt(i);
List<int> values = map.values.elementAt(i);
2023-06-02 01:42:56 +02:00
return Text(getFoodListStringByFood(
foodName, values[0], values[1]));
}),
2023-05-29 12:08:46 +02:00
),
widget.selectedMeal.isNotEmpty
2023-06-02 01:42:56 +02:00
? Column(
children: [
const Divider(),
Text("${getCountedCalories()} kcal")
],
)
2023-05-29 12:08:46 +02:00
: const SizedBox.shrink(),
],
));
}
}