RayCC
Cover Image

Flutter study - BMI Calculator

Flutter Study Coding Sample

Source code reference: https://www.geeksforgeeks.org/how-to-make-simple-bmi-calculator-app-in-flutter/

I made some modifications here.

lib/main.dart

 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
import 'package:flutter/material.dart';

import 'bmi_calculator.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorSchemeSeed: Color(0xffaa22aa),
        useMaterial3: true,
        brightness: Brightness.light,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: BmiCalculator(),
      ),
    );
  }
}

lib/bmi_calculator.dart

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import 'package:flutter/material.dart';

class BmiCalculator extends StatefulWidget {
  const BmiCalculator({super.key});

  @override
  State<BmiCalculator> createState() => _BmiCalculatorState();
}

class _BmiCalculatorState extends State<BmiCalculator> {
  String bmi = "";
  Color colorBmi = Colors.transparent;

  TextEditingController heightController = TextEditingController();
  TextEditingController weightController = TextEditingController();

  void _updateBMI() {
    if (heightController.text.isNotEmpty && weightController.text.isNotEmpty) {
      setState(() {
        double weight = double.parse(weightController.text);
        double height = double.parse(heightController.text) / 100; // cm -> m

        bmi = (weight / (height * height)).toStringAsFixed(2);
        colorBmi = _bmiColor();
      });
    } else {
      // Clear bmi field
      setState(() {
        bmi = "";
        colorBmi = Colors.transparent;
      });
    }
  }

  Color _bmiColor() {
    double doubleBmi = double.parse(bmi);
    if (doubleBmi < 18.5) {
      return const Color(0xFF87B1D9); // Underweight
    } else if (doubleBmi < 24.9) {
      return const Color(0xFF3DD365); // Normal
    } else if (doubleBmi < 29.9) {
      return const Color(0xFFEEE133); // Overweight
    } else if (doubleBmi < 34.9) {
      return const Color(0xFFFD802E); // Obese
    }
    return const Color(0xFFF95353); // Extreme
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(colors: [
              Color(0xFF87B1D9),
              Color(0xFF3DD365),
              Color(0xFFEEE133),
              Color(0xFFFD802E),
              Color(0xFFF95353)
            ]),
          ),
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Text(
              "BMI Calculator",
              style: TextStyle(
                fontSize: 40,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: TextField(
            controller: heightController,
            keyboardType: TextInputType.numberWithOptions(decimal: true),
            style: TextStyle(
              fontSize: 30,
            ),
            decoration: InputDecoration(
              labelText: "Height(cm): ",
              filled: true,
              fillColor: Colors.white,
            ),
            onChanged: (value) => _updateBMI(),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: TextField(
            controller: weightController,
            keyboardType: TextInputType.numberWithOptions(decimal: true),
            style: TextStyle(
              fontSize: 30,
            ),
            decoration: InputDecoration(
              labelText: "Weight(kg): ",
              filled: true,
              fillColor: Colors.white,
            ),
            onChanged: (value) => _updateBMI(),
          ),
        ),
        Text(
          "Your BMI: $bmi",
          style: TextStyle(
            backgroundColor: colorBmi,
            fontSize: 30,
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _colorContainer(color: 0xFF87B1D9, text: "Underweight"),
            _colorContainer(color: 0xFF3DD365, text: "Normal"),
            _colorContainer(color: 0xFFEEE133, text: "Overweight"),
            _colorContainer(color: 0xFFFD802E, text: "Obese"),
            _colorContainer(color: 0xFFF95353, text: "Extreme"),
          ],
        ),
        SizedBox(
          height: 40,
        )
      ],
    );
  }

  Widget _colorContainer({required int color, required String text}) {
    return Container(
      width: MediaQuery.of(context).size.width / 6,
      height: MediaQuery.of(context).size.width / 6,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        color: Color(color),
      ),
      child: Center(
        child: Text(
          text,
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}