Reputation: 43
I'm learning Flutter right now, trying to code a simple calculator. I am building rows using this code:
Row( // creates row
mainAxisAlignment: MainAxisAlignment.spaceEvenly, //row is spaced evenly
children: <Widget>[
_button("4", _number("4")), //calls _button widget passing String 4 and function _number, which passes string 4 also
_button("5", _number("5")), //button with value 5
_button("6", _number("6")), //button with value 6
_button("-", _min("null")) //button for subtraction
],
),
my _button widget looks like this:
Widget _button(String number, Function() f(String number)){ //parameters: the button value as String and the function with the value as String
return MaterialButton(
height: buttonHeight,
minWidth: buttonHeight,
child: Text(number,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 48.0)),
textColor: Colors.black,
color: Colors.grey[100],
onPressed: f, //function is called
);
}
Now I want to pass the String number to the Function f, so when the function _number is called, it takes the String number and pastes it onto the display:
void _number(String number){
setState(() {
display=display + number ;
});
}
It does not work, I tried to solve it, but was not successful. Does anyone have an idea?
Thanks!
Upvotes: 1
Views: 7986
Reputation: 2556
you have to change this:
Widget _button(String number, Function() f(String number)){ //parameters: the button value as String and the function with the value as String
return MaterialButton(
height: buttonHeight,
minWidth: buttonHeight,
child: Text(number,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 48.0)),
textColor: Colors.black,
color: Colors.grey[100],
onPressed: f, //function is called
);
for this:
Widget _button(String number, Function(String number) f){ //parameters: the button value as String and the function with the value as String
return MaterialButton(
height: buttonHeight,
minWidth: buttonHeight,
child: Text(number,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 48.0)),
textColor: Colors.black,
color: Colors.grey[100],
onPressed: () {
f(number); // function is called
},
);
The main change is that the parameters go inside Function(String param1, String param2) nameFunction
which in your case would be Function(String number) f
Upvotes: 3