Undo and Redo actions on the annotations in Flutter PDF Viewer widget (Syncfusion)

11 Dec 20233 minutes to read

If you performed any undesired actions when adding, removing, or editing annotations, you can undo and redo the action to restore the previous state. This section will go through how to undo and redo the changes made to the annotations.

For desktop platforms such as Windows, macOS, and desktop web, you can use the following shortcut keys to perform the actions.

Action & Shortcut keys Windows macOS
Undo Ctrl + z Command + z
Redo Ctrl + y Command + y

You can perform the undo and redo operations in the SfPdfViewer by assigning the UndoHisotyController instance to the undoController property of the SfPdfViewer. The UndoHistoryController class contains the undo and redo methods to perform the undo and redo operations, respectively. The canUndo and canRedo properties are used to check whether the undo and redo operations can be performed, respectively. The following code example illustrates how to perform the undo and redo operations programmatically in the SfPdfViewer with the help of the UndoHistoryController class.

final UndoHistoryController _undoController = UndoHistoryController();

@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: Scaffold(
      appBar: AppBar(actions: [
        ValueListenableBuilder(
            valueListenable: _undoController,
            builder: (context, value, child) {
              return IconButton(
                onPressed:
                    _undoController.value.canUndo ? _undoController.undo : null,
                icon: const Icon(Icons.undo),
              );
            }),
        ValueListenableBuilder(
            valueListenable: _undoController,
            builder: (context, value, child) {
              return IconButton(
                onPressed:
                    _undoController.value.canRedo ? _undoController.redo : null,
                icon: const Icon(Icons.redo),
              );
            }),
      ]),
      body: SfPdfViewer.network(
        'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
        undoController: _undoController,
      ),
    ),
  );
}