PdfViewer constructor

const PdfViewer({
  1. Key? key,
  2. String? pdfPath,
  3. Uint8List? pdfData,
  4. bool initialSidebarOpen = false,
  5. double sidebarWidth = 160,
  6. double thumbnailHeight = 150,
  7. Color sidebarBackgroundColor = Colors.grey,
  8. BoxDecoration? selectedPageDecoration,
  9. bool showZoomHint = true,
  10. int initialPage = 1,
  11. bool showPageIndicator = true,
  12. void onPageChanged(
    1. int page
    )?,
  13. PanAxis panAxis = PanAxis.free,
  14. Color backgroundColor = Colors.grey,
  15. ValueChanged<double>? onScroll,
  16. void onError(
    1. Object error
    )?,
  17. Widget errorBuilder(
    1. BuildContext context,
    2. Object error
    )?,
  18. PdfSemanticLabels semanticLabels = const PdfSemanticLabels(),
  19. PdfViewerController? controller,
})

Creates a PdfViewer widget.

Provide exactly one PDF source:

  • pdfPath: a URL (starting with http:// or https://) or an asset path.
  • pdfData: the raw bytes of the PDF document.

The initialSidebarOpen parameter determines whether the thumbnail sidebar is initially visible.

The sidebarWidth parameter sets the width of the thumbnail sidebar.

The thumbnailHeight parameter sets the height of each thumbnail in the sidebar.

The sidebarBackgroundColor parameter sets the background color of the sidebar.

The selectedPageDecoration parameter sets the decoration for the selected page thumbnail.

The showZoomHint parameter determines whether to show an animated hint indicating that the user can pinch to zoom. Defaults to true.

The initialPage parameter sets the page shown on first open. Defaults to 1.

The showPageIndicator parameter shows a "current / total" overlay. Defaults to true.

The onPageChanged callback is called whenever the user navigates to a new page.

The panAxis parameter constrains panning (e.g. PanAxis.aligned locks each gesture to its dominant axis). Defaults to PanAxis.free.

The backgroundColor parameter sets the color behind and between the pages. Defaults to Colors.grey.

The onScroll callback reports the vertical scroll offset (in view pixels, 0 = top), e.g. to collapse surrounding chrome while reading.

The onError callback is invoked when the document fails to load or parse (network/asset error or corrupt/encrypted PDF).

The errorBuilder parameter customises the widget shown on failure. Defaults to a centred icon with a visible, screen-reader-announced message.

The semanticLabels parameter allows customising the labels announced by screen readers (VoiceOver / TalkBack). Defaults to English labels.

The controller parameter lets the caller drive the scroll/zoom position (e.g. go to a page, or forward a drag from a surrounding header). When null, the viewer creates its own.

Implementation

const PdfViewer({
  super.key,
  this.pdfPath,
  this.pdfData,
  this.initialSidebarOpen = false,
  this.sidebarWidth = 160,
  this.thumbnailHeight = 150,
  this.sidebarBackgroundColor = Colors.grey,
  this.selectedPageDecoration,
  this.showZoomHint = true,
  this.initialPage = 1,
  this.showPageIndicator = true,
  this.onPageChanged,
  this.panAxis = PanAxis.free,
  this.backgroundColor = Colors.grey,
  this.onScroll,
  this.onError,
  this.errorBuilder,
  this.semanticLabels = const PdfSemanticLabels(),
  this.controller,
}) : assert(
       pdfPath != null || pdfData != null,
       'Either pdfPath or pdfData must be provided.',
     ),
     assert(
       pdfPath == null || pdfData == null,
       'Provide only one of pdfPath or pdfData, not both.',
     );