skip to Main Content

I have a BMI calculator and i want to use it in my other dart file which is a result screen.
This is my calculator :

calculatePressed() {
        var tall = _currentValue1.toString();
        var weight = _currentValue2.toString();
        var cTall = int.parse(tall);
        var dWeight = int.parse(weight);
        var dTall = cTall / 100;
        var bmi = (dWeight / dTall * dTall).toInt();
        var result = 'Your Bmi is nn $bmi';
        print(bmi);
      }

I want to use it in a Text widget.
How should i do that?

2

Answers


  1. You can use this approach

    main.dart

    void main() {
    const String heightValue="195";
    const String weightValue= "80";
    final int result = Utils.calculatePressed(heightValue,weightValue);
    print('Your Bmi is nn $result');
    }
    

    utils.dart

     abstract class Utils{
        static  int  calculatePressed( String x, String y) {
       
        int cTall = int.parse(x);
        int dWeight = int.parse(y);
        double dTall = cTall / 100;
        int bmi = (dWeight / dTall * dTall).toInt();
      
        return bmi;
      }
    }
    
    Login or Signup to reply.
  2. This is what you could do in Flutter.

    Output

    main.dart

    import 'package:flutter/material.dart';
    
    import 'home_page.dart';
    
    void main() {
      runApp(const MainApp());
    }
    
    class MainApp extends StatelessWidget {
      const MainApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: HomePage(),
        );
      }
    }
    

    home_page.dart

    import 'package:flutter/material.dart';
    
    import 'util.dart';
    
    class HomePage extends StatefulWidget {
      const HomePage({super.key});
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      final TextEditingController _heightController = TextEditingController();
      final TextEditingController _weightController = TextEditingController();
    
      int? _bmi;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 20,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                TextFormField(
                  controller: _heightController,
                  decoration:
                      const InputDecoration(helperText: "Enter Height in cm"),
                ),
                TextFormField(
                  controller: _weightController,
                  decoration:
                      const InputDecoration(helperText: "Enter Weight in KG"),
                ),
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      _bmi = calculatePressed(
                          _heightController.text, _weightController.text);
                    });
                  },
                  child: const Text("Calculate BMI"),
                ),
                (_bmi != null)
                    ? Text(
                        "Your Bmi is nn $_bmi",
                      )
                    : const SizedBox(
                        height: 0,
                      ),
              ],
            ),
          ),
        );
      }
    }
    

    utils.dart

    calculatePressed(String height, String wt) {
      var tall = height.toString();
      var weight = wt.toString();
      var cTall = int.parse(tall);
      var dWeight = int.parse(weight);
      var dTall = cTall / 100;
      var bmi = (dWeight / dTall * dTall).toInt();
      // var result = 'Your Bmi is nn $bmi';
      return bmi;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search