0

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 \n\n $bmi';
        print(bmi);
      }

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

quoci
  • 2,940
  • 1
  • 9
  • 21
  • Does this answer your question? [How to reference another file in Dart?](https://stackoverflow.com/questions/12951989/how-to-reference-another-file-in-dart) – Abion47 Jul 11 '23 at 19:07

2 Answers2

0

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 \n\n $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;
  }
}
Madhan
  • 21
  • 4
  • i want to use it with NumberPicker and i can't provide a value that is not a int also i don't have void main in my dart file – Armin youuu Jul 12 '23 at 13:35
0

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 \n\n $_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 \n\n $bmi';
  return bmi;
}
Anand
  • 323
  • 1
  • 8