import 'package:assorted_layout_widgets/assorted_layout_widgets.dart'; import 'package:ernaehrung/android/config/cast_helper.dart'; import 'package:ernaehrung/android/config/statistics.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:hive/hive.dart'; import '../models/food.dart'; class SearchedFoodComponent extends StatefulWidget { final Food food; final String cardName; const SearchedFoodComponent(this.food, this.cardName, {Key? key}) : super(key: key); @override State createState() => _SearchFoodComponentState(); } class _SearchFoodComponentState extends State { void storeFood() async { StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); if (!mealplanBox.isOpen){ Hive.openBox(dotenv.env['MEALPLAN_BOX']!); } addValuesToList(mealplanBox, widget.cardName, [widget.food]); } void addValuesToList(box, String key, List newValues){ List existingList = castDynamicToListFood(box.get(key)); for(int i = 0; i < newValues.length;i++){ if(!existingList.contains(newValues[i])){ existingList.add(newValues[i]); } } box.put(key, existingList); } @override Widget build(BuildContext context) { return Container( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), decoration: const BoxDecoration(color: Color.fromARGB(234, 234, 123, 5)), child: WrapSuper( spacing: 32, wrapType: WrapType.balanced, wrapFit: WrapFit.larger, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( width: MediaQuery.of(context).size.width * 0.6, child: Text( widget.food.name, maxLines: 1, overflow: TextOverflow.ellipsis, softWrap: false, style: const TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0), ), ), SizedBox( width: MediaQuery.of(context).size.width * 0.6, child: Text( "${widget.food.fatg} 100g " "${widget.food.calories} 100g " "${widget.food.carbohydrateg.toString()} 100g", maxLines: 1, overflow: TextOverflow.ellipsis, softWrap: false, style: const TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0), ), ), ], ), ElevatedButton( onPressed: () async { storeFood(); }, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: Colors.green, shadowColor: Colors.greenAccent, elevation: 3, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(32.0)), maximumSize: Size( MediaQuery.of(context).size.width * 0.2, 36), //////// HERE ), child: const Text('+'), ) ], )); } }