点击gesture识别器在flutter web视图中不工作

7gyucuyw  于 2022-12-05  发布在  Flutter
关注(0)|答案(3)|浏览(193)

我试着在flutter webview中使用GestureRecognizer来缩小双捏。但是它不起作用。如果你有任何解决方案,请帮助我

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';

void main()=>runApp(WebView());

class WebView extends StatefulWidget {
  @override
  _WebViewState createState() => _WebViewState();
}

class _WebViewState extends State<WebView> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Builder(builder: (BuildContext context) {
        return WebView(
          key: UniqueKey(),
          gestureRecognizers:
          Set()..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer(),)),//ZOOM WEBVIEW

          initialUrl:
              'https://stackoverflow.com/questions/',

          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (WebViewController webViewController) {
            _controller.complete(webViewController);
          },
        );
      }),

    );
  }
}

这是代码的重要组成部分,如果你需要完整的代码,请发表评论。

yhived7q

yhived7q1#

我也遇到了同样的问题,通过将WebView Package 到GestureDetector中,并在其中包含onTap(),可以使TapGestureRecognizer工作。

@override
 Widget build(BuildContext context) {
return Scaffold(
  body: GestureDetector(
    onTap: () {
      print("This one doesn't print");
    },
    child: Container(
      child: Stack(
        children: <Widget>[
          WebView(
            initialUrl:
                'example.com',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _controller.complete(webViewController);
            },
            gestureRecognizers: Set()
              ..add(
                  Factory<TapGestureRecognizer>(() => TapGestureRecognizer()
                    ..onTapDown = (tap) {
                      print("This one prints");
                    })),
          ),
        ],
      ),
    ),
  ),
);
}

想知道是否有人有类似的问题,并使其正常工作。

7rfyedvj

7rfyedvj2#

直到没有一个适当的方法来做它,我已经这样做了下面为我的情况

Container(
    child: Stack(
      children: <Widget>[
        WebView(
          initialUrl:
              'example.com',
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (WebViewController webViewController) {
            _controller.complete(webViewController);
          },
          gestureRecognizers: Set()
            ..add(
                Factory<TapGestureRecognizer>(() => TapGestureRecognizer()
                  ..onTapDown = (tap) {
                    print("This one prints");
                  })),
        ),
        GestureDetector(
              onTap: () {
                print("ontap");
              },
              child: Container(
                  color: Colors.transparent,
                  width: double.infinity,
                  height: 50,
              ),
            )
      ],
    ),
  ),
ct2axkht

ct2axkht3#

要在iOS上解决这个问题,只需将以下代码行添加到info.plist文件中。

<key>NSAppTransportSecurity</key>
 <dict>
 <key>NSAllowsArbitraryLoads</key><true/>
</dict>

希望这能为你解决问题。

相关问题