I am trying to read the stylus with a "global" listener, while still being able to interact with the rest of the UI with the finger. The event object passed to the listeners of the Listener widget actually has a property for the device kind, but I can't tell it which events to absorb and which not to. You can only specify it for every event with HitTestBehavior, but this is not what I want.
I tried a bit to reverse engineer the Listener widget, but it doesn't seem to be possible to know the pointer device kind at the point where you have to decide whether to fire a hit. And I also could not find out how to cancel an event in the handleEvent callback provided by RenderObject or something like that.
Listener(
onPointerDown: (event) {
if (!pointerKinds.contains(event.kind)) return;
// Absorb now
...
},
);
class _SomeRenderObject extends RenderProxyBox {
@override
void handleEvent(PointerEvent event, covariant HitTestEntry entry) {
if(event.kind != PointerDeviceKind.stylus) {
// Cancel event
}
}
}
Turns out, the mechanism I was searching for is built on
Listenerand is called gesture-disambiguation. Its API is exposed throughRawGestureDetectorandGestureRecognizers. These are used under the hood ofGestureDetectorfor example.Listeneritself is actually rarely used to listen to events.A
GestureRecognizerneeds to decide whether or not some user interaction fits a specific gesture and when it does fit, it can claim any pointers that it needs, so no otherGestureRecognizercan claim these pointers.There are already many implementations available in flutter, like
DragGestureRecognizerand it turns out, they already can filter for specificPointerDeviceKinds. The Constructor has asupportedDevicesproperty you can use. But for some reason, you can't use it inGestureDetectordirectly, but you have to useRawGestureDetector, where you have to construct theGestureRecognizers yourself. Here is an example:It is a bit more Boilerplate involved thou!
But I went a bit further because I didn't want the drag gesture to start, after the pointer has moved, but rather to start in the moment, the pointer touches the screen, and implemented my own
GestureRecognizer(based on what I found inDragGestureRecognizer):This is actually a really nice way of implementing gesture disambiguation! One benefit of using flutter, but some more documentation on how to write gesture recognizers would be great!