0
点赞
收藏
分享

微信扫一扫

Flutter监听返回键

在项目过程中,有两个实际场景需要去监听返回键

1.双击两次返回键退出应用

2.全屏webview,点击返回键,回退到上个url或退出webview,实现如下图:

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        Future<bool> canGoBack = flutterWebViewPlugin.canGoBack();
        canGoBack.then((str) {
          if (str) {
            flutterWebViewPlugin.goBack();
          } else {
            Navigator.of(context).pop();
          }
        });
      },
      child: Scaffold(
        body: new Center(
            child: new WebviewScaffold(
          url: widget.url,
          appBar: PreferredSize(
            preferredSize:
                Size.fromHeight(MediaQueryData.fromWindow(window).padding.top),
            child: SafeArea(
              top: true,
              child: Offstage(),
            ),
          ),
        )),
      ),
    );
  }```
举报

相关推荐

0 条评论