buildSuggestions method

  1. @override
Widget buildSuggestions(
  1. BuildContext context
)
override

Suggestions shown in the body of the search page while the user types a query into the search field.

The delegate method is called whenever the content of query changes. The suggestions should be based on the current query string. If the query string is empty, it is good practice to show suggested queries based on past queries or the current context.

Usually, this method will return a ListView with one ListTile per suggestion. When ListTile.onTap is called, query should be updated with the corresponding suggestion and the results page should be shown by calling showResults.

Implementation

@override
Widget buildSuggestions(BuildContext context) {
  // Calles the 'onQueryUpdated' functions at the start of the operation
  onQueryUpdate?.call(query);

  // Deletes possible blank spaces & converts the string to lower case
  final cleanQuery = query.toLowerCase().trim();

  // Using the [filter] method, filters through the [items] list
  // in order to select matching items
  final result = items
      .where(
        // First we collect all [String] representation of each [item]
        (item) => filter(item)
            // Then, transforms all results to lower case letters
            .map((value) => value?.toLowerCase().trim())
            // Finally, checks wheters any coincide with the cleaned query
            // Checks wheter the [startsWith] or [endsWith] are 'true'
            .any((value) => _filterByValue(query: cleanQuery, value: value)),
      )
      .toList();

  if (sort != null) {
    result.sort(sort);
  }

  // Builds a list with all filtered items
  // if query and result list are not empty
  return cleanQuery.isEmpty && !showItemsOnEmpty
      ? suggestion
      : result.isEmpty
          ? failure
          : ListView(
              children: result.map(builder).toList(),
            );
}