Search code examples
flutterdartflutter-web

Prevent refresh button or prompt message if click refresh (flutter web)


i know that for back button we can do by using the willpopscope, but right now i' m trying to prevent user from refreshing the page, is there a way to disable the refresh button or prompt a message if user click the refresh button?

example of willpopscope

return WillPopScope (
onWillPop: () async {
  return shouldPop;
},
child: const Text('WillPopScope sample'),
);

Solution

  • Adding this to lib/web/index.html will display a confirmation dialog:

    ...
    <body>
    <script type="text/javascript">
      window.onbeforeunload = function() {
          return "Changes that you made may not be saved.";
      }
    </script>
    ...
    </body>
    ...
    

    Like so:

    enter image description here