Advertisement
joaopaulofcc

Untitled

Sep 9th, 2020
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.48 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(
  4.       MaterialApp(
  5.         home: Home(),
  6.         debugShowCheckedModeBanner: false,
  7.       ),
  8.     );
  9.  
  10. class Home extends StatefulWidget {
  11.   @override
  12.   _HomeState createState() => _HomeState();
  13. }
  14.  
  15. class _HomeState extends State<Home> {
  16.   GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  17.  
  18.   TextEditingController _weightController = TextEditingController();
  19.   TextEditingController _heightController = TextEditingController();
  20.   String _result;
  21.  
  22.   @override
  23.   void initState() {
  24.     super.initState();
  25.     resetFields();
  26.   }
  27.  
  28.   void resetFields() {
  29.     _weightController.text = '';
  30.     _heightController.text = '';
  31.     setState(() {
  32.       _result = 'Informe seus dados';
  33.     });
  34.   }
  35.  
  36.   void calculateImc() {
  37.     double weight = double.parse(_weightController.text);
  38.     double height = double.parse(_heightController.text) / 100.0;
  39.     double imc = weight / (height * height);
  40.  
  41.     setState(() {
  42.       _result = "IMC = ${imc.toStringAsPrecision(2)}\n";
  43.       if (imc < 18.6)
  44.         _result += "Abaixo do peso";
  45.       else if (imc < 25.0)
  46.         _result += "Peso ideal";
  47.       else if (imc < 30.0)
  48.         _result += "Levemente acima do peso";
  49.       else if (imc < 35.0)
  50.         _result += "Obesidade Grau I";
  51.       else if (imc < 40.0)
  52.         _result += "Obesidade Grau II";
  53.       else
  54.         _result += "Obesidade Grau IIII";
  55.     });
  56.   }
  57.  
  58.   @override
  59.   Widget build(BuildContext context) {
  60.     return Scaffold(
  61.         appBar: buildAppBar(),
  62.         backgroundColor: Colors.white,
  63.         body: SingleChildScrollView(
  64.             padding: EdgeInsets.all(20.0), child: buildForm()));
  65.   }
  66.  
  67.   AppBar buildAppBar() {
  68.     return AppBar(
  69.       title: Text('Calculadora de IMC'),
  70.       backgroundColor: Colors.blue,
  71.       actions: <Widget>[
  72.         IconButton(
  73.           icon: Icon(Icons.refresh),
  74.           onPressed: () {
  75.             resetFields();
  76.           },
  77.         )
  78.       ],
  79.     );
  80.   }
  81.  
  82.   Form buildForm() {
  83.     return Form(
  84.       key: _formKey,
  85.       child: Column(
  86.         crossAxisAlignment: CrossAxisAlignment.stretch,
  87.         children: <Widget>[
  88.           buildTextFormField(
  89.               label: "Peso (kg)",
  90.               error: "Insira seu peso!",
  91.               controller: _weightController),
  92.           buildTextFormField(
  93.               label: "Altura (cm)",
  94.               error: "Insira uma altura!",
  95.               controller: _heightController),
  96.           buildTextResult(),
  97.           buildCalculateButton(),
  98.         ],
  99.       ),
  100.     );
  101.   }
  102.  
  103.   Padding buildCalculateButton() {
  104.     return Padding(
  105.       padding: EdgeInsets.symmetric(vertical: 36.0),
  106.       child: RaisedButton(
  107.         onPressed: () {
  108.           if (_formKey.currentState.validate()) {
  109.             calculateImc();
  110.           }
  111.         },
  112.         child: Text('CALCULAR', style: TextStyle(color: Colors.white)),
  113.       ),
  114.     );
  115.   }
  116.  
  117.   Padding buildTextResult() {
  118.     return Padding(
  119.       padding: EdgeInsets.symmetric(vertical: 36.0),
  120.       child: Text(
  121.         _result,
  122.         textAlign: TextAlign.center,
  123.       ),
  124.     );
  125.   }
  126.  
  127.   TextFormField buildTextFormField(
  128.       {TextEditingController controller, String error, String label}) {
  129.     return TextFormField(
  130.       keyboardType: TextInputType.number,
  131.       decoration: InputDecoration(labelText: label),
  132.       controller: controller,
  133.       validator: (text) {
  134.         return text.isEmpty ? error : null;
  135.       },
  136.     );
  137.   }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement