Flutter-Ernaehrungsapp/lib/android/components/founded_search_component.dart

104 lines
3.6 KiB
Dart
Raw Normal View History

2023-05-29 12:08:46 +02:00
import 'package:assorted_layout_widgets/assorted_layout_widgets.dart';
import 'package:ernaehrung/android/config/cast_helper.dart';
2023-05-29 12:08:46 +02:00
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<SearchedFoodComponent> createState() => _SearchFoodComponentState();
}
class _SearchFoodComponentState extends State<SearchedFoodComponent> {
StatisticsService statisticsService = StatisticsService();
void storeFood() async {
statisticsService.addItemToMainBox(widget.food, widget.cardName);
2023-05-29 12:08:46 +02:00
final todayBox = Hive.box(dotenv.env['TODAY_BOX']!);
if (!todayBox.isOpen){
Hive.openBox(dotenv.env['TODAY_BOX']!);
}
addValuesToList(todayBox, widget.cardName, [widget.food]);
2023-05-29 12:08:46 +02:00
}
void addValuesToList(box, String key, List<Food> newValues){
List<Food> 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);
2023-05-29 12:08:46 +02:00
}
@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('+'),
)
],
));
}
}