didUpdateWidget method

  1. @override
void didUpdateWidget(
  1. covariant SfPdfViewer oldWidget
)
override

Called whenever the widget configuration changes.

If the parent widget rebuilds and requests that this location in the tree update to display a new widget with the same runtimeType and Widget.key, the framework will update the widget property of this State object to refer to the new widget and then call this method with the previous widget as an argument.

Override this method to respond when the widget changes (e.g., to start implicit animations).

The framework always calls build after calling didUpdateWidget, which means any calls to setState in didUpdateWidget are redundant.

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

Implementations of this method should start with a call to the inherited method, as in super.didUpdateWidget(oldWidget).

See the discussion at Element.rebuild for more information on when this method is called.

Implementation

@override
void didUpdateWidget(SfPdfViewer oldWidget) {
  super.didUpdateWidget(oldWidget);
  final double oldZoomLevel = _pdfViewerController.zoomLevel;
  final Offset oldScrollOffset = _pdfViewerController.scrollOffset;
  // Handle all cases of needing to dispose and initialize
  // _pdfViewerController.
  if (oldWidget.controller == null) {
    if (widget.controller != null) {
      _pdfViewerController._removeListener(_handleControllerValueChange);
      _pdfViewerController._reset();
      _pdfViewerController = widget.controller!;
      _pdfViewerController._addListener(_handleControllerValueChange);
    }
  } else {
    if (widget.controller == null) {
      _pdfViewerController._removeListener(_handleControllerValueChange);
      _pdfViewerController = PdfViewerController();
      _pdfViewerController._addListener(_handleControllerValueChange);
    } else if (widget.controller != oldWidget.controller) {
      _pdfViewerController._removeListener(_handleControllerValueChange);
      _pdfViewerController = widget.controller!;
      _pdfViewerController._addListener(_handleControllerValueChange);
    }
  }
  if (oldWidget.undoController != widget.undoController) {
    _changeTracker.resetController();
    _undoController?.dispose();
    _changeTracker.undoController = _effectiveUndoController;
  }
  _compareDocument(oldWidget._provider.getPdfBytes(context),
      widget._provider.getPdfBytes(context), oldWidget.password);
  if (oldWidget.pageLayoutMode != widget.pageLayoutMode) {
    _transformationController = TransformationControllerExt();
    _updateOffsetOnLayoutChange(
        oldZoomLevel, oldScrollOffset, oldWidget.pageLayoutMode);
  }
  _scrollDirection = widget.scrollDirection != null
      ? widget.scrollDirection!
      : (widget.pageLayoutMode == PdfPageLayoutMode.single
          ? PdfScrollDirection.horizontal
          : PdfScrollDirection.vertical);
  Future<void>.delayed(Durations.short4, _getTileImage);

  if (oldWidget.canShowTextSelectionMenu != widget.canShowTextSelectionMenu) {
    if (widget.canShowTextSelectionMenu) {
      Future<void>.delayed(Duration.zero, _checkPositionOfTextSelectionMenu);
    } else {
      _hideTextSelectionMenu();
    }
  }
}