Last active
June 29, 2021 06:02
-
-
Save dander11/4e1e4bfc751ff1573c37d9fcd5bca072 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomSearchDelegate extends SearchDelegate { | |
| @override | |
| List<Widget> buildActions(BuildContext context) { | |
| return [ | |
| IconButton( | |
| icon: Icon(Icons.clear), | |
| onPressed: () { | |
| query = ''; | |
| }, | |
| ), | |
| ]; | |
| } | |
| @override | |
| Widget buildLeading(BuildContext context) { | |
| return IconButton( | |
| icon: Icon(Icons.arrow_back), | |
| onPressed: () { | |
| close(context, null); | |
| }, | |
| ); | |
| } | |
| @override | |
| Widget buildResults(BuildContext context) { | |
| if (query.length < 3) { | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Center( | |
| child: Text( | |
| "Search term must be longer than two letters.", | |
| ), | |
| ) | |
| ], | |
| ); | |
| } | |
| //Add the search term to the searchBloc. | |
| //The Bloc will then handle the searching and add the results to the searchResults stream. | |
| //This is the equivalent of submitting the search term to whatever search service you are using | |
| InheritedBlocs.of(context) | |
| .searchBloc | |
| .searchTerm | |
| .add(query); | |
| return Column( | |
| children: <Widget>[ | |
| //Build the results based on the searchResults stream in the searchBloc | |
| StreamBuilder( | |
| stream: InheritedBlocs.of(context).searchBloc.searchResults, | |
| builder: (context, AsyncSnapshot<List<Result>> snapshot) { | |
| if (!snapshot.hasData) { | |
| return Column( | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Center(child: CircularProgressIndicator()), | |
| ], | |
| ); | |
| } else if (snapshot.data.length == 0) { | |
| return Column( | |
| children: <Widget>[ | |
| Text( | |
| "No Results Found.", | |
| ), | |
| ], | |
| ); | |
| } else { | |
| var results = snapshot.data; | |
| return ListView.builder( | |
| itemCount: results.length, | |
| itemBuilder: (context, index) { | |
| var result = results[index]; | |
| return ListTile( | |
| title: Text(result.title), | |
| ); | |
| }, | |
| ); | |
| } | |
| }, | |
| ), | |
| ], | |
| ); | |
| } | |
| @override | |
| Widget buildSuggestions(BuildContext context) { | |
| // This method is called everytime the search term changes. | |
| // If you want to add search suggestions as the user enters their search term, this is the place to do that. | |
| return Column(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment