A Body Mass Index (BMI) calculator is an application that helps
determine an individual's body weight status based on their height and weight.
Here is an example implementation of a BMI calculator using Flutter:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget{
const MyApp({Key? key}) :
super(key: key);
@override
Widget build(BuildContext
context){
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner:
false,
theme: ThemeData(
primarySwatch:
Colors.cyan,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) :
super(key: key);
@override
State<MyHomePage>
createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var wtController =
TextEditingController();
var ftController =
TextEditingController();
var inController =
TextEditingController();
var result = "";
@override
Widget build(BuildContext
context) {
return Scaffold(
appBar: AppBar(
title: Text('BMI
Calculator'),
),
body: Center(
child: Container(
width: 300,
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Text('BMI',
style:TextStyle(
fontSize: 34,
fontWeight: FontWeight.w700
),),
SizedBox(height:
21,),
TextField(
controller:
wtController,
decoration:
InputDecoration(
label:
Text('Weight (in kg)'),
prefixIcon:
Icon(Icons.line_weight),
),
keyboardType:
TextInputType.number,
),
SizedBox(height:
11,),
TextField(
controller:
ftController,
decoration:
InputDecoration(
label:
Text('Height in feet'),
prefixIcon:
Icon(Icons.height),
),
keyboardType:
TextInputType.number,
),
SizedBox(height:
11,),
TextField(
controller:
inController,
decoration:
InputDecoration(
label:
Text('Height in inches'),
prefixIcon:
Icon(Icons.height),
),
keyboardType: TextInputType.number,
),
SizedBox(height:
21,),
ElevatedButton(onPressed: (){
var wt =
wtController.text.toString();
var ft = ftController.text.toString();
var inch =
inController.text.toString();
if(wt
!="" && ft!="" && inch !=""){
var iwt =
int.parse(wt);
var ift =
int.parse(ft);
var iinch =
int.parse(inch);
var tinch = (ift
* 12) + iinch;
var tcm = tinch
* 2.54;
var tm = tcm /
100;
var bmi =
iwt/(tm * tm);
var msg =
"";
if(bmi>25){
msg =
"You are Over Weight :(";
}else
if(bmi<18){
msg =
"You are Underweight :(";
}else{
msg =
"You are Healthy :)";
}
setState(() {
result =
"$msg \n Your BMI is: ${bmi.toStringAsFixed(2)} ";
});
}else{
setState((){
result =
"All Fields Required!!";
});
}
},
child:Text('Calculate')),
SizedBox(height:11,),
Text(result, style:
TextStyle(fontSize: 19),),
],
),
),
)
);
}
}
Comments
Post a Comment