Flutter-Ernaehrungsapp/lib/components/card_component.dart

91 lines
2.4 KiB
Dart

import 'dart:convert';
import 'package:ernaehrung/models/food.dart';
import 'package:flutter/services.dart' as root_bundle;
import 'package:flutter/material.dart';
import '../models/card.dart';
class CardComponent extends StatefulWidget {
final Image image;
final String cardName;
final int? caloriesSummary;
final List<CardModel> listOfCardItems;
const CardComponent(this.image, this.cardName, this.caloriesSummary, this.listOfCardItems, {super.key});
@override
State<CardComponent> createState() => _CardComponentState();
}
class _CardComponentState extends State<CardComponent> {
Future<List<Food>> readJson() async {
final jsonData =
await root_bundle.rootBundle.loadString('assets/json/csvjson_full.json');
final list = json.decode(jsonData) as List<dynamic>;
return list.map((e) => Food.fromJson(e)).toList();
}
String getSummaryOfAllNames(List<CardModel> cardModels){
String names = "";
for (var element in cardModels) {
names += element.getName;
}
return names;
}
Widget _getCalorieContainer(){
if (widget.caloriesSummary != null){
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 8,
horizontal: 0
),
child: Center(
child: Column(
children: [
const Divider(
thickness: 1,
),
Text(widget.caloriesSummary.toString())
],
),
),
);
}else {
return const Placeholder();
}
}
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.image,
Column(
children: [
Text(widget.cardName),
Text(getSummaryOfAllNames(widget.listOfCardItems))
],
),
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(8),
),
child: const Text('+', style: TextStyle(fontSize: 28),),
)
],
),
_getCalorieContainer()
],
)
);
}
}