Skip to content

Commit

Permalink
#11 use a List of Food and caculate the avg for a categorie (cal)
Browse files Browse the repository at this point in the history
  • Loading branch information
JessieRamaux committed Aug 3, 2021
1 parent 0218436 commit d68cccf
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 82 deletions.
92 changes: 91 additions & 1 deletion lib/pages/statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:quiver/core.dart';
import 'package:transfer_learning_fruit_veggies/source/statistics2.dart';

class Statistics extends StatefulWidget {
Expand Down Expand Up @@ -33,6 +34,11 @@ class _StatisticsState extends State<Statistics> {
List<String> titleButtonRadio = ['Week', 'Month', 'Year'];
late LineChartData dataXTitle;

//Use of a Food list
List<Food> itemListDay = [];
// Food finalMeal = Food(nameFood: "");
// finalMeal.volEstim = 15;

@override
void initState() {
super.initState();
Expand All @@ -44,13 +50,17 @@ class _StatisticsState extends State<Statistics> {
// day of the week: 1:monday,...7:sunday
weekDay = _Today.weekday;
maxValue = 2000;
//test food avg
fillItemFood(itemListDay, 9);
print('avg food item for a day is: ${getAvgFoodItem(itemListDay)}');
}

@override
void dispose() {
super.dispose();
data = [];
dataCal = [];
itemListDay = [];
}

//get AVG from the data
Expand All @@ -63,6 +73,17 @@ class _StatisticsState extends State<Statistics> {
return mean / i;
}

// the avg of a food item in a list
//add an integer to choose the category (protein, glucide..) ? Or made the avg for all categories
int getAvgFoodItem(List<Food> food) {
double mean = 0;
for (int i = 0; i < food.length; i++) {
mean += food[i].kal;
}
mean = mean / food.length;
return mean.toInt();
}

/* -----------CREATE DATA TO DISPLAY----------------- */

//give a size and inster plot (i,0) , initialize/register data
Expand Down Expand Up @@ -104,6 +125,29 @@ class _StatisticsState extends State<Statistics> {
}
}

// fill a list of Food for a day
void fillItemFood(List<Food> food, int taille) {
int kal = -1, protein = -1, carbohydrates = -1, sugar = -1, fat = -1;
var r = new Random();
for (int i = 0; i < taille; i++) {
kal = ((r.nextDouble()) * 200 + 10).toInt();
protein = ((r.nextDouble()) * 200 + 10).toInt();
carbohydrates = ((r.nextDouble()) * 200 + 10).toInt();
sugar = ((r.nextDouble()) * 200 + 10).toInt();
fat = ((r.nextDouble()) * 200 + 10).toInt();
food.add(Food(
nameFood: 'numero $i',
id: i,
kal: kal,
protein: protein,
carbohydrates: carbohydrates,
sugar: sugar,
fat: fat,
));
}
print('voici la liste de food item: $food');
}

/* ____________Widget Part________________*/

// display the top list above the chart
Expand Down Expand Up @@ -308,7 +352,7 @@ class _StatisticsState extends State<Statistics> {
displayRadioButton(),
DisplayChart(),
AVGButton(),
//Statistics2(),
Statistics2(),
],
);
}
Expand Down Expand Up @@ -438,3 +482,49 @@ class _StatisticsState extends State<Statistics> {
return xMax;
}
}

//class Food
class Food {
// static int cpt = 0;
int id = 0;
String nameFood = '';
String nutriscore = '';
int volEstim = -1;
int volumicMass = -1;
int mass = -1;
int kal = -1;
int protein = -1;
int carbohydrates = -1;
int sugar = -1;
int fat = -1;

Food({
required this.nameFood,
required this.id,
required this.kal,
required this.protein,
required this.carbohydrates,
required this.sugar,
required this.fat,
});

String toString() {
String nameFood = 'nameFood : ' + this.nameFood + '\n';
// String volEstim = 'Volume : ' + this.volEstim.toString() + 'cm³' + '\n';
// String volumicMass =
// 'Volumic mass : ' + (this.volumicMass).toString() + '\n';
// String mass = 'mass food : ' + this.mass.toString() + '\n';
String kal = this.kal.toString() + 'kcal\n';
// String protein = 'protein : ' + this.protein.toString() + 'g' + '\n';
// String carbohydrates =
// 'carbohydrates : ' + this.carbohydrates.toString() + 'g' + '\n';
// String sugar = 'sugar : ' + this.sugar.toString() + 'g' + '\n';
// String fat = 'fat : ' + this.fat.toString() + 'g' + '\n';
String finalString =
(nameFood + kal); //+ volEstim + protein + carbohydrates + sugar + fat);
// pour moi pas de sens de mettre la masse volumique d'un repas
// if (this.volumicMass != null) finalString += volumicMass;
// if (this.mass != null) finalString += mass;
return finalString;
}
}
166 changes: 85 additions & 81 deletions lib/source/statistics2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,96 @@ class Statistics2 extends StatefulWidget {

class _StatisticsState2 extends State<Statistics2> {
int touchedIndex = -1;
int numberCategorie = 5;
/* ___________________Widget part _______________________*/

//create Piechart
PieChart createPieChart() {
return PieChart(
PieChartData(
pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
final desiredTouch =
pieTouchResponse.touchInput is! PointerExitEvent &&
pieTouchResponse.touchInput is! PointerUpEvent;
if (desiredTouch && pieTouchResponse.touchedSection != null) {
touchedIndex =
pieTouchResponse.touchedSection!.touchedSectionIndex;
} else {
touchedIndex = -1;
}
});
}),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 0,
centerSpaceRadius: 40,
sections: showingSections()),
);
}

//display a Card to insert a graphe
Card displayCard() {
return Card(
color: Colors.white,
child: Row(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1,
child: createPieChart(),
),
),
displayPieCHartColor(),
const SizedBox(
width: 28,
),
],
),
);
}

// widget to display the pie chart categories
Widget displayPieCHartColor() {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
displayEachCategories(Color(0xff0293ee), 'First', true, 4),
displayEachCategories(Color(0xfff8b250), 'Second', true, 4),
displayEachCategories(Color(0xff845bef), 'Third', true, 4),
displayEachCategories(Color(0xff13d38e), 'Fourth', true, 18),
],
);
}

//WIdget for each categories colors
Widget displayEachCategories(
Color color, String title, bool isSquare, double heightBox) {
return Column(
children: <Widget>[
Indicator(
color: color,
text: title,
isSquare: isSquare,
),
SizedBox(
height: heightBox,
),
],
);
}

@override
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 1.3,
child: Card(
color: Colors.white,
child: Row(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1,
child: PieChart(
PieChartData(
pieTouchData:
PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
final desiredTouch = pieTouchResponse.touchInput
is! PointerExitEvent &&
pieTouchResponse.touchInput is! PointerUpEvent;
if (desiredTouch &&
pieTouchResponse.touchedSection != null) {
touchedIndex = pieTouchResponse
.touchedSection!.touchedSectionIndex;
} else {
touchedIndex = -1;
}
});
}),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 0,
centerSpaceRadius: 40,
sections: showingSections()),
),
),
),
Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Indicator(
color: Color(0xff0293ee),
text: 'First',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xfff8b250),
text: 'Second',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xff845bef),
text: 'Third',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xff13d38e),
text: 'Fourth',
isSquare: true,
),
SizedBox(
height: 18,
),
],
),
const SizedBox(
width: 28,
),
],
),
),
child: displayCard(),
);
}

Expand Down

0 comments on commit d68cccf

Please sign in to comment.