Skip to content
New issue

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

bmi calculater in dart language #66

Open
hiteshkumar2004 opened this issue Oct 24, 2023 · 0 comments
Open

bmi calculater in dart language #66

hiteshkumar2004 opened this issue Oct 24, 2023 · 0 comments
Assignees
Labels
hacktoberfest hacktoberfest-2023

Comments

@hiteshkumar2004
Copy link

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';
});

}
}

@roshanlimbu roshanlimbu added hacktoberfest hacktoberfest-2023 hacktoberfest-accepted pr-accepted for hacktoberfest and removed hacktoberfest-accepted pr-accepted for hacktoberfest labels Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest hacktoberfest-2023
Projects
None yet
Development

No branches or pull requests

2 participants