Created
August 25, 2019 23:16
-
-
Save kleberandrade/f5b15ac8c485d20736b6e0f9616e23f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp( | |
MaterialApp( | |
home: Home(), | |
debugShowCheckedModeBanner: false, | |
), | |
); | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
GlobalKey<FormState> _formKey = GlobalKey<FormState>(); | |
TextEditingController _weightController = TextEditingController(); | |
TextEditingController _heightController = TextEditingController(); | |
String _result; | |
@override | |
void initState() { | |
super.initState(); | |
resetFields(); | |
} | |
void resetFields() { | |
_weightController.text = ''; | |
_heightController.text = ''; | |
setState(() { | |
_result = 'Informe seus dados'; | |
}); | |
} | |
void calculateImc() { | |
double weight = double.parse(_weightController.text); | |
double height = double.parse(_heightController.text) / 100.0; | |
double imc = weight / (height * height); | |
setState(() { | |
_result = "IMC = ${imc.toStringAsPrecision(2)}\n"; | |
if (imc < 18.6) | |
_result += "Abaixo do peso"; | |
else if (imc < 25.0) | |
_result += "Peso ideal"; | |
else if (imc < 30.0) | |
_result += "Levemente acima do peso"; | |
else if (imc < 35.0) | |
_result += "Obesidade Grau I"; | |
else if (imc < 40.0) | |
_result += "Obesidade Grau II"; | |
else | |
_result += "Obesidade Grau IIII"; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: buildAppBar(), | |
backgroundColor: Colors.white, | |
body: SingleChildScrollView( | |
padding: EdgeInsets.all(20.0), child: buildForm())); | |
} | |
AppBar buildAppBar() { | |
return AppBar( | |
title: Text('Calculadora de IMC'), | |
backgroundColor: Colors.blue, | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.refresh), | |
onPressed: () { | |
resetFields(); | |
}, | |
) | |
], | |
); | |
} | |
Form buildForm() { | |
return Form( | |
key: _formKey, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
buildTextFormField( | |
label: "Peso (kg)", | |
error: "Insira seu peso!", | |
controller: _weightController), | |
buildTextFormField( | |
label: "Altura (cm)", | |
error: "Insira uma altura!", | |
controller: _heightController), | |
buildTextResult(), | |
buildCalculateButton(), | |
], | |
), | |
); | |
} | |
Padding buildCalculateButton() { | |
return Padding( | |
padding: EdgeInsets.symmetric(vertical: 36.0), | |
child: RaisedButton( | |
onPressed: () { | |
if (_formKey.currentState.validate()) { | |
calculateImc(); | |
} | |
}, | |
child: Text('CALCULAR', style: TextStyle(color: Colors.white)), | |
), | |
); | |
} | |
Padding buildTextResult() { | |
return Padding( | |
padding: EdgeInsets.symmetric(vertical: 36.0), | |
child: Text( | |
_result, | |
textAlign: TextAlign.center, | |
), | |
); | |
} | |
TextFormField buildTextFormField( | |
{TextEditingController controller, String error, String label}) { | |
return TextFormField( | |
keyboardType: TextInputType.number, | |
decoration: InputDecoration(labelText: label), | |
controller: controller, | |
validator: (text) { | |
return text.isEmpty ? error : null; | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment