Home > Mobile >  How to calculate with TextEditingControllers in flutter?
How to calculate with TextEditingControllers in flutter?

Time:12-10

So in flutter I have auto-calculated text fields and I want to calculate a result using a formula.

Let me first show what I am doing:

enter image description here

These are those text fields.

The code for these fields:

enum UnitType {
  meter,
  centimeter,
  feet,
  kilogram,
  gram,
}

class Units extends StatefulWidget {
  const Units({Key? key}) : super(key: key);

  @override
  State<Units> createState() => _UnitsState();
}

class _UnitsState extends State<Units> {
  final TextEditingController _feetController = TextEditingController();

  final TextEditingController _centimeterController = TextEditingController();

  final TextEditingController _meterController = TextEditingController();

  final TextEditingController _kilogramController = TextEditingController();

  final TextEditingController _gramController = TextEditingController();

  void _convertUnit(UnitType type) {
    double? ft = double.tryParse(_feetController.value.text) ?? 0;
    double? cm = double.tryParse(_centimeterController.value.text) ?? 0;
    double? m = double.tryParse(_meterController.value.text) ?? 0;
    double? kg = double.tryParse(_kilogramController.value.text) ?? 0;
    double? g = double.tryParse(_gramController.value.text) ?? 0;

    switch (type) {
      case UnitType.feet:
        _meterController.text = (ft / 3.281).toStringAsFixed(2);
        _centimeterController.text = (ft * 30.48).toStringAsFixed(2);
        break;
      case UnitType.meter:
        _centimeterController.text = (m * 100).toStringAsFixed(2);
        _feetController.text = (m * 3.281).toStringAsFixed(2);
        break;
      case UnitType.centimeter:
        _meterController.text = (cm / 100).toStringAsFixed(2);
        _feetController.text = (cm / 30.48).toStringAsFixed(2);
        break;
      case UnitType.kilogram:
        _gramController.text = (kg * 1000).toStringAsFixed(2);
        break;
      case UnitType.gram:
        _kilogramController.text = (g / 1000).toStringAsFixed(2);
        break;
    }
  }

The formula of BMI(body mass index) is Kg/m^2

enter image description here

Now I want when I click the BMI button I want the result to be displayed in the adjacent text field. The formula is BMI = Kilogram/(meter*meter).

I couldn't come up with any idea or code. Please help me out with some code to achieve my desired result.

CodePudding user response:

  1. You need to create one more TextEditingController, "bmiController" for example.
  2. Then create a text field with a button, and assign bmi text controller to this field.
  3. Create a method "calculate" and assign this method to the button.

Your calculate method should look like (I DIDN'T TEST THE CODE BELOW):

void calculate() {
  try {
    final kgValue = double.tryParse(_kilogramController.text);
    final meterValue = double.tryParse(_meterController.text);
    final bmiValue = kgValue / meterValue.pow(2);
    bmiController.text = bmiValue.toString();
  } catch (e) {
    // This is an example, you need to handle possible null exception anyway
    bmiController.text = 'Error';
  }
}
  • Related