searchText method Null safety

Future<PdfTextSearchResult> searchText(
  1. String searchText,
  2. {TextSearchOption? searchOption}
)

Searches the given text in the document.

Returns the PdfTextSearchResult object using which the search navigation can be performed on the instances found.

  • text - required - The text to be searched in the document.
  • searchOption - optional - Defines the constants that specify the option for text search.

This example demonstrates how to search text in SfPdfViewer.

class MyAppState extends State<MyApp> {

late PdfViewerController _pdfViewerController;
late PdfTextSearchResult _searchResult;

@override
void initState() {
  _pdfViewerController = PdfViewerController();
  _searchResult = PdfTextSearchResult();
  super.initState();
}

void _showDialog(BuildContext context)
{
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Search Result'),
        content: Text('No more occurrences found. Would you like to continue to search from the beginning?'),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              _searchResult.nextInstance();
              Navigator.of(context).pop();
            },
              child: Text('YES'),
          ),
          FlatButton(
            onPressed: () {
              _searchResult.clear();
              Navigator.of(context).pop();
            },
            child: Text('NO'),
          ),
        ],
      );
    },
  );
}

@override
Widget build(BuildContext context) {
   return MaterialApp(
     home: Scaffold(
        appBar: AppBar(
          title: Text('Syncfusion Flutter PdfViewer'),
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.search,
                color: Colors.white,
              ),
              onPressed: () async {
                _searchResult = await _pdfViewerController.searchText(
                    'PDF', searchOption: TextSearchOption.caseSensitive);
               setState(() {});
             },
           ),
           Visibility(
             visible: _searchResult.hasResult,
             child: IconButton(
               icon: Icon(
                 Icons.clear,
                 color: Colors.white,
               ),
               onPressed: () {
                 setState(() {
                   _searchResult.clear();
                 });
               },
             ),
           ),
            Visibility(
              visible: _searchResult.hasResult,
              child: IconButton(
                icon: Icon(
                  Icons.navigate_before,
                  color: Colors.white,
                ),
                onPressed: () {
                  _searchResult.previousInstance();
                },
              ),
            ),
            Visibility(
              visible: _searchResult.hasResult,
              child: IconButton(
                icon: Icon(
                    Icons.navigate_next,
                    color: Colors.white,
                ),
                onPressed: () {
                  if (_searchResult.currentInstanceIndex ==
                      _searchResult.totalInstanceCount) {
                    _showDialog(context);
                  }
                  else {
                    _searchResult.nextInstance();
                  }
                },
              ),
            ),
          ],
        ),
        body:
        SfPdfViewer.asset(
            'assets/flutter-succinctly.pdf',
            controller: _pdfViewerController,
            searchTextHighlightColor: Colors.blue
        )
        )
    );
  }
}
'''

Implementation

Future<PdfTextSearchResult> searchText(String searchText,
    {TextSearchOption? searchOption}) async {
  _searchText = searchText;
  _textSearchOption = searchOption;
  notifyPropertyChangedListeners(property: 'searchText');
  return _pdfTextSearchResult;
}