美文网首页flutter
Flutter之WillPopScope组件

Flutter之WillPopScope组件

作者: 习惯了_就好 | 来源:发表于2019-04-10 15:19 被阅读0次
/**
 *  导航返回拦截,避免用户误触返回按钮而导致APP退出,常用的双击退出功能
 * const WillPopScope({
    Key key,
    @required this.child,
    @required this.onWillPop,//当用户点击返回按钮时调用(包括导航返回按钮及Android物理返回按钮),返回 Future.value(false); 表示不退出;返回 Future.value(true); 表示退出.
    })
 */
@override
  Widget build(BuildContext context) {
    DateTime lastTime;

    return WillPopScope(
      onWillPop: () async {
        if (lastTime == null || DateTime.now().difference(lastTime) > Duration(seconds: 1)) {
          lastTime = DateTime.now();
          Toast.toast(context, "双击退出");
          return false;
        }
        return true;
      },
      child: MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text("双击退出"),
          ),
        ),
      ),
    );
  }

相关文章

网友评论

    本文标题:Flutter之WillPopScope组件

    本文链接:https://www.haomeiwen.com/subject/fgegiqtx.html