将计算器传递到另一个dart文件

2vuwiymt  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(92)

我有一个BMI计算器,我想在我的其他 dart 文件中使用它,这是一个结果屏幕。这是我的计算器:

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);
      }

字符串
我想在文本小部件中使用它。我该怎么做?

ecr0jaav

ecr0jaav1#

您可以使用这种方法
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;
  }
}

kkbh8khc

kkbh8khc2#

这就是你可以在Flutter中做的事情。


的数据

主省道

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(),
    );
  }
}

字符串

主页.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;
}

相关问题