有没有一种使用Flutter在PDF中检测触摸的方法?

3
我正在使用dart pdf库,并且想要在查看pdf时检测屏幕的触摸事件。有没有办法实现这个功能?为了查看pdf文件,我使用了一个带有创建文件路径的PDFViewerScaffold。我尝试用ListenerGestureDetector包装PDFViewerScaffold,但是没有成功。
我目前的代码:
class PdfViewerPage extends StatelessWidget {
  final String path;
  const PdfViewerPage({Key key, this.path}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(
          path: path,
    );
  }
}

制作 PDF 文件:
final Document pdf = Document();

...

pdf.addPage(MultiPage(
      pageFormat:
      ...
      footer: (Context context) {
        ...
      },
      build: (Context context) => <Widget>[
        ...
...

任何帮助都将不胜感激!

哦,我用打印库测试了一下, 现在我会尝试使用flutter_full_pdf_viewer.. - Taleb
如果你只是想要手势,你应该将它与主 Scaffold 和 GestureDetector 包装起来: @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: (){ print('tap'); }, child: PDFViewerScaffold(path: 'assets/pdf/flutter_tutorial.pdf')), ); - Taleb
你想在 PDF 上使用触摸做什么?(请详细说明您想要做什么) - Taleb
请按照此教程进行操作: https://pspdfkit.com/blog/2019/getting-started-with-pspdfkit-flutter/同时,您也可以克隆此存储库并自行检查测试示例: https://github.com/PSPDFKit/pspdfkit-flutter - Taleb
你是否尝试使用MethodChannel通过本地方式获取数据?因为在Android有解决方案。https://stackoverflow.com/questions/59861608/gesturedetector-not-working-for-android-pdfviewer-lib - Neelay Srivastava
显示剩余6条评论
1个回答

1

我之前也遇到了同样的问题,FlutterFullPdfViewer使用本地组件并将其封装在FrameLayout(在Android中)中,只能通过本地代码进行操作。 我的解决方法是fork该项目,并添加自己的实现。 在Android部分,您可以在FlutterFullPdfViewerManager.java中找到一个名为的方法:

void openPDF(String path) {
    File file = new File(path);
    pdfView.fromFile(file)
            .enableSwipe(true)
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(0)
            .load();
}

您可以将此更改为:

void openPDF(String path) {
    File file = new File(path);
    pdfView.fromFile(file)
            .enableSwipe(true)
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .onTap(e-> {
                if (e.getRawX() > 300) {//put your value here
                    //send rebuild instruction to flutter
                }
            .defaultPage(0)
            .load();
}

或者类似这样,您还会发现在pdfView上有许多额外的功能可以帮助您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接