fluttercontextmenudisable

How to disable the context menu on Flutter web? (right click, touch press)


How to disable the context menu in Flutter (2.x, Web / Browser) when e.g. right click or in touch long press on mobile devices (e.g. DevTools mobile view).

I come from web development with Angular etc. In HTML/JS it works like:

document.body.addEventListener('contextmenu', (event) => {
  event.preventDefault();
});

But how to do this on Flutter? Is there contextmenu event which can be disabled. Blocking right click will not work. Because it also appears on mobile long press (on release).


Solution

  • Currently there is no official solution. Here is the related GitHub issue: flutter#78671.

    Workaround: Just open the dev tools of the browser and execute the following code in the console:

    document.body.addEventListener('contextmenu', (event) => {
      event.preventDefault();
    });
    

    The hot reload from Flutter does not reload the page. So it will work for the whole session.