Reputation: 2420
I want to pass a function as a parameter of a widget which contains a button (the function passed will be executed on the onPressed
of the button)
myFunction1(){
// do some things
}
myWidget(Function onPress){
return FlatButton(
onPressed : onPress,
);
}
and then calling myWidget(myFunction1)
. But it doesn't work and I believe it's normal. The function is right executed and try to give a result. But I don't want the function to be executed now for it's result, I kind of want to pass the 'reference" of the function.
Upvotes: 0
Views: 307
Reputation: 824
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: myWidget(myFunction1),
),
);
}
myFunction1() {
print('called');
}
myWidget(Function onPressed) {
return FlatButton(
child: Text('FlatButton'),
onPressed: onPressed,
);
}
Upvotes: 1
Reputation: 1580
Widget myWidget(Function onPress){
return FlatButton(
onPressed : onPress,
child: Text('Button'),
);
}
myWidget((){
//your code...
});
Upvotes: 0