Flutter: Can I pass arguments to a function defined in an onPress event on a button?

后端 未结 5 791
北荒
北荒 2021-02-18 20:04

I have a simple form with a button to calculate the form. I figure it\'s better to hit the button to start the action of calculating and pass the variables to the dumb function

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 20:54

    Do you remember the very first line in flutter which is

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

    Where => represents one line functions or methods which is key while calling functions with arguments.

    Ex:

    void display() {
    
    }
    

    we can call the above function by

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

    here we can not include () with display

    But, when there is one or more than one arguments to be passed then it goes like below

    void addition(int value1, int value2) {
        value1 + value2
    }
    
    IconButton(icon: Icon(Icons.add), onPressed: ()=>addition(2, 4))
    

提交回复
热议问题