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) {
  // print("local languageCode is "+Localizations.localeOf(context).languageCode);
  return FutureBuilder(
    future: query == ""
        ? null
        : apiClient!.fetchSuggestions(
            query, Localizations.localeOf(context).languageCode),
    builder: (context,AsyncSnapshot<List<Suggestion>?> snapshot) {
      switch (snapshot.connectionState) {
        case ConnectionState.none:
         return Container(
            padding: const EdgeInsets.all(16.0),
            child:const Text('Enter your address'),
          );
        case ConnectionState.active:
        case ConnectionState.waiting:
        return const Center(
              child: CircularProgressIndicator(
                valueColor:
                     AlwaysStoppedAnimation<Color>(AppColors.black),
              ),
            );
        case ConnectionState.done:
        if (snapshot.hasError) {
          return CustomText(
              text: snapshot.error.toString(),
              font: 20,
              txtColor: AppColors.black,
            );
        }else{
        return  query == '' ? Container(
            padding: const EdgeInsets.all(16.0),
            child:const Text('Enter your address'),
          ) : ListView.builder(
                itemBuilder: (context, index) => ListTile(
                  title:
                      Text((snapshot.data![index]).description),
                  onTap: () {
                    close(context, snapshot.data![index]);
                  },
                ),
                itemCount: snapshot.data!.length,
              );
        }
      }
    }
  );
}