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

93 lines
2.8 KiB
Dart
Raw Normal View History

import 'package:basic_utils/basic_utils.dart';
2023-05-29 22:04:44 +02:00
import 'package:ernaehrung/android/components/card/card_data_food_component.dart';
import 'package:ernaehrung/android/components/card/card_title_component.dart';
2023-06-01 12:14:23 +02:00
import 'package:ernaehrung/android/config/cast_helper.dart';
2023-05-29 22:04:44 +02:00
import 'package:flutter/material.dart';
import '../../models/food.dart';
2023-05-29 22:04:44 +02:00
import '../../pages/nav_pages/search_food.dart';
class CardComponent extends StatelessWidget {
final String title;
2023-06-02 19:21:06 +02:00
final Color color;
2023-05-29 22:04:44 +02:00
final List<dynamic> foods;
2023-06-02 19:21:06 +02:00
const CardComponent(this.title, this.foods, {super.key, Color? color})
: color = color ?? Colors.black;
2023-05-29 22:04:44 +02:00
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,
);
},
);
}
int countCalories(List<Food> foods){
int summary = 0;
for(int i=0; i<foods.length;i++){
summary += foods[i].calories is int ? foods[i].calories as int : (foods[i].calories as double).round();
}
return summary;
}
2023-05-29 22:04:44 +02:00
@override
Widget build(BuildContext context) {
return Container(
2023-06-02 19:21:06 +02:00
decoration: BoxDecoration(
color: color,
borderRadius: const BorderRadius.all(Radius.circular(12)),
2023-05-29 22:04:44 +02:00
),
2023-06-02 19:21:06 +02:00
margin: const EdgeInsets.fromLTRB(0, 0, 0, 16),
2023-05-29 22:04:44 +02:00
child: Column(
children: [
CardTitleComponent(
2023-06-02 19:21:06 +02:00
StringUtils.capitalize(title),
"${countCalories(castDynamicToListFood(foods))} Kalorien",
),
2023-05-29 22:04:44 +02:00
CardDataFoodComponent(
2023-06-02 19:21:06 +02:00
castDynamicToListFood(foods),
color,
2023-05-29 22:04:44 +02:00
),
Padding(
2023-06-02 19:21:06 +02:00
padding: const EdgeInsets.only(left: 8, right: 8,bottom: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(40),
backgroundColor: const Color(0xFFffffff),
foregroundColor: color,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
2023-05-29 22:04:44 +02:00
),
2023-06-02 19:21:06 +02:00
),
onPressed: () async {
Navigator.of(context).push(createRoute(title));
},
child: const Text(
'+ Gericht hinzufügen',
style:TextStyle(
fontSize: 17
) ,
2023-05-29 22:04:44 +02:00
),
),
2023-06-02 19:21:06 +02:00
),
2023-05-29 22:04:44 +02:00
],
2023-06-02 19:21:06 +02:00
),
);
2023-05-29 22:04:44 +02:00
}
}