import 'package:assorted_layout_widgets/assorted_layout_widgets.dart'; import 'package:basic_utils/basic_utils.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:fluttertoast/fluttertoast.dart'; import 'package:hive/hive.dart'; import '../config/format_helper.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 { int counter = 1; void storeFood(int counter, BuildContext context) async { final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); if (!mealplanBox.isOpen){ Hive.openBox(dotenv.env['MEALPLAN_BOX']!); } for(int i = 0; i < counter; i++){ StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); addValuesToList(mealplanBox, widget.cardName, [widget.food]); } showSuccessToast(context); } void showSuccessToast(BuildContext context){ Fluttertoast.showToast( msg: "${getToastFoodNameString(widget.food,context)} erfolgreich zu ${StringUtils.capitalize(widget.cardName)} hinzugefĆ¼gt", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 5, backgroundColor: Colors.green, textColor: Colors.black, ); } 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), margin: const EdgeInsets.only(top: 8), decoration: BoxDecoration( color: Colors.lightGreen, borderRadius: BorderRadius.circular(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: 2, 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.calories} kcal | 100g", maxLines: 1, overflow: TextOverflow.ellipsis, softWrap: false, style: const TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0, ), ), ), ], ), Padding( padding: const EdgeInsets.only(top:12), child: Row( children: [ Padding( padding: const EdgeInsets.only(right :12), child: Container( width: 40, height: 40, decoration: BoxDecoration( color: Colors.green, borderRadius: BorderRadius.circular(5), border: Border.all(color: Colors.white), ), child: InkWell( onTap: () { setState(() { counter = counter > 1 ? counter - 1 : 1; }); }, child: const Center( child: Text( "-", style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), ), ), ), ), ), SizedBox( width: 20, child: Text( counter.toString(), style: const TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), textAlign: TextAlign.center, ), ), Padding( padding: const EdgeInsets.only(left:12), child: Container( width: 40, height: 38, decoration: BoxDecoration( color: Colors.green, borderRadius: BorderRadius.circular(5), border: Border.all(color: Colors.white), ), child: InkWell( onTap: () { setState(() { counter++; }); }, child: const Center( child: Text( "+", style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), ), ), ), ), ) ], ), ), Padding( padding: const EdgeInsets.only(top:12), child: ElevatedButton( onPressed: () async { storeFood(counter,context); }, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: Colors.green, shadowColor: Colors.greenAccent, elevation: 3, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5.0), ), ), child: const Text( '+', style: TextStyle(fontSize: 25), ), ), ) ], ), ); } }