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

Thi*_*tal 9 dart flutter

我有一个简单的表单,带有一个按钮来计算表格.我认为最好按下按钮开始计算动作并将变量传递给哑函数,而不是让函数知道它不应该知道的文本字段.我可以这样做,还是我的计算功能需要访问我的TextFields?

         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) {

  }
}
Run Code Online (Sandbox Code Playgroud)

kev*_*ink 24

例如

    int  result = 0;

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


    new RaisedButton(
          onPressed: () => calculate(1, 100),
          ...
    ),
    new Text("$result")
Run Code Online (Sandbox Code Playgroud)

  • 所以实际上我们并没有将函数本身(回想一下 Dart 中的一切都是对象,甚至是函数)传递给 `onPressed` 属性,我们创建了一个调用我们的函数的匿名函数。与`onPressed: () {calculate(1,100);}`相同 (7认同)

小智 10

你还记得 flutter 的第一行是

void main() => runApp(MyApp());
Run Code Online (Sandbox Code Playgroud)

其中=>代表一行函数方法 ,这在调用带参数的函数时很关键。

前任:

void display() {

}
Run Code Online (Sandbox Code Playgroud)

我们可以通过调用上面的函数

IconButton(icon: Icon(Icons.add), onPressed: display)
Run Code Online (Sandbox Code Playgroud)

这里我们可以不包括()display

但是,当要传递一个或多个参数时,它会如下所示

void addition(int value1, int value2) {
    value1 + value2
}

IconButton(icon: Icon(Icons.add), onPressed: ()=>addition(2, 4))
Run Code Online (Sandbox Code Playgroud)


SAn*_*riy 7

简写函数的设计方式允许返回类型为 void 的函数,如本文所述 -->那么,为什么即使返回类型为 void,也可以通过简写函数 [()=>] 返回所有允许的内容?

这样你就有两种方法可供选择:

使用

onPressed: () => calculate(1, 2),
Run Code Online (Sandbox Code Playgroud)

或者

onPressed: (){calculate(1, 2);},
Run Code Online (Sandbox Code Playgroud)


Thi*_*tal 6

刚刚找到答案。号根据该onPressed属性的的RaisedButton类中,onPressed属性有一个类型的VoidCallback,它返回void并根据该文档VoidCallBack是 不具有参数和返回任何数据回调的签名。

编辑:谢谢各位。为了解决这个问题,你使用一个匿名函数,就像这样

(){your code here}