create a flutter BMI app
- User
create a flutter BMI app
- Assistant
create a flutter BMI app
Creating a Body Mass Index (BMI) calculator app using Flutter is a great way to learn Flutter development while building a useful tool. In this guide, we'll walk through the steps to create a simple BMI calculator app that allows users to input their weight and height and then calculates and displays their BMI.
Before you begin, ensure you have the following installed on your system:
Create a new Flutter project by running the following command in your terminal:
flutter create bmi_calculator
Navigate to the project directory:
cd bmi_calculator
Open the project in your preferred IDE.
We'll create a simple user interface that contains:
TextField widgets to input height and weight.RaisedButton (or ElevatedButton in newer Flutter versions) to trigger the BMI calculation.Text widget to display the result.main.dartReplace the contents of lib/main.dart with the following code:
import 'package:flutter/material.dart';
void main() => runApp(BMICalculatorApp());
class BMICalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BMI Calculator',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BMICalculatorHomePage(),
);
}
}
class BMICalculatorHomePage extends StatefulWidget {
@override
_BMICalculatorHomePageState createState() => _BMICalculatorHomePageState();
}
class _BMICalculatorHomePageState extends State<BMICalculatorHomePage> {
// Controllers to retrieve input from TextFields
TextEditingController heightController = TextEditingController();
TextEditingController weightController = TextEditingController();
String _result = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter BMI Calculator'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
// Height Input
TextField(
controller: heightController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Height in meters',
icon: Icon(Icons.height),
),
),
SizedBox(height: 16),
// Weight Input
TextField(
controller: weightController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Weight in kilograms',
icon: Icon(Icons.line_weight),
),
),
SizedBox(height: 32),
// Calculate Button
ElevatedButton(
onPressed: _calculateBMI,
child: Text('Calculate BMI'),
),
SizedBox(height: 32),
// Result Display
Text(
_result,
style: TextStyle(fontSize: 24),
),
],
),
),
);
}
// Function to calculate BMI
void _calculateBMI() {
double? height = double.tryParse(heightController.text);
double? weight = double.tryParse(weightController.text);
if (height != null && weight != null && height > 0) {
double bmi = weight / (height * height);
String bmiCategory;
if (bmi < 18.5) {
bmiCategory = 'Underweight';
} else if (bmi < 25) {
bmiCategory = 'Normal weight';
} else if (bmi < 30) {
bmiCategory = 'Overweight';
} else {
bmiCategory = 'Obesity';
}
setState(() {
_result = 'Your BMI is ${bmi.toStringAsFixed(1)} ($bmiCategory)';
});
} else {
setState(() {
_result = 'Please enter valid numbers';
});
}
}
}
The BMI is calculated using the formula:
[ \text{BMI} = \frac{\text{weight~(kg)}}{\text{height~(m)}^2} ]
In the _calculateBMI function, we:
_result variable to display the BMI and category.The entire code is provided above in the main.dart file. Ensure that you have only this code in your main.dart to avoid any conflicts.
BMICalculatorApp: The root StatelessWidget that sets up the MaterialApp and theme.BMICalculatorHomePage: A StatefulWidget that holds the state of our application.TextEditingController instances are used to retrieve text input from the TextField widgets._calculateBMI Function: Parses the inputs, calculates BMI, determines the BMI category, and updates the result.TextField widgets for user input of height and weight.ElevatedButton to trigger the BMI calculation.Text widget to display the result.Here are some suggestions to improve and expand your BMI app:
By following this guide, you've created a basic Flutter BMI calculator app. This app can serve as a foundation for more complex features and is a great starting point for learning Flutter development. Happy coding!
Not yet included in a published release.