Flutter-Ernaehrungsapp/lib/components/founded_search_component.dart

114 lines
3.8 KiB
Dart

import 'package:assorted_layout_widgets/assorted_layout_widgets.dart';
import 'package:ernaehrung/config/setup_todaybox_config.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<SearchedFoodComponent> createState() => _SearchFoodComponentState();
}
class _SearchFoodComponentState extends State<SearchedFoodComponent> {
void storeFood() async {
final todayBox = Hive.box(dotenv.env['TODAY_BOX']!);
if (!todayBox.isOpen){
Hive.openBox(dotenv.env['TODAY_BOX']!);
}
final todayBoxDateField = todayBox.containsKey(dotenv.env['DATE_FIELD']!.toString());
final todayBoxEatingField = todayBox.containsKey(widget.cardName.toUpperCase().toString());
if (todayBoxDateField && todayBoxEatingField){
updateFoodInStorage(todayBox);
}else{
addFoodToStorage(todayBox);
}
}
void updateFoodInStorage(dynamic todayBox) async {
final values = todayBox.get(widget.cardName.toUpperCase());
values.add(widget.food);
await todayBox.put(widget.cardName.toUpperCase(), values);
}
void addFoodToStorage(dynamic todayBox) async{
List<Food> foods = [];
foods.add(widget.food);
todayBox.put(widget.cardName.toUpperCase(), foods);
todayBox.put(dotenv.env['DATE_FIELD']!, getFormatedTodayDate());
}
@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('+'),
)
],
));
}
}