I’m using syncfusion_flutter_pdfviewer which has its own ‘gesture event handler’, and I wanted to detect when a user tap on screen to show/hide some buttons.
The problem is that any touch inside the pdf page isn’t detected by GestureDetector
So is there a way to force detect touches inside pdf widget or something??
GestureDetector(
onTap: (){
print('click event');
},
child: Stack(
children: [
PdfViewer(filePath: path, pdfKey: _pdfViewerKey, controller: controller,),
TopBar(title: basename(path)),
],
),
),
2
Answers
I referred to this answer from Syncfusion team which implies the use of
RawGestureDetector
, but I noticed a delay when detecting taps; also when I performed (pinch to zoom) gesture, an error occurs 90% of the time.Then I referred to this answer in which they used a
GestureDetector
withonPanDown
attribute, but the problem is that it detects any touch before the user left up his finger, and that includs swipes.What I came up with:
1- I put an empty
Container
with the size of the screen with aGestureDetector
on top of my PdfView.2- Normally using a
GestureDetector
wouldn't work on an emptyContainer
so I had to use theHitTestBehavior.translucent
attribute.3- You can't use a translucid color because that would prevent any touch event on the PdfView.
We have prepared a workaround to retrieve the tapped coordinates when the user taps the SfPdfViewer, which can be downloaded from the following link,
https://www.syncfusion.com/downloads/support/directtrac/general/ze/tapped_position-721291336
Please find the workflow for this example application below,
We have used the TapGestureRecoginzer to catch the tap action and onTapDown callback we have called _getPdfCoordinates() method, it has the logic to return the tapped location coordinates relative to the PDF page.