import 'package:assorted_layout_widgets/assorted_layout_widgets.dart'; import 'package:basic_utils/basic_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:hive/hive.dart'; import '../../../helper/cast_helper.dart'; import '../../../helper/format_helper.dart'; import '../../../models/food.dart'; import '../../../services/statistics.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 todayBox = Hive.box(dotenv.env['TODAY_BOX']!); if (!todayBox.isOpen){ Hive.openBox(dotenv.env['TODAY_BOX']!); } for(int i = 0; i < counter; i++){ DataService.instance.addItemToMainBox(widget.food, widget.cardName); addValuesToList(todayBox, 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), ), ), ) ], ), ); } }