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

203 lines
6.5 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:basic_utils/basic_utils.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:fluttertoast/fluttertoast.dart';
2023-05-29 12:08:46 +02:00
import 'package:hive/hive.dart';
import '../config/format_helper.dart';
2023-05-29 12:08:46 +02:00
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> {
2023-06-02 20:02:32 +02:00
int counter = 1;
void storeFood(int counter) async {
2023-06-01 12:14:23 +02:00
final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!);
if (!mealplanBox.isOpen){
Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
2023-05-29 12:08:46 +02:00
}
2023-06-02 20:02:32 +02:00
for(int i = 0; i < counter; i++){
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
addValuesToList(mealplanBox, widget.cardName, [widget.food]);
}
showSuccessToast();
2023-05-29 12:08:46 +02:00
}
void showSuccessToast(){
Fluttertoast.showToast(
msg: "${getToastFoodNameString(widget.food)} 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<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) {
2023-06-02 20:02:32 +02:00
2023-05-29 12:08:46 +02:00
return Container(
2023-06-02 18:35:07 +02:00
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
2023-06-02 20:02:32 +02:00
margin: const EdgeInsets.only(top: 8),
2023-06-02 18:35:07 +02:00
decoration: BoxDecoration(
color: Colors.lightGreen,
2023-06-02 20:02:32 +02:00
borderRadius: BorderRadius.circular(5),
2023-06-02 18:35:07 +02:00
),
child: WrapSuper(
spacing: 32,
wrapType: WrapType.balanced,
wrapFit: WrapFit.larger,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
2023-06-02 20:02:32 +02:00
width: MediaQuery.of(context).size.width * 0.6,
2023-06-02 18:35:07 +02:00
child: Text(
widget.food.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
softWrap: false,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20.0,
2023-05-29 12:08:46 +02:00
),
),
2023-06-02 18:35:07 +02:00
),
SizedBox(
2023-06-02 20:02:32 +02:00
width: MediaQuery.of(context).size.width * 0.6,
2023-06-02 18:35:07 +02:00
child: Text(
2023-06-02 19:21:06 +02:00
"${widget.food.calories} kcal | 100g",
2023-06-02 18:35:07 +02:00
maxLines: 1,
overflow: TextOverflow.ellipsis,
softWrap: false,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20.0,
2023-05-29 12:08:46 +02:00
),
),
2023-06-02 18:35:07 +02:00
),
],
),
2023-06-02 20:02:32 +02:00
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),
),
),
),
),
)
],
2023-05-29 12:08:46 +02:00
),
2023-06-02 20:02:32 +02:00
),
Padding(
padding: const EdgeInsets.only(top:12),
child: ElevatedButton(
onPressed: () async {
storeFood(counter);
},
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.green,
shadowColor: Colors.greenAccent,
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
child: const Text(
2023-06-02 18:35:07 +02:00
'+',
2023-06-02 20:02:32 +02:00
style: TextStyle(fontSize: 25),
2023-05-29 12:08:46 +02:00
),
2023-06-02 18:35:07 +02:00
),
2023-06-02 20:02:32 +02:00
)
2023-06-02 18:35:07 +02:00
],
),
);
2023-06-02 20:02:32 +02:00
2023-05-29 12:08:46 +02:00
}
}