小编典典

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

flutter

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

  }
}

阅读 725

收藏
2020-08-13

共1个答案

小编典典

例如

    int  result = 0;

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


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