我正在使用flatter_pdfview在我的Flutter应用程序。在后台,PDFView小部件根据平台使用AndroidView或UiCitView
我想获得点击事件相对于PDF文档的坐标,因为我知道用户可以在PDF上缩放和平移
为此,我需要订阅缩放和平移事件,以便了解文档的哪个部分当前显示在屏幕上。然后我可以弄清楚用户点击的确切位置。
我试着通过我自己的ScaleGestureRecognizer
和PanGestureRecognizer
。不幸的是,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),
},
);
}
}
我找到了这个问题的解决方案。我正在检查onTapDown的存在,并添加了
gestureRecognizers: Set()
..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer()
..onTapDown = (tap) {
print("hell bro");
})),
但是,不知怎么的,它不起作用。我正在检查其他解决方案,并考虑添加手势检测器并从中检测敲击。
所以,我刚刚用手势检测器包装了PDFView。令人惊讶的是,PDFView中的手势识别器在添加GestureDetector((Widget后工作。由于我使用手势识别器检查onTapDown事件,我在手势检测器中添加了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),
),)
);