如何在Flutter中检测AndroidView或UiKitView中的缩放和平移手势?

t98cgbkg  于 2023-01-06  发布在  Flutter
关注(0)|答案(1)|浏览(236)

我正在使用flutter_pdfview在我的Flutter应用程序中显示PDF。在引擎盖下,PDFView小部件使用AndroidViewUiKitView,具体取决于平台。
我想得到一个点击事件相对于PDF文档的坐标,知道用户可以缩放和平移PDF。
为此,我需要订阅缩放和平移事件,以便知道文档的哪一部分当前显示在屏幕上,然后我才能计算出用户点击的确切位置。
我试过传递我自己的ScaleGestureRecognizerPanGestureRecognizer,不幸的是,onUpdate()onStart()似乎从未被触发过。
下面是我的代码:

class PDFViewer extends StatelessWidget {
  PDFViewer({this.filePath});

  final String filePath;

  @override
  Widget build(BuildContext context) {
    final scale = ScaleGestureRecognizer()
      ..onUpdate = (details) {
        print(details.scale);
      };
    final pan = PanGestureRecognizer()
      ..onUpdate = (details) {
        print(details.delta);
      }
      ..onStart = (_) {
        print('Start pan');
      }
      ..onEnd = (_) {
        print('End pan');
      };

    return PDFView(
      filePath: filePath,
      gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
        Factory<ScaleGestureRecognizer>(() => scale),
        Factory<PanGestureRecognizer>(() => pan),
      },
    );
  }
}
dfty9e19

dfty9e191#

我找到了这个问题的解决方案。我正在检查onTapDown的存在,并添加了

gestureRecognizers: Set()
        ..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer()
          ..onTapDown = (tap) {
            print("hell bro");
          })),

但是,它不工作不知何故。我正在检查其他解决方案,并认为添加手势检测器和检测点击从。
所以,我只是用一个GestureDetector Package PDFView。令人惊讶的是,PDFView中的gestureRecognizers在添加GestureDetector()小部件后工作。因为我用gestureRecognizers检查onTapDown事件,所以我在GestureDetector中添加了onTap参数。
我的小工具看起来像这样:

GestureDetector(
    onTap: (){}, //This has to be added. Won't work without this.
    child: PDFView(
      filePath: widget.link,
      onError: (error) {
        print(error.toString());
      },
      onPageError: (page, error) {
        print('$page: ${error.toString()}');
      },
      onViewCreated: (PDFViewController pdfViewController) {
        pdfController=pdfViewController;
      },
      onPageChanged: (a, b){
        visible=false;
      },
      gestureRecognizers: Set()
        ..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer()
          ..onTapDown = (tap) {
            print("Tap Down Gesture Detected");
          })),
    ),
  )

因此,对于您的情况,我猜您必须添加以下代码:

return GestureDetector(
            onScaleStart: (a){}, //This has to be added. Won't work without this.
            onPanStart: (a){}, //This has to be added. Won't work without this.
            child: PDFView(
              filePath: filePath,
              gestureRecognizers: Set()
                ..add(
                    Factory<OneSequenceGestureRecognizer>(() => scale)
                )
                ..add(
                Factory<OneSequenceGestureRecognizer>(() => pan),
            ),)
          );

相关问题