当前位置:网站首页>Monitor the shuttle return button

Monitor the shuttle return button

2022-07-04 21:32:00 Boundless 6688

Monitoring of physical buttons and return buttons

Flutter The return button is monitored through WillPopScope To achieve

class BackDemoState extends State<BackDemoWidget> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(
          appBar: AppBar(
            title: const Text(" Back key monitor "),
            leading: IconButton(
              icon: const Icon(Icons.arrow_back),
              onPressed: (){
                SmartDialog.showToast(" return ");
                if (Navigator.canPop(context)) {
                  Navigator.pop(context);
                } else {
                  SystemNavigator.pop();
                }
              },
            ),
          ),
        ));
  }

  Future<bool> _onWillPop() {
    SmartDialog.showToast(" return ");
    if (Navigator.canPop(context)) {
      Navigator.pop(context);
    } else {
      SystemNavigator.pop();
    }
    return Future.value(false);
  }
}

WebView Return listening of

 @override
  Widget build(BuildContext context) {
    return FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (context, snapshot) {
          return WillPopScope(
            onWillPop: () async {
              if (snapshot.hasData) {
                final bool canGoBack = await snapshot.data!.canGoBack();
                if (canGoBack) {
                  //  When a web page can be returned , Return to the previous page first 
                  await snapshot.data!.goBack();
                  return Future.value(false);
                }
              }
              return Future.value(true);
            },
            child: Scaffold(
              appBar: AppBar(
                title: Text(widget.title),
              ),
              body: Stack(
                children: [
                  WebView(
                    initialUrl: "https://www.baidu.com",
                    javascriptMode: JavascriptMode.unrestricted,
                    allowsInlineMediaPlayback: true,
                    onWebViewCreated: (WebViewController webViewController) {
                      _controller.complete(webViewController);
                    },
                    onProgress: (int progress) {
                      debugPrint('WebView is loading (progress : $progress%)');
                      setState(() {
                        _progressValue = progress;
                      });
                    },
                  ),
                  if (_progressValue != 100) LinearProgressIndicator(
                    value: _progressValue / 100,
                    backgroundColor: Colors.transparent,
                    minHeight: 2,
                  ) else Gaps.empty,
                ],
              ),
            ),
          );
        }
    );
  }
原网站

版权声明
本文为[Boundless 6688]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042034502057.html