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

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

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

}

}

回答:

例如

    int  result = 0;

void calculate(num1, num2) {

setState(() {

result = num1 + num2;

});

}

new RaisedButton(

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

...

),

new Text("$result")

以上是 Flutter:我可以将参数传递给按钮上的onPress事件中定义的函数吗? 的全部内容, 来源链接: utcz.com/qa/425839.html

回到顶部