【问题标题】:Returning a value from class in flutter after Raisedbutton在 Raisedbutton 之后从类中返回一个值
【发布时间】:2019-12-06 20:01:14
【问题描述】:

我正在构建我的第一个应用程序来计算我工作所需的值。 我已经在 Python 中进行了这项工作,但这确实不同。 我有一个文本表单字段,我在其中输入了“我需要的数据”。 成功后,我从“onPress”按钮调用一个类并传递数据。 计算后,我需要将计算值显示在文本字段(spev)中。 如何将“Buisber”类中的值返回到文本字段。 我正在寻找几天,但找不到它。 我确定我在某处读过它,但只是不明白。 这是一个剥离版本:

import 'package:flutter/material.dart';
void main() {
  runApp(MaterialApp(
    home: Buis(),
  ));
}
class Buis extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String spev = '0';
    TextEditingController kapController = TextEditingController(text: '10.60');
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text("Power Outcome"),
        ),
      ),
      body: Center(
        child: Container(
          decoration: BoxDecoration(
          ),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(height: 20),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[

                    // Calculate
                    Center(
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(18.0),
                            side: BorderSide(color: Colors.red)),
                        onPressed: () {
                          Buisber(
                            kapController.text);
                        },
                        color: Colors.blue,
                        textColor: Colors.white,
                        child: Text("Calculate".toUpperCase(),
                            style: TextStyle(fontSize: 14)),
                      ),
                    ),

                    // Outcome Spev
                    Container(
                      alignment: Alignment(0, 0),
                      width: 80,
                      height: 30,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.horizontal(
                          left: Radius.circular(40),
                          right: Radius.circular(40),
                        ),
                        border: Border.all(width: 1.0),
                      ),
                      child: Text(
                        '$spev',
                        style: TextStyle(
                          fontSize: 15,
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
class Buisber {
  Buisber(kap) {
    var kap1 = double.parse(kap);
    print(kap1);
    var spev = 1.7 * (50 / kap1);
    spev = num.parse(spev.toStringAsFixed(2));
    //testing (print works)
    //how to send back Outcome Spev
    print("Spev is " '$spev');
  }
}

【问题讨论】:

    标签: class flutter return-value


    【解决方案1】:

    @Pim,为什么不创建一个函数而不是类并在 Stateful 小部件中接收值?然后你可以像这样刷新状态来更新值,

    void main() {
      runApp(new Buis());
    }
    
    class Buis extends StatefulWidget {
      @override
      BuisState createState() => BuisState();
    }
    
    class BuisState extends State<Buis> {
      var value = '';
    
      @override
      Widget build(BuildContext context) {
        String spev = '0';
        TextEditingController kapController = TextEditingController(text: '10.60');
        return MaterialApp(home: Scaffold(
          appBar: AppBar(
            title: Center(
              child: Text("Power Outcome"),
            ),
          ),
          body: Center(
            child: Container(
              decoration: BoxDecoration(
              ),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(height: 20),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
    
                        // Calculate
                        Center(
                          child: RaisedButton(
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(18.0),
                                side: BorderSide(color: Colors.red)),
                            onPressed: () {
                              value = getValue(kapController.text).toString();
                              setState(() {});
                            },
                            color: Colors.blue,
                            textColor: Colors.white,
                            child: Text("Calculate".toUpperCase(),
                                style: TextStyle(fontSize: 14)),
                          ),
                        ),
    
                        // Outcome Spev
                        Container(
                          alignment: Alignment(0, 0),
                          width: 80,
                          height: 30,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.horizontal(
                              left: Radius.circular(40),
                              right: Radius.circular(40),
                            ),
                            border: Border.all(width: 1.0),
                          ),
                          child: Text(
                            value.toString(),
                            style: TextStyle(
                              fontSize: 15,
                            ),
                          ),
                        ),
                        SizedBox(height: 10),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        )
        );
      }
    }
    
    // This can be within your BuisState
    getValue (kap) {
        var kap1 = double.parse(kap);
        print(kap1);
        var spev = 1.7 * (50 / kap1);
        spev = num.parse(spev.toStringAsFixed(2));
        //testing (print works)
        //how to send back Outcome Spev
        print("Spev is " '$spev');
        return spev;
    }
    

    希望这会有所帮助。

    【讨论】:

    • 看起来很棒,谢谢。我将实施并使用它。只是缺少经验。我需要传递 3 个值,然后在程序的不同部分传递大约 10 个或更多,知道该怎么做吗?或者只是重复这些行
    • 太好了,请将其标记为答案以帮助其他有类似问题的人。关于传递更多值,有不同的方法可以实现。您可以创建三个不同的文本字段来传递值并使用不同的参数调用相同的 getValue 函数。您还可以创建 utils.dart 并将该 getValue 函数移动到 utils 以在其他类中使用它。
    • 非常感谢。这真的很有帮助。我会将其标记为答案。
    • 我对同样的问题还有另一个问题。在 getValue 函数中,我有 2 个或更多计算结果。如何返回这 2 个或更多值?其余的工作完美。
    • 您可以改为在getValue 中返回一个双精度值列表。您可以先将列表实例化为List&lt;double&gt; valueList = new List&lt;double&gt;(),然后您可以将值添加为valueList.add(value)
    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多