feat: adjust list logic on today page
parent
5553a521d1
commit
fd4e07fc8c
|
@ -9,7 +9,7 @@ import '../../pages/nav_pages/search_food.dart';
|
|||
class CardComponent extends StatelessWidget {
|
||||
final String title;
|
||||
final Color color;
|
||||
final List<dynamic> foods;
|
||||
final List<Food> foods;
|
||||
|
||||
const CardComponent(this.title, this.foods, {super.key, Color? color})
|
||||
: color = color ?? Colors.black;
|
||||
|
@ -58,7 +58,7 @@ class CardComponent extends StatelessWidget {
|
|||
"${countCalories(castDynamicToListFood(foods))} Kalorien",
|
||||
),
|
||||
CardDataFoodComponent(
|
||||
castDynamicToListFood(foods),
|
||||
foods,
|
||||
color,
|
||||
),
|
||||
Padding(
|
||||
|
|
|
@ -2,6 +2,8 @@ import 'package:ernaehrung/android/components/card/card_food_item_component.dart
|
|||
import 'package:ernaehrung/android/models/food.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../config/format_helper.dart';
|
||||
|
||||
class CardDataFoodComponent extends StatelessWidget {
|
||||
final List<Food> foods;
|
||||
final Color color;
|
||||
|
@ -19,11 +21,19 @@ class CardDataFoodComponent extends StatelessWidget {
|
|||
ListView.builder(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
itemCount: foods.length,
|
||||
itemCount: getMapOfDistinctElementsWithCounterAndCalories(
|
||||
foods)
|
||||
.keys
|
||||
.length,
|
||||
itemBuilder: (context, i) {
|
||||
Map<String, List<int>> map =
|
||||
getMapOfDistinctElementsWithCounterAndCalories(
|
||||
foods);
|
||||
String foodName = map.keys.elementAt(i);
|
||||
List<int> values = map.values.elementAt(i);
|
||||
return Column(
|
||||
children: [
|
||||
CardFoodItemComponent(foods[i]),
|
||||
CardFoodItemComponent(foodName,values),
|
||||
Divider(
|
||||
color: Colors.grey.shade300,
|
||||
thickness: 1.2,
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import 'package:ernaehrung/android/models/food.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CardFoodItemComponent extends StatelessWidget {
|
||||
final Food food;
|
||||
final String foodName;
|
||||
final List<int> countAndCalories; // [count, calories]
|
||||
|
||||
const CardFoodItemComponent(this.food, {Key? key}) : super(key: key);
|
||||
const CardFoodItemComponent(this.foodName,this.countAndCalories, {Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -16,7 +16,7 @@ class CardFoodItemComponent extends StatelessWidget {
|
|||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
food.name.toString(),
|
||||
"$foodName - ${countAndCalories[0] * 100}g",
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 17,
|
||||
|
@ -26,7 +26,7 @@ class CardFoodItemComponent extends StatelessWidget {
|
|||
maxLines: 2,
|
||||
),
|
||||
Text(
|
||||
"\n${food.calories.toString()} kcal | 100g",
|
||||
"\n${countAndCalories[1]} kcal | 100g",
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
],
|
||||
|
|
|
@ -19,16 +19,18 @@ class SearchedFoodComponent extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
||||
|
||||
void storeFood() async {
|
||||
showSuccessToast();
|
||||
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
|
||||
int counter = 1;
|
||||
void storeFood(int counter) async {
|
||||
final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!);
|
||||
if (!mealplanBox.isOpen){
|
||||
Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
|
||||
}
|
||||
for(int i = 0; i < counter; i++){
|
||||
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
|
||||
addValuesToList(mealplanBox, widget.cardName, [widget.food]);
|
||||
}
|
||||
showSuccessToast();
|
||||
}
|
||||
|
||||
void showSuccessToast(){
|
||||
Fluttertoast.showToast(
|
||||
|
@ -53,12 +55,13 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||
margin: const EdgeInsets.only(top:8),
|
||||
margin: const EdgeInsets.only(top: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.lightGreen,
|
||||
borderRadius: BorderRadius.circular(5), // Rounded corners with 5px radius
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
),
|
||||
child: WrapSuper(
|
||||
spacing: 32,
|
||||
|
@ -69,7 +72,7 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
|||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: MediaQuery.of(context).size.width * 0.8,
|
||||
width: MediaQuery.of(context).size.width * 0.6,
|
||||
child: Text(
|
||||
widget.food.name,
|
||||
maxLines: 2,
|
||||
|
@ -83,7 +86,7 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
|||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: MediaQuery.of(context).size.width * 0.8,
|
||||
width: MediaQuery.of(context).size.width * 0.6,
|
||||
child: Text(
|
||||
"${widget.food.calories} kcal | 100g",
|
||||
maxLines: 1,
|
||||
|
@ -98,9 +101,78 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
|||
),
|
||||
],
|
||||
),
|
||||
ElevatedButton(
|
||||
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();
|
||||
storeFood(counter);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
foregroundColor: Colors.white,
|
||||
|
@ -108,19 +180,23 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
|
|||
shadowColor: Colors.greenAccent,
|
||||
elevation: 3,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(32.0),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'+',
|
||||
style: TextStyle(
|
||||
fontSize: 25
|
||||
),
|
||||
style: TextStyle(fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ Map<String,List<int>> getMapOfDistinctElementsWithCounterAndCalories(List<Food>
|
|||
return resultMap;
|
||||
}
|
||||
|
||||
|
||||
List<Food> getListOfDistinctElements(List<Food> foods){
|
||||
List<Food> result = [];
|
||||
for(int i = 0; i < foods.length;i++){
|
||||
|
|
Loading…
Reference in New Issue