3

I'm using dart pdf library and I want to detect touch on screen while the pdf is being viewed. Is there a way to do that? For viewing the pdf file I'm using a PDFViewerScaffold with the created file's path. I tried wrapping the PDFViewerScaffold with Listener and GestureDetector, but no luck. My code so far:

Viewing the pdf file:

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,
    );
  }
}

Making the pdf file:

final Document pdf = Document();

...

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

Any help would be appreciated!

Mobina
  • 6,369
  • 2
  • 25
  • 41
  • I checked this library but i did not find any GestureDetector widget, it seems this library dose not support GestureDetector yet. – Taleb Jul 26 '20 at 11:32
  • The _dart pdf library_ only creates a pdf file. It can neither show the file nor interact with user. That's why it doesn't have a GestureDetector. I'm using _flutter full pdf viewer_ library to show the pdf file. @Taleb – Mobina Jul 26 '20 at 11:48
  • Oh, i test it with printing library, i will try flutter_full_pdf_viewer now.. – Taleb Jul 26 '20 at 11:55
  • if you just want the gesture you should wrap it with your main Scaffold and GestureDetector : @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: (){ print('tap'); }, child: PDFViewerScaffold(path: 'assets/pdf/flutter_tutorial.pdf')), ); – Taleb Jul 26 '20 at 12:34
  • I've already tried this, it doesn't work. @Taleb – Mobina Jul 26 '20 at 17:19
  • What do you want to do with touch on pdf ??? (please explain more about what you want to do exactly ) – Taleb Jul 26 '20 at 17:29
  • I want the user to choose the contents of the pdf file, so I need to detect tap on different spots of the screen while the user is viewing the pdf file (like when a tap is detected on the right side of the screen, it generates the pdf file again but with new contents). @Taleb – Mobina Jul 26 '20 at 17:37
  • please follow this tutorial => https://pspdfkit.com/blog/2019/getting-started-with-pspdfkit-flutter/ and also clone this repository and checkout test example your self => https://github.com/PSPDFKit/pspdfkit-flutter – Taleb Jul 26 '20 at 18:08
  • Unfortunately PSPDFKit is not free to use, thank you though. @Taleb – Mobina Jul 26 '20 at 18:42
  • I try it with no licence key at this moment and it works for me . – Taleb Jul 26 '20 at 18:44
  • have you tried going native and getting data through methodchannel as there are solution in android https://stackoverflow.com/questions/59861608/gesturedetector-not-working-for-android-pdfviewer-lib – Neelay Srivastava Jun 22 '21 at 08:53

1 Answers1

1

I had the same problem a while back, FlutterFullPdfViewer uses native component and encapsulate it in a FrameLayout(in android) that can only be manipulated through native code. What I did is that I forked the project, and added my own implementation. In the android part you have a method called in FlutterFullPdfViewerManager.java :

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

You can change this to:

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();
}

Or something along these lines, you will also find that there is a lot of extra stuff that you can do on pdfView that could be helpful to you.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mike
  • 348
  • 2
  • 8