I'm making an Android app in Flutter and I want to add an interactive widget that listens to raw pointer events and reacts to them. This is easy to do using the Listener
widget:
class _MyWidget extends State<MyWidget> {
@override
build(BuildContext ctx) {
return Listener(
onPointerMove: (event) {
event.delta // use this to do some magic...
},
child: ...
);
}
}
However, this widget is rendered inside a PageView
, which allows the user to swipe in order to change pages. This is a behavior I don't want to get rid of – except for the time when the user swipes over MyWidget
. Since the MyWidget
requires the user to touch-and-drag, I don't want them to accidentaly nagivate to a different page.
In a browser, this would be extremely simple to implement, I'd just call event.stopPropagation()
and the event wouldn't propagate (ie. bubble) from MyWidget
to its ancestor PageView
. How do I stop propagation of an event in Flutter?
I could, in theory, make MyWidget
set the application state, and use that to disable PageView
while I'm swiping over MyWidget
. However, that would go against the natural dataflow of Flutter's widgets: it would require me to add callbacks on multiple places and make all the widgets more intertwined and less reusable. I would much prefer to prevent the event from bubbling, locally.
EDIT: I've tried using AbsorbPointer
, however it seems to be "blocking propagation" in the wrong direction. It blocks all children of AbsorbPointer
from recieving pointer events. What I want is quite the opposite – stop pointer events on a child from propagating to its ancestors.