Skip to content

Instantly share code, notes, and snippets.

@annacruz
Created October 6, 2019 21:57
Show Gist options
  • Save annacruz/f86a59b4ada6bbb34a334739e11129fb to your computer and use it in GitHub Desktop.
Save annacruz/f86a59b4ada6bbb34a334739e11129fb to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
TextEditingController weightController = TextEditingController();
TextEditingController heightController = TextEditingController();
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _infoText = 'Informe seus dados';
void _resetFields(){
weightController.text = '';
heightController.text = '';
setState(() {
_infoText = 'Informe seus dados';
_formKey = GlobalKey<FormState>();
});
}
void _calculate(){
setState(() {
double weight = double.parse(weightController.text);
double height = double.parse(heightController.text) / 100;
double imc = weight / (height * height);
String imcString = imc.toStringAsPrecision(4);
if(imc < 18.6) {
_infoText = 'Abaixo do Peso($imcString)';
} else if (imc >= 18.6 && imc < 24.9) {
_infoText = 'Peso Ideal ($imcString)';
} else if (imc >= 24.9 && imc < 29.9) {
_infoText = 'Levemente acima do peso ($imcString)';
} else if (imc >= 29.9 && imc < 34.9) {
_infoText = 'Obesidade grau I ($imcString)';
} else if (imc > 34.9 && imc <= 39.9) {
_infoText = 'Obesidade grau II ($imcString)';
} else if (imc >= 40) {
_infoText = 'Obesidade grau III ($imcString)';
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Calculadora de IMC'),
centerTitle: true,
backgroundColor: Colors.green,
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: _resetFields ,
)
],
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Icon(Icons.person_outline, size: 120.0, color: Colors.green),
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Peso (kg)',
labelStyle: TextStyle(color: Colors.green)),
textAlign: TextAlign.center,
style: TextStyle(color: Colors.green, fontSize: 25.0),
controller: weightController,
validator: (value) {
if(value.isEmpty){
return 'Insira seu peso';
}
return null;
},
),
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Altura (cm)',
labelStyle: TextStyle(color: Colors.green)),
textAlign: TextAlign.center,
style: TextStyle(color: Colors.green, fontSize: 25.0),
controller: heightController,
validator: (value) {
if(value.isEmpty){
return 'Insira sua altura';
}
return null;
},
),
Padding(
padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
child: Container(
height: 50.0,
child: RaisedButton(
onPressed: () {
if(_formKey.currentState.validate()){
_calculate();
}
},
child: Text('Calcular', style: TextStyle(color: Colors.white, fontSize: 25.0),),
color: Colors.green,
)
),
),
Text(_infoText,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.green, fontSize: 25.0),
)
],
),
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment