getSuggestionList method

ListView getSuggestionList(
  1. BuildContext context,
  2. dynamic searchController,
  3. List<Suggestion> list
)

Implementation

ListView getSuggestionList(BuildContext context,
    SearchController searchController, List<Suggestion> list) {
  List<Widget> suggestionsList = list.map((suggestion) {
    void handleTap() {
      // Perform actions on suggestions tap
      searchController.setValue(suggestion.value,
          options: Options(triggerCustomQuery: true));
      this.query = suggestion.value;
      String? objectId;
      if (suggestion.source != null && suggestion.source!['_id'] is String) {
        objectId = suggestion.source!['_id'].toString();
      }
      if (objectId != null &&
          suggestion.clickId != null &&
          searchController.appbaseSettings?.recordAnalytics == true) {
        try {
          // Record click analytics
          searchController.recordClick({objectId: suggestion.clickId!},
              isSuggestionClick: true);
        } catch (e) {
          print(e);
        }
      }

      close(context, null);
    }

    return Container(
        alignment: Alignment.topLeft,
        height: 50,
        child: buildSuggestionItem != null
            ? buildSuggestionItem!(suggestion, handleTap)
            : Container(
                child: ListTile(
                  onTap: handleTap,
                  leading: suggestion.isRecentSearch
                      ? Icon(Icons.history)
                      : (suggestion.isPopularSuggestion)
                          ? Icon(Icons.trending_up)
                          : Icon(Icons.search),
                  title: Text(suggestion.label,
                      maxLines: 1, overflow: TextOverflow.ellipsis),
                  trailing: showAutoFill == true
                      ? IconButton(
                          icon: Icon(FeatherIcons.arrowUpLeft),
                          onPressed: () => {this.query = suggestion.value})
                      : null,
                ),
                decoration: new BoxDecoration(
                    border: new Border(
                        bottom: new BorderSide(
                            color: Color(0xFFC8C8C8), width: 0.5)))));
  }).toList();
  return ListView(
      padding: const EdgeInsets.all(8), children: suggestionsList);
}