Flutter - Webview 中的 onPageFinished 无法正常工作

RJB*_*RJB 12 webview flutter

我使用回调方法“onPageFinished”来控制进度拨号器的可见性,但该行为在 Android 上不起作用。

该回调方法在Android设备上被调用3次,分别在开始页面加载和完成页面加载时,

版本:- webview_flutter:^3.0.0

这是我的代码:

class _MyHomePageState extends State<MyHomePage> {

  final Completer<WebViewController> _controller =
  Completer<WebViewController>();
  bool isLoading=true;

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
  }


  @override
  Widget build(BuildContext context) {

    return SafeArea(
      child: Scaffold(

        body: Stack(
          children: [

          Builder(builder: (BuildContext context) {
            return WebView(
              initialUrl: 'https://google.com/',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController webViewController) {
                _controller.complete(webViewController);
              },
              onProgress: (int progress) {
                print('WebView is loading (progress : $progress%)');

              },
              javascriptChannels: <JavascriptChannel>{
                _toasterJavascriptChannel(context),
              },
              navigationDelegate: (NavigationRequest request) {
                if (request.url.startsWith('https://www.youtube.com/')) {
                  print('blocking navigation to $request}');
                  return NavigationDecision.prevent;
                }
                print('allowing navigation to $request');
                return NavigationDecision.navigate;
              },
              onPageStarted: (String url) {
                print('Page started loading: $url');
              },
              onPageFinished: (String url) {
                print('Page finished loading: $url');
                setState(() {
                  isLoading = false;
                });
              },
              gestureNavigationEnabled: true,
              backgroundColor: const Color(0x00000000),
            );
          }),
            isLoading ? Center( child: CircularProgressIndicator(),)
                : Stack(),
          ]
        ),

      ),
    );
  }

  JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
    return JavascriptChannel(
        name: 'Toaster',
        onMessageReceived: (JavascriptMessage message) {
          // ignore: deprecated_member_use
          Scaffold.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        });
  }


}
Run Code Online (Sandbox Code Playgroud)

小智 1

尝试这个 :

 onProgress: (int progress) {
         print('WebView is loading (progress : $progress%)');
    if( progress == 100 ) { 
    setState(() => isLoading = false ; );
}      },
Run Code Online (Sandbox Code Playgroud)