美文网首页Flutter
Flutter(二十九)WillPopScope

Flutter(二十九)WillPopScope

作者: 天色将变 | 来源:发表于2019-07-19 07:37 被阅读1次

硬件返回键的监控,注意

构造方法
const WillPopScope({
    Key key,
    @required this.child, // 可以给出一些提示信息
    @required this.onWillPop,// 点击硬件返回键调用此方法
  })
image.png
class _MyHomePageState extends State<MyHomePage> {
  DateTime lastTime;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: WillPopScope(
            child: SizedBox(
              width: 50,
              height: 50,
              child: Text("Back"),
            ),
          onWillPop: () async {
              //连点两次,间隔不超过1秒时,认为是真的想退出
              if(lastTime==null || DateTime.now().difference(lastTime)>Duration(seconds: 1)){
                lastTime = DateTime.now();
                print(false);
                return false;// 不退出
              }
              print(true);
              return true;// 退出
          },
        ),
      ),
    );
  }
}

相关文章

网友评论

    本文标题:Flutter(二十九)WillPopScope

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