Flutter-Ernaehrungsapp/lib/pages/search_food/sub_components/searched_food.dart

203 lines
6.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:basic_utils/basic_utils.dart';
2023-05-29 12:08:46 +02:00
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';
2023-06-25 14:43:07 +02:00
import '../../../helper/cast_helper.dart';
import '../../../helper/format_helper.dart';
import '../../../models/food.dart';
import '../../../services/statistics.dart';
2023-05-29 12:08:46 +02:00
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;
2023-06-16 12:03:04 +02:00
void storeFood(int counter, BuildContext context) async {
2023-06-25 14:43:07 +02:00
final todayBox = Hive.box(dotenv.env['TODAY_BOX']!);
if (!todayBox.isOpen){
Hive.openBox(dotenv.env['TODAY_BOX']!);
2023-05-29 12:08:46 +02:00
}
2023-06-02 20:02:32 +02:00
for(int i = 0; i < counter; i++){
2023-06-25 14:43:07 +02:00
DataService.instance.addItemToMainBox(widget.food, widget.cardName);
addValuesToList(todayBox, widget.cardName, [widget.food]);
2023-06-02 20:02:32 +02:00
}
2023-06-16 12:03:04 +02:00
showSuccessToast(context);
2023-05-29 12:08:46 +02:00
}
2023-06-16 12:03:04 +02:00
void showSuccessToast(BuildContext context){
Fluttertoast.showToast(
2023-06-16 12:03:04 +02:00
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<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 {
2023-06-16 12:03:04 +02:00
storeFood(counter,context);
2023-06-02 20:02:32 +02:00
},
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
}
}