0

https://pub.dev/packages/flutter_inappwebviewパッケージを使用して、Flutter アプリケーションで Web コンテンツを表示しています。しかし、試しているすべてのWebサイトで奇妙なポップアップとアラートが表示されます。これらのポップアップは、私のコードやプロジェクトとは関係ありません。ポップアップの内容はまったく意味がありません。ここにいくつかのスクリーンショットを添付しました。

ここに画像の説明を入力

ここに画像の説明を入力

これらは Flutter ウィジェットからのものではありません。これらは webview 内にあります。ここにコードを含めます。どこが欠けているかを知る必要があるため、ここにクラス全体を含める必要があります

class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {

  final GlobalKey webViewKey = GlobalKey();


  InAppWebViewController? webViewController;
  PullToRefreshController? pullToRefreshController;

  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
        supportZoom: Parameters.zoomControl,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

  // late PullToRefreshController pullToRefreshController;
  late ContextMenu contextMenu;
  String url = "";
  double progress = 0;
  final urlController = TextEditingController();
  Map _source = {ConnectivityResult.wifi: true};
  final MyConnectivity _connectivity = MyConnectivity.instance;
  final BannerAd myBanner = BannerAd(
    adUnitId: Parameters.adUnitId,
    size: AdSize.banner,
    request: AdRequest(),
    listener: BannerAdListener(),
  );


  getPermissions() async {
    for (var element in Parameters.permissions) {
      if(element=='camera') {
        await Permission.camera.request();
      } else if(element=='location') {
        await Permission.location.request();
      } else if(element=='audio') {
        await Permission.microphone.request();
        await Permission.speech.request();
      } else if(element=='storage') {
        await Permission.storage.request();
      }
    }
  }
  @override
  void initState() {
    getPermissions();
    pullToRefreshController = PullToRefreshController(
      onRefresh: (){
        if(Parameters.pullToReload) {
          webViewController!.reload().then((value) {
          pullToRefreshController!.endRefreshing();
        });
        }

      },
    );
    if(Parameters.adUnitId.isNotEmpty) {
      myBanner.load();
    }
    super.initState();
      _connectivity.initialise();
      _connectivity.myStream.listen((source) {
        setState(() => _source = source);
      });


    // contextMenu = ContextMenu(
    //     menuItems: [
    //       ContextMenuItem(
    //           androidId: 1,
    //           iosId: "1",
    //           title: "Special",
    //           action: () async {
    //             print("Menu item Special clicked!");
    //             print(await webViewController?.getSelectedText());
    //             await webViewController?.clearFocus();
    //           })
    //     ],
    //     options: ContextMenuOptions(hideDefaultSystemContextMenuItems: false),
    //     onCreateContextMenu: (hitTestResult) async {
    //       print("onCreateContextMenu");
    //       print(hitTestResult.extra);
    //       print(await webViewController?.getSelectedText());
    //     },
    //     onHideContextMenu: () {
    //       print("onHideContextMenu");
    //     },
    //     onContextMenuActionItemClicked: (contextMenuItemClicked) async {
    //       var id = (Platform.isAndroid)
    //           ? contextMenuItemClicked.androidId
    //           : contextMenuItemClicked.iosId;
    //       print("onContextMenuActionItemClicked: " +
    //           id.toString() +
    //           " " +
    //           contextMenuItemClicked.title);
    //     });


  }

  @override
  void dispose() {
    if(Parameters.adUnitId.isNotEmpty) {
      myBanner.dispose();
    }
    super.dispose();
  }

  Future<bool> _goBack(BuildContext context) async {
    if (await webViewController!.canGoBack()) {
      webViewController!.goBack();
      return Future.value(false);
    } else {
      CoolAlert.show(
          context: context,
          type: CoolAlertType.confirm,
          confirmBtnColor: Colors.red,
          text: "Exit",
          onConfirmBtnTap: (){
            exit(0);
          }
      );
      return Future.value(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => _goBack(context),
      child: _source.keys.toList()[0]!=ConnectivityResult.none ?  SafeArea(
        child: Scaffold(
            body: Column(

                children: <Widget>[
             
              Expanded(
                child: Stack(
                  children: [
                    InAppWebView(
                      key: webViewKey,
                      pullToRefreshController: pullToRefreshController,
                      onJsAlert: (InAppWebViewController controller,  message) async {
                        print('收到--$message');
                        return null;
                      },
                      // contextMenu: contextMenu,
                      /// HERE YOU NEED TO PUT URL BELOW
                      initialUrlRequest:
                      URLRequest(url: Uri.parse(Parameters.webViewURL)),
                      // initialFile: "assets/index.html",
                      initialUserScripts: UnmodifiableListView<UserScript>([]),
                      initialOptions: options,
                      //pullToRefreshController: pullToRefreshController,
                      onWebViewCreated: (controller) {
                        webViewController = controller;
                      },
                      onLoadStart: (controller, url) {
                        if(Parameters.webViewMode=='desktop') {
                          webViewController!.evaluateJavascript(source: "document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));");
                        }
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      androidOnPermissionRequest: (controller, origin, resources) async {
                        return PermissionRequestResponse(
                            resources: resources,
                            action: PermissionRequestResponseAction.GRANT);
                      },
                      shouldOverrideUrlLoading: (controller, navigationAction) async {

                        var uri = navigationAction.request.url!;

                        if (![
                          "http",
                          "https",
                          "file",
                          "chrome",
                          "data",
                          "javascript",
                          "about"
                        ].contains(uri.scheme)) {
                            return NavigationActionPolicy.CANCEL;
                        }

                        return NavigationActionPolicy.ALLOW;
                      },
                      onLoadStop: (controller, url) async {

                        //pullToRefreshController.endRefreshing();
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      onLoadError: (controller, url, code, message) {
                        //pullToRefreshController.endRefreshing();
                      },
                      onProgressChanged: (controller, progress) {

                        if (progress == 100) {
                          //pullToRefreshController.endRefreshing();
                        }
                        setState(() {
                          this.progress = progress / 100;
                          urlController.text = this.url;
                        });
                      },
                      onUpdateVisitedHistory: (controller, url, androidIsReload) {
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      onConsoleMessage: (controller, consoleMessage) {
                        print(consoleMessage);
                      },
                    ),
                    progress < 1.0
                        ? LinearProgressIndicator(value: progress)
                        : Container(),
                   
                  ],
                ),
              ),
             
            ])),
      ):
      Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(child: Column(
              children: [
                Icon(Icons.signal_wifi_off,size: 100,),
                SizedBox(height: 20,),
                Text('No Interent Connection',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20),)
              ],
            )),

          ],
        ),
      ),
    );
  }
4

0 に答える 0