Events and notifications

Nutrient Flutter SDK allows you to listen to various events that occur when the end user interacts with Nutrient.

PspdfkitWidget events

Nutrient Flutter SDK supports the following events:

  • onDocumentLoaded — Called when a document is loaded with the loaded PdfDocument object. This event is triggered after the document is loaded and the first page is rendered.

  • onDocumentLoadFailed — Called when a document fails to load. The event delivers an error message with the reason for the failure.

  • onPageChanged — Called when a page is changed with the new page index. This event is triggered when a document is first loaded or when a user navigates to a new page.

To listen to these events, add an event listener to the PspdfkitWidget:

PspdfkitWidget(
  document: document,
  onDocumentLoaded: (pdfDocument) {
    print('Document loaded');
  },
  onDocumentLoadFailed: (error) {
    print('Document load failed: $error');
  },
  onPageChanged: (pageIndex) {
    print('Page changed to $pageIndex');
  },
);

Web-only events

Nutrient Flutter SDK supports Web-only annotation events. An event listener can be added to the PspdfkitWidget via the PspdfkitWidgetController:

pspdfkitWidgetController.addEventListener('<event-type>', (event) {
            // Do something with the event.
    });

A complete list of supported events can be found in the Nutrient Web SDK documentation.

Present mode events

The following table lists the events supported by Nutrient Flutter SDK in present mode.

Event Description
flutterPdfActivityOnPause Android only. Called when the PDF activity is paused.
pdfViewControllerWillDismiss iOS only. Called right before the dismissal of the PDF view controller.
pdfViewControllerDidDismiss iOS only. Called right after the dismissal of the PDF view controller.

Implementing a callback method

The example below shows how to implement a callback to get notified when the PDF activity is paused or when the PDF view controller is about to be dismissed.

// First implement the callback methods:

void flutterPdfActivityOnPauseHandler() {
  print('flutterPdfActivityOnPauseHandler');
}

void pdfViewControllerWillDismissHandler() {
  print('pdfViewControllerWillDismissHandler');
}

void pdfViewControllerDidDismissHandler() {
  print('pdfViewControllerDidDismissHandler');
}

// Next, register these callbacks with `Pspdfkit` once. This can be done in the `build` method of your widget.
Pspdfkit.flutterPdfActivityOnPause = () => flutterPdfActivityOnPauseHandler();
Pspdfkit.pdfViewControllerWillDismiss = () => pdfViewControllerWillDismissHandler();
Pspdfkit.pdfViewControllerDidDismiss = () => pdfViewControllerDidDismissHandler();

Now whenever any of these events happen, the corresponding method will trigger and print the relevant text.