Flutter:我可以将参数传递给按钮上 onPress 事件中定义的函数吗?

2024-01-14

我有一个简单的表单,带有一个用于计算表单的按钮。我认为最好点击按钮开始计算操作并将变量传递给哑函数,而不是让函数知道它不需要知道的文本字段。我可以这样做吗?或者我的计算函数是否需要访问我的文本字段?

         new Container(
            color: Colors.grey.shade300,
            padding: new EdgeInsets.all(15.0),
            child: new Column(
              children: <Widget>[
                new TextField(
                  controller: _ageController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.person_outline), labelText: "Age"),
                ),
                new TextField(
                  controller: _heightController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.insert_chart),
                      labelText: "Height (in feet)"),
                ),
                new TextField(
                  controller: _weightController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.line_weight),
                      labelText: "Weight (in lbs)"),
                ),
                new Padding(padding: new EdgeInsets.only(top: 20.0)),
                new RaisedButton(
                  onPressed: calculate(1, 2),
                  color: Colors.pinkAccent,
                  child: new Text(
                    "Calculate",
                    style: new TextStyle(color: Colors.white),
                  ),
                )
              ],
            ),
          ),

        ],
      ),
    );
  }

  void calculate(num1, num2) {

  }
}

e.g.

    int  result = 0;

    void calculate(num1, num2) {
     setState(() {
         result = num1 + num2;
     });
    }


    new RaisedButton(
          onPressed: () => calculate(1, 100),
          ...
    ),
    new Text("$result")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter:我可以将参数传递给按钮上 onPress 事件中定义的函数吗? 的相关文章

随机推荐