We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import 'package:flutter/material.dart';
void main() { runApp(BMICalculator()); }
class BMICalculator extends StatefulWidget { @OverRide _BMICalculatorState createState() => _BMICalculatorState(); }
class _BMICalculatorState extends State { // Variables to hold user input double weight = 0.0; double height = 0.0; String selectedGender = 'Boy';
// Variable to hold the BMI result String bmiResult = '';
@OverRide Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('BMI Calculator'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // User input fields TextField( keyboardType: TextInputType.number, onChanged: (value) { setState(() { weight = double.tryParse(value) ?? 0.0; }); }, decoration: InputDecoration(labelText: 'Weight (kg)'), ), TextField( keyboardType: TextInputType.number, onChanged: (value) { setState(() { height = double.tryParse(value) ?? 0.0; }); }, decoration: InputDecoration(labelText: 'Height (cm)'), ),
// Gender selection Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Radio( value: 'Boy', groupValue: selectedGender, onChanged: (value) { setState(() { selectedGender = value!; }); }, ), Text('Boy'), Radio( value: 'Girl', groupValue: selectedGender, onChanged: (value) { setState(() { selectedGender = value!; }); }, ), Text('Girl'), ], ), ElevatedButton( onPressed: calculateBMI, child: Text('Calculate BMI'), ), // BMI result display Text( bmiResult, style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), textAlign: TextAlign.center, ), ], ), ), ), );
}
// Calculate BMI function void calculateBMI() { if (height == 0.0) { setState(() { bmiResult = 'Please enter a valid height.'; }); return; }
// Convert height from cm to meters double heightMeters = height / 100.0; // Calculate BMI differently based on gender double bmi; if (selectedGender == 'Boy') { bmi = weight / (heightMeters * heightMeters); } else { // Adjust the formula for girls (you can use different constants) bmi = weight / (heightMeters * heightMeters); } // Determine the BMI category for interpretation String bmiCategory; if (bmi < 18.5) { bmiCategory = 'Underweight'; } else if (bmi >= 18.5 && bmi < 24.9) { bmiCategory = 'Normal Weight'; } else if (bmi >= 24.9 && bmi < 29.9) { bmiCategory = 'Overweight'; } else { bmiCategory = 'Obese'; } // Set the BMI result setState(() { bmiResult = 'Your BMI: ${bmi.toStringAsFixed(2)}\nCategory: $bmiCategory'; });
} }
The text was updated successfully, but these errors were encountered:
hiteshkumar2004
No branches or pull requests
import 'package:flutter/material.dart';
void main() {
runApp(BMICalculator());
}
class BMICalculator extends StatefulWidget {
@OverRide
_BMICalculatorState createState() => _BMICalculatorState();
}
class _BMICalculatorState extends State {
// Variables to hold user input
double weight = 0.0;
double height = 0.0;
String selectedGender = 'Boy';
// Variable to hold the BMI result
String bmiResult = '';
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('BMI Calculator'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// User input fields
TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
weight = double.tryParse(value) ?? 0.0;
});
},
decoration: InputDecoration(labelText: 'Weight (kg)'),
),
TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
height = double.tryParse(value) ?? 0.0;
});
},
decoration: InputDecoration(labelText: 'Height (cm)'),
),
}
// Calculate BMI function
void calculateBMI() {
if (height == 0.0) {
setState(() {
bmiResult = 'Please enter a valid height.';
});
return;
}
}
}
The text was updated successfully, but these errors were encountered: