Handling Flutter Webview Back-Button
Join the DZone community and get the full member experience.Join For Free
In my previous article, I introduced how to implement Flutter Webview URL Listeners to your flutter applications.
Now, we need to add functionality to navigate through the website with the back button. Currently, instead of going back, our application gets closed. To prevent this, we need to add an operation check to see whether the webview can go back, and if it can, then, we need to load the webview of previous pages.
To get a callback when we press the back button, we need to wrap our view inside
WillPopScope and create a method inside
_WebViewWebPageState to check if webview can go back. If it can, then we perform the back operation. Otherwise, we'll show exit dialog.
You may also like: Core Dart
Create one webview variable inside
_WebViewWebPageState and assign its value inside the
Now, run the application, and it will be responsive to the back button operation. The final code will look like the following screenshot. ( File: ProjectRoot/webviewapp/lib/SecondPage.dart )
Opinions expressed by DZone contributors are their own.