颤振,从函数读取多个值

时间:2019-12-09 17:14:14

标签: list function flutter

这是几天前我对第一个Flutter应用提出的一个问题的跟进。 遵循PRO的建议,我更改了代码,以实现我想要的功能。 唯一的问题是,从函数中我只能得到1个值。 Pro建议您列出一个清单,该清单可以在应有的功能下工作。 我不清楚如何在文本字段中获取这些值。 我只能显示想要发送完整代码的内容。 稍后,我想基于具有更多结果值的同一主体。 我使用value.toString()以及value [1] .toString()。 第一个给我第一个值,第二个给我一个角色。 希望有人能帮忙

import 'package:flutter/material.dart';

void main() {
  runApp(Buis());
}

class Buis extends StatefulWidget {
  @override
  BuisState createState() => BuisState();
}

class BuisState extends State<Buis> {
  var kap2 = '';
  var buizen2 = '';
  var diam2 = '';
  var spev = '';
  var spo = '';
  var value;

  @override
  Widget build(BuildContext context) {

    TextEditingController kapController = TextEditingController(text: '9.60');
    TextEditingController buizenController = TextEditingController(text: '12');
    TextEditingController diamController = TextEditingController(text: '51');

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text("Specifiek buis vermogen"),
          ),
        ),
        body: Center(
          child: Container(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SizedBox(height: 20),

                  // Input kap breedte
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: kapController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                            EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Kap breedte (mtr)",
                        hintText: "$kap2",
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  // Input aantal buizen
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: buizenController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                            EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Aantal buizen",
                        hintText: "$buizen2",
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  // Input buis diameter
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: diamController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                            EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Buis diameter (mm)",
                        hintText: '$diam2',
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  //  Uitkomsten
                  Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[

                      SizedBox(height: 20),

                      // Berekenen knop
                      Center(
                        child: RaisedButton(
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(18.0),
                              side: BorderSide(color: Colors.red)),
                          onPressed: () {
                            value = getValue(kapController.text, buizenController.text, diamController.text).toString();
                            setState(() {});
                          },
                          color: Colors.blue,
                          textColor: Colors.white,
                          child: Text("Berekenen".toUpperCase(),
                              style: TextStyle(fontSize: 14)),
                        ),
                      ),
                      SizedBox(height: 20),

                      Text(
                        'Specifiek vermogen     ',
                        style: TextStyle(
                            backgroundColor: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),

                      // Specifiek vermogen
                      Container(
                        alignment: Alignment(0, 0),
                        width: 80,
                        height: 30,
                        margin: const EdgeInsets.only(right: 5.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                          color: Colors.red[200],
                          borderRadius: BorderRadius.horizontal(
                            left: Radius.circular(40),
                            right: Radius.circular(40),
                          ),
                          border: Border.all(width: 1.0),
                        ),

                        child:Text(
                          value[2].toString(), //here I read the first outcome
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      SizedBox(height: 10),

                      Text(
                        'Specifiek oppervlak       ',
                        style: TextStyle(
                            backgroundColor: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),

                      // Specifiek oppervlak
                      Container(
                        alignment: Alignment(0, 0),
                        width: 80,
                        height: 30,
                        margin: const EdgeInsets.only(right: 5.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                          color: Colors.red[200],
                          borderRadius: BorderRadius.horizontal(
                            left: Radius.circular(40),
                            right: Radius.circular(40),
                          ),
                          border: Border.all(width: 1.0),
                        ),
                        child: Text(
                          value[3].toString(), // and here the second
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      SizedBox(height: 20),

                      SizedBox(height: 60),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

getValue(kap, buizen, diam) {
  var kap1 = double.parse(kap);
  var buizen1 = double.parse(buizen);
  var diam1 = double.parse(diam);

  var spev = 1.7 * (diam1 / 51) * (buizen1 / kap1);
  spev = num.parse(spev.toStringAsFixed(2));
  var spo = (buizen1 * diam1 * 0.1) / kap1;
  spo = num.parse(spo.toStringAsFixed(2));
  print("Spev is " '$spev');
  print("Spo is " '$spo');

  List<double> valueList = List<double>();
  valueList.add(spev);
  valueList.add(spo);

  print(valueList);
  return valueList;

}

2 个答案:

答案 0 :(得分:1)

@Pim,您的代码中几乎没有错误

  • 您正在显式调用小部件构建中的列表项,此处您还没有值
  • 您正在使用value = getValue(kapController.text, buizenController.text, diamController.text).toString();将列表转换为字符串,不确定为什么要这样做,但这不正确。

尝试在小部件构建中为textFields分配空值,然后在按下按钮时分配实际值,最后像这样刷新状态,

class Buis extends StatefulWidget {
  @override
  BuisState createState() => BuisState();
}

class BuisState extends State<Buis> {
  var kap2 = '';
  var buizen2 = '';
  var diam2 = '';
  var spev = '';
  var spo = '';
  var value;
  var firstValue = '';
  var secondValue = '';
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    TextEditingController kapController = TextEditingController();
    TextEditingController buizenController = TextEditingController();
    TextEditingController diamController = TextEditingController();

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text("Specifiek buis vermogen"),
          ),
        ),
        body: Center(
          child: Container(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SizedBox(height: 20),

                  // Input kap breedte
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: kapController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                        EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Kap breedte (mtr)",
                        hintText: "$kap2",
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  // Input aantal buizen
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: buizenController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                        EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Aantal buizen",
                        hintText: "$buizen2",
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  // Input buis diameter
                  Container(
                    width: 170,
                    height: 30,
                    child: TextFormField(
                      controller: diamController,
                      keyboardType: TextInputType.number,
                      style: TextStyle(fontWeight: FontWeight.bold),
                      decoration: InputDecoration(
                        contentPadding:
                        EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                        labelText: "Buis diameter (mm)",
                        hintText: '$diam2',
                        fillColor: Colors.cyan[200],
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(40.0),
                          borderSide: BorderSide(),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 10),

                  //  Uitkomsten
                  Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[

                      SizedBox(height: 20),

                      // Berekenen knop
                      Center(
                        child: RaisedButton(
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(18.0),
                              side: BorderSide(color: Colors.red)),
                          onPressed: () {
                            value = getValue(kapController.text, buizenController.text, diamController.text);
                            //Depending on how many value you will have;
                            firstValue = value[0].toString();
                            secondValue = value[1].toString();
                            setState(() {});
                          },
                          color: Colors.blue,
                          textColor: Colors.white,
                          child: Text("Berekenen".toUpperCase(),
                              style: TextStyle(fontSize: 14)),
                        ),
                      ),
                      SizedBox(height: 20),

                      Text(
                        'Specifiek vermogen     ',
                        style: TextStyle(
                            backgroundColor: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),

                      // Specifiek vermogen
                      Container(
                        alignment: Alignment(0, 0),
                        width: 80,
                        height: 30,
                        margin: const EdgeInsets.only(right: 5.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                          color: Colors.red[200],
                          borderRadius: BorderRadius.horizontal(
                            left: Radius.circular(40),
                            right: Radius.circular(40),
                          ),
                          border: Border.all(width: 1.0),
                        ),

                        child:Text(
                          firstValue.toString(), //here I read the first outcome
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      SizedBox(height: 10),

                      Text(
                        'Specifiek oppervlak       ',
                        style: TextStyle(
                            backgroundColor: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),

                      // Specifiek oppervlak
                      Container(
                        alignment: Alignment(0, 0),
                        width: 80,
                        height: 30,
                        margin: const EdgeInsets.only(right: 5.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                          color: Colors.red[200],
                          borderRadius: BorderRadius.horizontal(
                            left: Radius.circular(40),
                            right: Radius.circular(40),
                          ),
                          border: Border.all(width: 1.0),
                        ),
                        child: Text(
                          secondValue.toString(), // and here the second
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      SizedBox(height: 20),

                      SizedBox(height: 60),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

getValue(kap, buizen, diam) {
  var kap1 = double.parse(kap);
  var buizen1 = double.parse(buizen);
  var diam1 = double.parse(diam);

  var spev = 1.7 * (diam1 / 51) * (buizen1 / kap1);
  spev = num.parse(spev.toStringAsFixed(2));
  var spo = (buizen1 * diam1 * 0.1) / kap1;
  spo = num.parse(spo.toStringAsFixed(2));
  print("Spev is " '$spev');
  print("Spo is " '$spo');

  List<double> valueList = List<double>();
  valueList.add(spev);
  valueList.add(spo);

  print(valueList);
  return valueList;

}

demo

答案 1 :(得分:0)

当您将字符串称为string[1]时,是在要求字符串中的第二个字符。

设置value时,会将其设置为字符串。尝试从.toString()的末尾删除value = getValue(kapController.text, buizenController.text, diamController.text).toString();,这样value将成为一个列表,您可以获得该列表的第n个元素。

我还建议您在首次声明value时设置其类型。代替var value;使用List<double> value;