touchEventToMouseEvent function
Converts a TouchEvent event
to a MouseEvent.
This helps to use TouchEvent as normal MouseEvent, simplifying UI support for touch events and mouse events.
Implementation
MouseEvent? touchEventToMouseEvent(TouchEvent event) {
var touches = event.touches;
if (touches == null || touches.isEmpty) return null;
var first = touches[0];
var type = '';
switch (event.type.toLowerCase()) {
case 'touchstart':
type = 'mousedown';
break;
case 'touchmove':
type = 'mousemove';
break;
case 'touchend':
type = 'mouseup';
break;
default:
return null;
}
EventTarget? target;
// If `event.target` is null, not dispatched, it will throw an exception.
try {
target = event.target;
}
// ignore: empty_catches
catch (ignore) {}
var simulatedEvent = MouseEvent(type,
canBubble: event.bubbles!,
cancelable: event.cancelable!,
view: window,
detail: 1,
screenX: first.screen.x as int,
screenY: first.screen.y as int,
clientX: first.client.x as int,
clientY: first.client.y as int,
ctrlKey: event.ctrlKey!,
altKey: event.altKey!,
shiftKey: event.shiftKey!,
metaKey: event.metaKey!,
button: 0,
relatedTarget: target);
return simulatedEvent;
}