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) {
  return SearchWidgetConnector(
      id: id,
      triggerQueryOnInit: true,
      subscribeTo: [
        KeysToSubscribe.Error,
        KeysToSubscribe.RequestPending,
        KeysToSubscribe.Results,
        KeysToSubscribe.Value,
        KeysToSubscribe.RecentSearches
      ],
      // properties to configure search component
      credentials: credentials,
      index: index,
      url: url,
      appbaseConfig: appbaseConfig,
      transformRequest: transformRequest,
      transformResponse: transformResponse,
      headers: headers,
      type: QueryType.search,
      react: react,
      queryFormat: queryFormat,
      dataField: dataField,
      categoryField: categoryField,
      categoryValue: categoryValue,
      nestedField: nestedField,
      from: from,
      size: size,
      sortBy: sortBy,
      aggregationField: aggregationField,
      aggregationSize: aggregationSize,
      after: after,
      includeNullValues: includeNullValues,
      includeFields: includeFields,
      excludeFields: excludeFields,
      fuzziness: fuzziness,
      searchOperators: searchOperators,
      highlight: highlight,
      highlightField: highlightField,
      customHighlight: customHighlight,
      interval: interval,
      aggregations: aggregations,
      missingLabel: missingLabel,
      showMissing: showMissing,
      enableSynonyms: enableSynonyms,
      selectAllLabel: selectAllLabel,
      pagination: pagination,
      queryString: queryString,
      defaultQuery: defaultQuery,
      customQuery: customQuery,
      beforeValueChange: beforeValueChange,
      onValueChange: onValueChange,
      onResults: onResults,
      onAggregationData: onAggregationData,
      onError: onError,
      onRequestStatusChange: onRequestStatusChange,
      onQueryChange: onQueryChange,
      enablePopularSuggestions: enablePopularSuggestions,
      maxPopularSuggestions: maxPopularSuggestions,
      showDistinctSuggestions: showDistinctSuggestions,
      preserveResults: preserveResults,
      clearOnQueryChange: clearOnQueryChange,
      value: query,
      distinctField: distinctField,
      distinctFieldConfig: distinctFieldConfig,
      compoundClause: compoundClause,
      builder: (context, searchController) {
        if (query != searchController.value) {
          // To fetch the suggestions
          searchController.setValue(query,
              options: Options(triggerDefaultQuery: true));
        }
        if (query.isEmpty) {
          if (enableRecentSearches == true) {
            // Fetch recent searches
            searchController.getRecentSearches(
                options: Options(stateChanges: false));
          }
        }
        // If query is empty then render recent searches
        if (query.isEmpty &&
            searchController.recentSearches?.isNotEmpty == true) {
          return getSuggestionList(
              context, searchController, searchController.recentSearches!);
        }
        final List<Suggestion> popularSuggestions = searchController
            .suggestions
            .where((suggestion) => suggestion.isPopularSuggestion)
            .toList();
        List<Suggestion> filteredSuggestions = [];
        // Only display relevant suggestions when query is not empty
        if (query.isNotEmpty) {
          filteredSuggestions = searchController.suggestions
              .where((suggestion) => !suggestion.isPopularSuggestion)
              .toList();
          // Limit the suggestions by size
          if (filteredSuggestions.length > this.size!) {
            filteredSuggestions.sublist(0, this.size);
          }
        }
        // Append popular suggestions at bottom
        if (popularSuggestions.isNotEmpty) {
          filteredSuggestions = [
            ...filteredSuggestions,
            ...popularSuggestions
          ];
        }
        return (popularSuggestions.isEmpty && filteredSuggestions.isEmpty)
            ? ((query.isNotEmpty && searchController.requestPending == false)
                ? Container(
                    child: Center(child: Text('No suggestions found')),
                  )
                : searchController.requestPending
                    ? Center(child: CircularProgressIndicator())
                    : Container())
            : getSuggestionList(
                context, searchController, filteredSuggestions);
      });
}