From 43868f7d931777aad7e71851aa274c046f42a5cc Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 4 Jul 2021 00:27:12 +0200 Subject: [PATCH] feat: result page --- .vscode/settings.json | 10 ++++ README.md | 6 ++- lib/calculator.dart | 109 +++++++++++++++++++++++++++++++++++------- lib/main.dart | 17 +++---- lib/result.dart | 37 ++++++++++++++ 5 files changed, 153 insertions(+), 26 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 lib/result.dart diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..eb56df2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "spellright.language": [ + "en" + ], + "spellright.documentTypes": [ + "markdown", + "latex", + "plaintext" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index c2c4fc5..c70cdf5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ Compute and track your Body Mass Index and Body Fat Percentage - [ ] bmi over 40 : red - morbid obesity - [ ] bmi tracker: date, value, graph - [ ] profile: save age, height, genre, local storage -- [ ] bmi on second screen +- [x] bmi on second screen - [ ] feedback on genre select -* [doc](https://www.santepratique.fr/nutrition/calcul-imc) \ No newline at end of file +* [doc](https://www.santepratique.fr/nutrition/calcul-imc) + +- [design inspiration](https://dribbble.com/shots/13913052-DailyUI-004-Calculator) diff --git a/lib/calculator.dart b/lib/calculator.dart index 9f27a3e..a9e8c35 100644 --- a/lib/calculator.dart +++ b/lib/calculator.dart @@ -1,22 +1,32 @@ import 'package:flutter/material.dart'; +import 'result.dart'; + class CalculatorPage extends StatefulWidget { CalculatorPage({Key? key}) : super(key: key); final String title = 'BMI Calculator'; + static const routeName = '/'; @override _CalculatorPageState createState() => _CalculatorPageState(); } class _CalculatorPageState extends State { - int _counter = 0; + double _weight = 60; + double _height = 165; + double _age = 18; + int _gender = 0; // male = 1; female = 0 - void _incrementCounter() { - setState(() { - _counter++; - }); + @override + void initState() { + super.initState(); } + int bodyMassIndex() => ((_weight / (_height * _height)) * 10000).round(); + + int bodyFatPercentage() => + (1.2 * bodyMassIndex() + .23 * _age - 10.8 * _gender - 5.4).round(); + @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( @@ -24,22 +34,89 @@ class _CalculatorPageState extends State { ), body: Center( child: Column( - mainAxisAlignment: MainAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - Text( - 'You have pushed the button this many times:', + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Expanded( + child: Ink( + decoration: const ShapeDecoration( + color: Colors.lightGreen, + shape: CircleBorder(), + ), + child: IconButton( + icon: const Icon(Icons.female), + tooltip: 'Choose female gender', + onPressed: () { + setState(() => _gender = 0); + }, + color: Colors.white, + ), + ), + ), + Expanded( + child: Ink( + decoration: const ShapeDecoration( + color: Colors.lightGreen, + shape: CircleBorder(), + ), + child: IconButton( + icon: const Icon(Icons.male), + tooltip: 'Choose male gender', + onPressed: () { + setState(() => _gender = 1); + }, + color: Colors.white, + ), + ), + ), + ], ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, + Text('Weight'), + Text("${_weight.round().toString()} kg"), + Slider( + value: _weight, + min: 0, + max: 100, + divisions: 100, + label: _weight.round().toString(), + onChanged: (double value) => setState(() => _weight = value), + ), + Text('Height'), + Text("${_height.round().toString()} cm"), + Slider( + value: _height, + min: 100, + max: 200, + divisions: 100, + label: _height.round().toString(), + onChanged: (double value) => setState(() => _height = value), + ), + Text('Age'), + Text("${_age.round().toString()} years old"), + Slider( + value: _age, + min: 10, + max: 100, + divisions: 100, + label: _age.round().toString(), + onChanged: (double value) => setState(() => _age = value), + ), + ElevatedButton( + onPressed: () { + print( + "BMI: ${bodyMassIndex()}; BFP: ${bodyFatPercentage()}"); + Navigator.pushNamed( + context, + ResultPage.routeName, + arguments: Results(bodyMassIndex(), bodyFatPercentage()), + ); + }, + child: Text('Calculate'), ), ], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), ); } diff --git a/lib/main.dart b/lib/main.dart index b47a069..dde3e61 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,18 +1,19 @@ import 'package:flutter/material.dart'; import 'calculator.dart'; +import 'result.dart'; void main() => runApp(BMIApp()); class BMIApp extends StatelessWidget { @override Widget build(BuildContext context) => MaterialApp( - title: 'BMI/BFP Calculator', - theme: ThemeData( - primarySwatch: Colors.green, - ), - home: CalculatorPage(), - ); + title: 'BMI/BFP Calculator', + theme: ThemeData(primarySwatch: Colors.green), + initialRoute: '/', + routes: { + CalculatorPage.routeName: (context) => CalculatorPage(), + ResultPage.routeName: (context) => ResultPage(), + }, + ); } - - diff --git a/lib/result.dart b/lib/result.dart new file mode 100644 index 0000000..69fbeda --- /dev/null +++ b/lib/result.dart @@ -0,0 +1,37 @@ +import 'package:flutter/material.dart'; + +class Results { + int bodyMassIndex; + int bodyFatPercentage; + + Results(this.bodyMassIndex, this.bodyFatPercentage); +} + +class ResultPage extends StatelessWidget { + static const routeName = '/results'; + + @override + Widget build(BuildContext context) { + final results = ModalRoute.of(context)!.settings.arguments as Results; + + return Scaffold( + appBar: AppBar( + title: Text("Your results"), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "Your results: BMI: ${results.bodyMassIndex}; BFP: ${results.bodyFatPercentage}"), + ElevatedButton( + onPressed: () { + Navigator.pop(context); + }, + child: Text('Go back!'), + ), + ], + )), + ); + } +}