Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to exit app with two clicks in Flutter

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "Flutter how to click twice to exit app", the content is detailed, the steps are clear, and the details are handled properly. I hope this "Flutter how to click twice to quit app" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

There is a physical return key on Android phones, but not on ios phones, 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 exit the application.

In order to achieve this function in Flutter, we first know a component WillPopScope in Flutter. In Flutter, we use this component to intercept the physical return key, so as to click two return keys to exit the application.

OnWillPop is one of his callback functions, which is called when the user clicks the back button (Android physical return button). The callback needs to return a Future object. If the final value of Future returned is false, the current route will not exit the stack (it will not be returned); when the final value is true, the current route will exit the stack. We need to provide this callback to decide whether or not to exit.

1.App component DateTime? LastPopTime;onWillPop: () async {if (lastPopTime = = null | | DateTime.now (). Difference (lastPopTime!) > Duration (seconds: 1)) {lastPopTime = DateTime.now (); ToastUtils.showToast ("press exit again"); return Future.value (false);} else {lastPopTime = DateTime.now (); / / exit app return Future.value (true);}}); 2.InAppWebview component DateTime? LastPopTime;onWillPop: () async {/ / webViewController?.goBack (); / / return Future.value (false); if (lastPopTime = = null | | DateTime.now (). Difference (lastPopTime!) > Duration (seconds: 1)) {lastPopTime = DateTime.now (); if (await webViewController?.canGoBack () = = true) {await webViewController?.goBack ();} else {ToastUtils.showToast ("Press exit again");} return Future.value (false) } else {lastPopTime = DateTime.now (); / / exit app return Future.value (true); / / await SystemChannels.platform.invokeMethod ('SystemNavigator.pop');}}) After reading this, the article "how to click Flutter twice to quit app" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report