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

时间:2018-05-19 05:39:22

标签: 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) {

  }
}

5 个答案:

答案 0 :(得分:13)

e.g。

    int  result = 0;

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


    new RaisedButton(
          onPressed: () => calculate(1, 100),
          ...
    ),
    new Text("$result")

答案 1 :(得分:4)

您还记得拍打的第一行吗

void main() => runApp(MyApp());

其中=>代表一行功能方法 用参数调用函数时,这是关键。

例如:

void display() {

}

我们可以通过以下方式调用上述功能

IconButton(icon: Icon(Icons.add), onPressed: display)

在这里我们不能在()中包含display

但是,当要传递一个或多个参数时,它将像下面这样

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

IconButton(icon: Icon(Icons.add), onPressed: ()=>addition(2, 4))

答案 2 :(得分:1)

刚刚找到答案。没有。 根据{{​​3}}类的onPressed property, onPressed属性的类型为RaisedButton,返回void,根据文档,VoidCallBack是没有参数的回调签名,不返回任何数据。

编辑:谢谢你们。要解决此问题,请使用匿名函数,如此

(){your code here}

答案 3 :(得分:1)

使用

onPressed: (){calculate(1, 2);},

答案 4 :(得分:1)

速记函数的设计方式允许返回本文中提到的void类型的函数-> So why is returning everything allowed through a shorthand function[()=>] even if the return type is void?

因此,您有两种处理方法:

使用

    date    shop_id revenue
0   2013-01-02  59  22131846.0
1   2013-01-23  24  22131846.0
2   2013-01-20  27  22131846.0
3   2013-01-02  25  22131846.0
4   2013-01-03  25  22131846.0
5   2013-01-20  25  22131846.0
6   2013-01-23  25  22131846.0
7   2013-01-26  25  22131846.0
8   2013-01-27  6   22131846.0
9   2013-01-10  15  22131846.0
10  2013-01-10  7   22131846.0
11  2013-01-05  31  22131846.0
12  2013-01-02  54  22131846.0
13  2013-01-14  42  22131846.0
14  2013-01-26  38  22131846.0
15  2013-01-20  37  22131846.0
16  2013-01-02  46  22131846.0
17  2013-01-08  44  22131846.0
18  2013-02-05  51  14555178.0
19  2013-02-12  54  22131846.0
20  2013-02-12  16  22131846.0
21  2013-02-22  1   14555178.0
22  2013-02-10  0   14555178.0
23  2013-02-05  0   14555178.0
24  2013-03-21  28  22131846.0

onPressed: () => calculate(1, 2),