There will be a physical return button on Android phones, but iOS phones do not have it, so this is a unique feature of Android phones.
Usage scenario: When the user clicks the return key twice in a certain period of time, it will be considered to be quitting the application.
If you want to implement this function in Flutter, we first understand a component in Flutter WillPopScope. In Flutter, we use this component to implement physical return key interception, so as to click the two return keys to exit the application.
onWillPop is one of its callback functions. When the user clicks the return button, it is called (Android physical return button), and the callback needs to return aFuture
Object, if returnedFuture
The final value isfalse
When the current route does not go out of the stack (it will not return); the final value istrue
When the current route exits the stack. We need to provide this callback to decide whether to exit.
Components
DateTime? lastPopTime;
onWillPop: () async { if (lastPopTime == null || ().difference(lastPopTime!) > Duration(seconds: 1)) { lastPopTime = (); ("Press again to exit"); return (false); } else { lastPopTime = (); // Exit the app return (true); } });
Components
DateTime? lastPopTime;
onWillPop: () async { // webViewController?.goBack(); // return (false); if (lastPopTime == null || ().difference(lastPopTime!) > Duration(seconds: 1)) { lastPopTime = (); if (await webViewController?.canGoBack() == true) { await webViewController?.goBack(); } else { ("Press again to exit"); } return (false); } else { lastPopTime = (); // Exit the app return (true); // await (''); } });
This is the end of this article about the implementation example of Flutter click twice to exit the app. For more related Flutter click twice to exit the app, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!