Reputation: 133
I have in my main page a Container
which use as child a widget PageView
with a callback function onBookPlace
.
Container(
child: PageView(
onBookPlace: onBookPlace(
dateStart, dateEnd
),
),
)
In my PageView
, I have the function onBookPlace
which should be called when I press onTap
class PageView extends StatelessWidget {
final VoidCallback onBookPlace;
const PageView({Key key,this.onBookPlace})
: super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap:() {onBookPlace()},
);}}
But the problem here is my function is called before I click over the GestureDetector
widget. It's called on the construction of the PageView
.
How to fix this? I mean that the callback should only be called when I click over the GestureDetector
widget.
Thank you
Upvotes: 0
Views: 110
Reputation: 471
I think you should pass a reference to your function, instead of calling it right away. So you probably should remove ()
Container(
child: PageView1(onBookPlace: onBookPlace)
)
EDIT: if you want to pass some parameters with your function then you can create another function to pass as your callback. For example you can use anonymous function.
Container(
child: PageView1(onBookPlace: () => onBookPlace(dateStart, dateEnd))
)
The basic idea is that when you want to pass a callback function you want to pass the function itself, not its result. By putting parentheses there you call the function and pass its result, instead of passing a reference to your handler function.
Upvotes: 2