search method

  1. @override
void search(
  1. String query
)
override

Searches every page for query (ignoring case; a phrase may wrap lines), jumping to the first match as soon as it's found. Pages are searched in the background: searchState reports the matches as they arrive.

Implementation

@override
void search(String query) {
  final search = searchController;
  final document = _document;
  final trimmed = query.trim();
  search._reset(trimmed.isEmpty ? null : trimmed);
  if (trimmed.isEmpty || document == null) {
    search.notifyListeners();
    return;
  }
  final generation = search._generation;
  search._searching = true;
  search.notifyListeners();
  () async {
    var anyText = false;
    for (var page = 0; page < document.pageCount; page++) {
      try {
        final (hasText, found) = await document.search(page, trimmed);
        if (isDisposed || generation != search._generation) return;
        anyText |= hasText;
        if (found.isEmpty) continue;
        search._add(found);
        if (search._current < 0) {
          search._current = 0;
          _view?.showMatch(search._matches.first);
        }
        search.notifyListeners();
      } catch (_) {
        if (isDisposed || generation != search._generation) return;
      }
    }
    search._searching = false;
    if (search._matches.isEmpty && _hasTextLayer == null) {
      _hasTextLayer = anyText;
      notifyListeners();
    }
    search.notifyListeners();
  }();
}