【问题标题】:Flutter: Can I pass arguments to a function defined in an onPress event on a button?Flutter:我可以将参数传递给按钮上的 onPress 事件中定义的函数吗?
【发布时间】:2018-10-29 12:51:00
【问题描述】:

我有一个简单的表格,带有一个按钮来计算表格。我认为最好点击按钮开始计算操作并将变量传递给哑函数,而不是让函数知道它不需要知道的文本字段。我可以这样做还是我的计算函数需要访问我的 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) {

  }
}

【问题讨论】:

  • 请出示您的代码
  • 代码已添加到帖子中。

标签: dart flutter


【解决方案1】:

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

所以你有两种方法可供选择:

使用

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

或

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

【讨论】:

  • 第二种方法对我有用,我需要通过 BuildContext :)
【解决方案2】:

你还记得flutter的第一行吗 这是

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

其中=&gt;代表单行函数或方法 这是使用参数调用函数时的关键。

例如:

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

【讨论】:

    【解决方案3】:

    刚刚找到答案。不。 根据RaisedButton类的onPressed property, onPressed 属性的类型为 VoidCallback,它返回 void,根据文档,VoidCallBack 是 没有参数且不返回数据的回调的签名。

    编辑:谢谢大家。为了解决这个问题,你可以使用匿名函数,就像这样

    (){your code here}

    【讨论】:

    • 那你是怎么解决的呢? onPressed: () =&gt; calculate(1, 2),?
    • 是的,这就是解决方案。我刚回来更新它,但你们打败了我。
    【解决方案4】:

    例如

        int  result = 0;
    
        void calculate(num1, num2) {
         setState(() {
             result = num1 + num2;
         });
        }
    
    
        new RaisedButton(
              onPressed: () => calculate(1, 100),
              ...
        ),
        new Text("$result")
    

    【讨论】:

    • 所以实际上我们并没有将函数本身(回想一下 Dart 中的一切都是对象,甚至是函数)传递给onPressed 属性,我们创建了一个匿名函数来调用我们的函数。同onPressed: () { calculate(1,100);}
    • 这是唯一的方法吗?或者还有其他方法吗?
    【解决方案5】:

    使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2013-03-08
      • 2011-02-20
      • 1970-01-01
      相关资源
      最近更新 更多