为什么在WebView上更改URL后initState不起作用?

时间:2019-04-23 22:47:51

标签: webview dart flutter

我是新手,正在尝试阻止电话后退按钮,当应用启动时它可以工作,但是例如当我在google中搜索某些内容并且URL更改后,按钮仍然可以工作

我曾经尝试过WillPopScope,但从未为我工作

这是我的应用

    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
    import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
    import 'package:back_button_interceptor/back_button_interceptor.dart';

    String mainURL = "http://google.com";
    void main() async {
      await FlutterStatusbarManager.setHidden(true, animation:StatusBarAnimation.SLIDE);
      runApp(MaterialApp(home: App()));
    }

    class App extends StatefulWidget {
      @override
      AppState createState() => new AppState();
    }

    class AppState extends State<App> {
      @override
      void initState() {
        super.initState();
        BackButtonInterceptor.add(btnInterceptor);
        final fWPlugin = new FlutterWebviewPlugin();

        fWPlugin.onDestroy.listen((_) => exit(0));

        fWPlugin.onUrlChanged.listen((String url) {
          print("URL: " + url);
        });
      }

      @override
      void dispose() {
        BackButtonInterceptor.remove(btnInterceptor);
        super.dispose();
      }

      bool btnInterceptor(bool stopDefaultButtonEvent) {
        return true;
      }

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
              title: "Webview",
              theme: ThemeData.dark(),
              routes: {
                "/": (_) =>
                    WebviewScaffold(
                      url: mainURL,
                      withLocalUrl: true,
                      withJavascript: true,
                      withLocalStorage: true,
                      clearCache: false,
                      clearCookies: false,
                      withZoom: false,
                      enableAppScheme: true,
                    )
              });
      }
    }

我希望当您在资源管理器中搜索某些内容时,后退按钮仍被阻止`

1 个答案:

答案 0 :(得分:0)

那对我也不起作用。但我建议您转到https://github.com/fluttercommunity/flutter_webview_plugin/issues并提交问题报告。