WebViewController?_webviewController;WillPopScope(onWillPop:()=>_exitApp(context),child:xxx,}Future<bool>_exitApp(BuildContextcontext)async{if(await_webviewController!.canGoBack()){print("onwill goback");_webviewController!.goBack();returnFuture.value(false);}else{debugPrint("_exit will not go back");returnFuture.value(true);}}
import'dart:async';import'package:flutter/material.dart';import'package:webview_flutter/webview_flutter.dart';voidmain()=>runApp(MaterialApp(home:WebViewExample()));classWebViewExampleextendsStatefulWidget{@override_WebViewExampleStatecreateState()=>_WebViewExampleState();}class_WebViewExampleStateextendsState<WebViewExample>{WebViewController?_webviewController;Future<bool>_exitApp(BuildContextcontext)async{if(await_webviewController!.canGoBack()){print("onwill goback");_webviewController!.goBack();returnFuture.value(false);}else{debugPrint("_exit will not go back");returnFuture.value(true);}}@overrideWidgetbuild(BuildContextcontext){returnWillPopScope(onWillPop:()=>_exitApp(context),child:Scaffold(appBar:AppBar(title:constText('Flutter WebView example'),// This drop down menu demonstrates that Flutter widgets can be shown over the web view.),// We're using a Builder here so we have a context that is below the Scaffold// to allow calling Scaffold.of(context) so we can show a snackbar.body:Builder(builder:(BuildContextcontext){returnWebView(initialUrl:'http://droidyue.com',javascriptMode:JavascriptMode.unrestricted,onWebViewCreated:(WebViewControllerwebViewController){_webviewController=webViewController;},);}),),);}}