Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public void onInitiallyRendered(int pages) {
args.put("pages", pages);
methodChannel.invokeMethod("onRender", args);
}
}).onPageScroll(new OnPageScrollListener() {
@Override
public void onPageScrolled(int page, float positionOffset) {
Map<String, Object> args = new HashMap<>();
args.put("page", page);
args.put("positionOffset", positionOffset);
methodChannel.invokeMethod("onPageScrolled", args);
}
}).enableDoubletap(true).defaultPage(getInt(params, "defaultPage")).load();
}
}
Expand Down
60 changes: 36 additions & 24 deletions lib/flutter_pdfview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef PageChangedCallback = void Function(int? page, int? total);
typedef ErrorCallback = void Function(dynamic error);
typedef PageErrorCallback = void Function(int? page, dynamic error);
typedef LinkHandlerCallback = void Function(String? uri);
typedef PageScrolledCallback = void Function(int? page, double? positionOffset);

enum FitPolicy { WIDTH, HEIGHT, BOTH }

Expand All @@ -24,6 +25,7 @@ class PDFView extends StatefulWidget {
this.onViewCreated,
this.onRender,
this.onPageChanged,
this.onPageScrolled,
this.onError,
this.onPageError,
this.onLinkHandler,
Expand All @@ -39,8 +41,7 @@ class PDFView extends StatefulWidget {
this.defaultPage = 0,
this.fitPolicy = FitPolicy.WIDTH,
this.preventLinkNavigation = false,
})
: assert(filePath != null || pdfData != null),
}) : assert(filePath != null || pdfData != null),
super(key: key);

@override
Expand All @@ -53,6 +54,7 @@ class PDFView extends StatefulWidget {
final ErrorCallback? onError;
final PageErrorCallback? onPageError;
final LinkHandlerCallback? onLinkHandler;
final PageScrolledCallback? onPageScrolled;

/// Which gestures should be consumed by the pdf view.
///
Expand Down Expand Up @@ -84,15 +86,17 @@ class PDFView extends StatefulWidget {

class _PDFViewState extends State<PDFView> {
final Completer<PDFViewController> _controller =
Completer<PDFViewController>();
Completer<PDFViewController>();

@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.android) {
return PlatformViewLink(
viewType: 'plugins.endigo.io/pdfview',
surfaceFactory: (BuildContext context,
PlatformViewController controller,) {
surfaceFactory: (
BuildContext context,
PlatformViewController controller,
) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: widget.gestureRecognizers ??
Expand All @@ -108,9 +112,8 @@ class _PDFViewState extends State<PDFView> {
creationParams: _CreationParams.fromWidget(widget).toMap(),
creationParamsCodec: const StandardMessageCodec(),
)
..addOnPlatformViewCreatedListener(params
.onPlatformViewCreated)..addOnPlatformViewCreatedListener((
int id) {
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..addOnPlatformViewCreatedListener((int id) {
_onPlatformViewCreated(id);
})
..create();
Expand Down Expand Up @@ -141,7 +144,7 @@ class _PDFViewState extends State<PDFView> {
void didUpdateWidget(PDFView oldWidget) {
super.didUpdateWidget(oldWidget);
_controller.future.then(
(PDFViewController controller) => controller._updateWidget(widget));
(PDFViewController controller) => controller._updateWidget(widget));
}
}

Expand Down Expand Up @@ -178,17 +181,18 @@ class _CreationParams {
}

class _PDFViewSettings {
_PDFViewSettings({this.enableSwipe,
this.swipeHorizontal,
this.password,
this.nightMode,
this.autoSpacing,
this.pageFling,
this.pageSnap,
this.defaultPage,
this.fitPolicy,
this.fitEachPage,
this.preventLinkNavigation});
_PDFViewSettings(
{this.enableSwipe,
this.swipeHorizontal,
this.password,
this.nightMode,
this.autoSpacing,
this.pageFling,
this.pageSnap,
this.defaultPage,
this.fitPolicy,
this.fitEachPage,
this.preventLinkNavigation});

static _PDFViewSettings fromWidget(PDFView widget) {
return _PDFViewSettings(
Expand Down Expand Up @@ -251,9 +255,10 @@ class _PDFViewSettings {
}

class PDFViewController {
PDFViewController._(int id,
this._widget,)
: _channel = MethodChannel('plugins.endigo.io/pdfview_$id') {
PDFViewController._(
int id,
this._widget,
) : _channel = MethodChannel('plugins.endigo.io/pdfview_$id') {
_settings = _PDFViewSettings.fromWidget(_widget);
_channel.setMethodCallHandler(_onMethodCall);
}
Expand Down Expand Up @@ -296,6 +301,13 @@ class PDFViewController {
_widget.onLinkHandler!(call.arguments);
}

return null;
case 'onPageScrolled':
if (_widget.onPageScrolled != null) {
_widget.onPageScrolled!(
call.arguments['page'], call.arguments['positionOffset']);
}

return null;
}
throw MissingPluginException(
Expand All @@ -314,7 +326,7 @@ class PDFViewController {

Future<bool?> setPage(int page) async {
final bool? isSet =
await _channel.invokeMethod('setPage', <String, dynamic>{
await _channel.invokeMethod('setPage', <String, dynamic>{
'page': page,
});
return isSet;
Expand Down