如何在android上关闭Flitter WebView插件应用程序的后退按钮

如何在android上关闭Flitter WebView插件应用程序的后退按钮,android,flutter,flutterwebviewplugin,Android,Flutter,Flutterwebviewplugin,我是一个新手,在旅途中学习,当没有更多的WebView历史回顾时,我试图用后退按钮关闭我的应用程序。例如,在WebView应用程序中,初始URL是google.com,我导航到yahoo.com,当我按下后退按钮时,它会返回到google.com,如果我再次按下该应用程序,我希望它在没有更多历史记录时退出。我在Flatter WebView插件页面上尝试了CanGoBack()函数,但在vscode中出现了错误。我不知道如何实现这一点 import 'package:flutter/materi

我是一个新手,在旅途中学习,当没有更多的WebView历史回顾时,我试图用后退按钮关闭我的应用程序。例如,在WebView应用程序中,初始URL是google.com,我导航到yahoo.com,当我按下后退按钮时,它会返回到google.com,如果我再次按下该应用程序,我希望它在没有更多历史记录时退出。我在Flatter WebView插件页面上尝试了CanGoBack()函数,但在vscode中出现了错误。我不知道如何实现这一点

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

class WebviewInFlutter extends StatefulWidget {
  WebviewInFlutter({Key key}) : super(key: key);


  @override
  _WebviewInFlutterState createState() => _WebviewInFlutterState();
}

class _WebviewInFlutterState extends State<WebviewInFlutter> {

  final FirebaseMessaging _messaging = FirebaseMessaging();

  @override
  void initState(){

    super.initState();
    _messaging.getToken().then((token) {
      print(token);
    });

    _messaging.configure(
      onMessage: (Map<String, dynamic> message) async{
        print('on message $message');
      },
      onResume: (Map<String, dynamic> message) async{
        print('on resume $message');
      },
      onLaunch: (Map<String, dynamic> message) async{
        print('on launch $message');
      },
    );
    _messaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));

  }
  final flutterWebviewPlugin = new FlutterWebviewPlugin();




  @override
  Widget build(BuildContext context) {

    return WebviewScaffold(
      url: 'https://google.com',
      hidden: true,
      appCacheEnabled: true,
      withJavascript: true,
      withLocalStorage: true,
      appBar: AppBar(
        actions: <Widget>[
              IconButton(
                icon: Icon(Icons.refresh, color: Color.fromRGBO(255, 255, 255, 1.0),),
                onPressed: () => flutterWebviewPlugin.reload(), // this is reloading the url that was provided to webview, not the current URL.
              )
            ],
          elevation: 1.0,
          centerTitle: true,
          title: Text("Google Mobile")

      ),


    );

  }

}



导入“包装:颤振/材料.省道”;
导入“包:flatter_webview_plugin/flatter_webview_plugin.dart”;
导入“package:firebase_messaging/firebase_messaging.dart”;
类WebViewInflotter扩展StatefulWidget{
WebViewInflotter({Key}):超级(Key:Key);
@凌驾
_WebviewInFlutterState createState()=>\u WebviewInFlutterState();
}
类_WebviewInFlutterState扩展状态{
最终FirebaseMessaging_messaging=FirebaseMessaging();
@凌驾
void initState(){
super.initState();
_messaging.getToken().then((令牌){
打印(代币);
});
_消息传递。配置(
onMessage:(映射消息)异步{
打印('on message$message');
},
onResume:(映射消息)异步{
打印('在简历上$message');
},
onLaunch:(映射消息)异步{
打印('on launch$message');
},
);
_messaging.requestNotificationPermissions(
const IosNotificationSettings(声音:true、徽章:true、警报:true));
}
最终FlatterWebViewPlugin=新的FlatterWebViewPlugin();
@凌驾
小部件构建(构建上下文){
返回WebviewScaffold(
网址:'https://google.com',
隐藏:是的,
appCacheEnabled:true,
withJavascript:true,
withLocalStorage:true,
appBar:appBar(
行动:[
图标按钮(
图标:图标(Icons.refresh,color:color.fromRGBO(255,255,255,1.0)),
onPressed:()=>WebViewPlugin.reload(),//这是重新加载提供给webview的url,而不是当前url。
)
],
标高:1.0,
标题:对,
标题:文本(“谷歌移动”)
),
);
}
}

您可以根据需要自定义AppBar中的后退按钮

..................................

appBar: AppBar(
leading: new IconButton(
              icon: new Icon(Icons.arrow_back),
              onPressed: () {
                flutterWebviewPlugin.canGoBack().then((value) {
                  if (value) {
                    flutterWebviewPlugin.goBack();
                  } else {
                    Navigator.pop(context);
                  }
                });
              }),
actions: <Widget>[

..................................
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
appBar:appBar(
领先:新图标按钮(
图标:新图标(图标。箭头返回),
已按下:(){
flatterWebViewPlugin.canGoBack().then((值){
如果(值){
webviewplugin.goBack();
}否则{
Navigator.pop(上下文);
}
});
}),
行动:[
..................................

这将检查您是否可以在WebView中返回,或者堆栈中没有可用的历史记录。

您可以根据需要在AppBar中自定义“返回”按钮

..................................

appBar: AppBar(
leading: new IconButton(
              icon: new Icon(Icons.arrow_back),
              onPressed: () {
                flutterWebviewPlugin.canGoBack().then((value) {
                  if (value) {
                    flutterWebviewPlugin.goBack();
                  } else {
                    Navigator.pop(context);
                  }
                });
              }),
actions: <Widget>[

..................................
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
appBar:appBar(
领先:新图标按钮(
图标:新图标(图标。箭头返回),
已按下:(){
flatterWebViewPlugin.canGoBack().then((值){
如果(值){
webviewplugin.goBack();
}否则{
Navigator.pop(上下文);
}
});
}),
行动:[
..................................
这将检查您是否可以在WebView中返回,或者堆栈中没有可用的历史记录。

您可以尝试我的插件(编辑:它已重命名为)

下面是一个例子:

导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“包:flatter_inappwebview/flatter_inappwebview.dart”;
Future main()异步{
runApp(新的MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>new_MyAppState();
}
类MyAppState扩展了状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:InAppWebViewPage()
);
}
}
类InAppWebViewPage扩展了StatefulWidget{
@凌驾
_InAppWebViewPageState createState()=>新建;
}
类_InAppWebViewPageState扩展状态{
inappwebview控制器webView;
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()异步{
if(webView!=null){
if(等待webView.canGoBack()){
//获取webview历史记录
WebHistory WebHistory=等待webView.getCopyBackForwardList();
//如果webHistory.currentIndex对应于1或0
如果(webHistory.currentIndex,您可以尝试我的插件(编辑:已重命名为)

下面是一个例子:

导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“包:flatter_inappwebview/flatter_inappwebview.dart”;
Future main()异步{
runApp(新的MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>new_MyAppState();
}
类MyAppState扩展了状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:InAppWebViewPage()
);
}
}
类InAppWebViewPage扩展了StatefulWidget{
@凌驾
_InAppWebViewPageState createState()=>新建;
}
类_InAppWebViewPageState扩展状态{
inappwebview控制器webView;
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()异步{
if(webView!=null){
if(等待webView.canGoBack()){
//获取webview历史记录
韦比斯
@override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back_sharp),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        title: const Text('Properties Near You'),
        backgroundColor: Colors.green[700],
      ),


      body: WebView(
        initialUrl: initialUrl,

        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (webViewController) => _webViewController = webViewController,
        onPageFinished: (String url) {
          if (url == initialUrl) {
            _redirectToStripe(widget.sessionId);
          }
        },
        navigationDelegate: (NavigationRequest request) {
          print(request);
          print(request.url);
          if (request.url.startsWith('http://localhost:5000/success.html')) {
            Navigator.of(context).pop();
            //Navigator.of(context).pushReplacementNamed('/success');
          } else if (request.url.startsWith('http://mykinderpass/cancel')) {
            Navigator.of(context).pop();
            //Navigator.of(context).pushReplacementNamed('/cancel');
          }
          return NavigationDecision.navigate;
        },
      ),

    );
  }