1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import 'package:flutter/material.dart'; import 'package:programminghub/bmicalc/bmi_calc_input_field.dart';
class BmiCalculatorScreen extends StatefulWidget { @override State<StatefulWidget> createState() { return _BmiCalculatorScreenState(); } }
class _BmiCalculatorScreenState extends State<BmiCalculatorScreen> { GlobalKey<FormState> _formKey = GlobalKey(); TextEditingController weightControl = TextEditingController(); TextEditingController heightControl = TextEditingController(); double bmiIndex = 0.0;
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("BMI calculator"), ), body: Center( child: Padding( padding: EdgeInsets.only(left:20, right:20), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ BmiCalcuInputField( inputHint: "Enter Body Weight (in kg)", errorToDisplay: "Invalid input for weight", inputontroller: weightControl, ), SizedBox( height: 30, ), BmiCalcuInputField( inputHint: "Enter Body Heigh (in m)", errorToDisplay: "Invalid input for height", inputontroller: heightControl, ), SizedBox( height: 30, ), ElevatedButton( onPressed: () { if(_formKey.currentState!= null && _formKey.currentState!.validate()) { print("Weight input ${weightControl.text}"); print("Height input ${heightControl.text}"); double weightInKg = double.parse(weightControl.text); double heightInMeter = double.parse(heightControl.text); bmiIndex = weightInKg / (heightInMeter * heightInMeter); setState(() {
}); } }, child: Text("Calcuate BMI"), ), SizedBox( height: 30, ), Container( width: 200, height: 80, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.lightGreen, ), alignment: Alignment.center, child: Text( "BMI Index: -${bmiIndex.toStringAsFixed(2)}", style: TextStyle( color: Colors.white, fontSize: 24, ), ), ), ], ), ), ), ), ); } }
|