feat: adjust list logic on today page

pull/5/head
2211260 2023-06-02 20:02:32 +02:00
parent 5553a521d1
commit fd4e07fc8c
5 changed files with 123 additions and 36 deletions

View File

@ -9,7 +9,7 @@ import '../../pages/nav_pages/search_food.dart';
class CardComponent extends StatelessWidget { class CardComponent extends StatelessWidget {
final String title; final String title;
final Color color; final Color color;
final List<dynamic> foods; final List<Food> foods;
const CardComponent(this.title, this.foods, {super.key, Color? color}) const CardComponent(this.title, this.foods, {super.key, Color? color})
: color = color ?? Colors.black; : color = color ?? Colors.black;
@ -58,7 +58,7 @@ class CardComponent extends StatelessWidget {
"${countCalories(castDynamicToListFood(foods))} Kalorien", "${countCalories(castDynamicToListFood(foods))} Kalorien",
), ),
CardDataFoodComponent( CardDataFoodComponent(
castDynamicToListFood(foods), foods,
color, color,
), ),
Padding( Padding(

View File

@ -2,6 +2,8 @@ import 'package:ernaehrung/android/components/card/card_food_item_component.dart
import 'package:ernaehrung/android/models/food.dart'; import 'package:ernaehrung/android/models/food.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../config/format_helper.dart';
class CardDataFoodComponent extends StatelessWidget { class CardDataFoodComponent extends StatelessWidget {
final List<Food> foods; final List<Food> foods;
final Color color; final Color color;
@ -19,11 +21,19 @@ class CardDataFoodComponent extends StatelessWidget {
ListView.builder( ListView.builder(
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true, shrinkWrap: true,
itemCount: foods.length, itemCount: getMapOfDistinctElementsWithCounterAndCalories(
foods)
.keys
.length,
itemBuilder: (context, i) { 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( return Column(
children: [ children: [
CardFoodItemComponent(foods[i]), CardFoodItemComponent(foodName,values),
Divider( Divider(
color: Colors.grey.shade300, color: Colors.grey.shade300,
thickness: 1.2, thickness: 1.2,

View File

@ -1,10 +1,10 @@
import 'package:ernaehrung/android/models/food.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class CardFoodItemComponent extends StatelessWidget { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -16,7 +16,7 @@ class CardFoodItemComponent extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Text(
food.name.toString(), "$foodName - ${countAndCalories[0] * 100}g",
style: const TextStyle( style: const TextStyle(
color: Colors.white, color: Colors.white,
fontSize: 17, fontSize: 17,
@ -26,7 +26,7 @@ class CardFoodItemComponent extends StatelessWidget {
maxLines: 2, maxLines: 2,
), ),
Text( Text(
"\n${food.calories.toString()} kcal | 100g", "\n${countAndCalories[1]} kcal | 100g",
style: const TextStyle(color: Colors.white), style: const TextStyle(color: Colors.white),
), ),
], ],

View File

@ -19,15 +19,17 @@ class SearchedFoodComponent extends StatefulWidget {
} }
class _SearchFoodComponentState extends State<SearchedFoodComponent> { class _SearchFoodComponentState extends State<SearchedFoodComponent> {
int counter = 1;
void storeFood() async { void storeFood(int counter) async {
showSuccessToast();
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!);
if (!mealplanBox.isOpen){ if (!mealplanBox.isOpen){
Hive.openBox(dotenv.env['MEALPLAN_BOX']!); Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
} }
addValuesToList(mealplanBox, widget.cardName, [widget.food]); for(int i = 0; i < counter; i++){
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
addValuesToList(mealplanBox, widget.cardName, [widget.food]);
}
showSuccessToast();
} }
void showSuccessToast(){ void showSuccessToast(){
@ -53,12 +55,13 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
margin: const EdgeInsets.only(top:8), margin: const EdgeInsets.only(top: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.lightGreen, color: Colors.lightGreen,
borderRadius: BorderRadius.circular(5), // Rounded corners with 5px radius borderRadius: BorderRadius.circular(5),
), ),
child: WrapSuper( child: WrapSuper(
spacing: 32, spacing: 32,
@ -69,7 +72,7 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.8, width: MediaQuery.of(context).size.width * 0.6,
child: Text( child: Text(
widget.food.name, widget.food.name,
maxLines: 2, maxLines: 2,
@ -83,7 +86,7 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
), ),
), ),
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.8, width: MediaQuery.of(context).size.width * 0.6,
child: Text( child: Text(
"${widget.food.calories} kcal | 100g", "${widget.food.calories} kcal | 100g",
maxLines: 1, maxLines: 1,
@ -98,29 +101,102 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
), ),
], ],
), ),
ElevatedButton( Padding(
onPressed: () async { padding: const EdgeInsets.only(top:12),
storeFood(); child: Row(
}, children: [
style: ElevatedButton.styleFrom( Padding(
foregroundColor: Colors.white, padding: const EdgeInsets.only(right :12),
backgroundColor: Colors.green, child: Container(
shadowColor: Colors.greenAccent, width: 40,
elevation: 3, height: 40,
shape: RoundedRectangleBorder( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(32.0), color: Colors.green,
), borderRadius: BorderRadius.circular(5),
), border: Border.all(color: Colors.white),
child: const Text( ),
'+', child: InkWell(
style: TextStyle( onTap: () {
fontSize: 25 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(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(
'+',
style: TextStyle(fontSize: 25),
),
),
)
], ],
), ),
); );
} }
} }

View File

@ -30,6 +30,7 @@ Map<String,List<int>> getMapOfDistinctElementsWithCounterAndCalories(List<Food>
return resultMap; return resultMap;
} }
List<Food> getListOfDistinctElements(List<Food> foods){ List<Food> getListOfDistinctElements(List<Food> foods){
List<Food> result = []; List<Food> result = [];
for(int i = 0; i < foods.length;i++){ for(int i = 0; i < foods.length;i++){