Created
December 6, 2020 09:40
-
-
Save utkarsh-UK/bacabbdc6775cbd97cc84e7a13ce18fe to your computer and use it in GitHub Desktop.
This file contains 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
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
class ServiceSearchDelegate extends SearchDelegate<List<String>> { | |
@override | |
List<Widget> buildActions(BuildContext context) { | |
return [ | |
IconButton( | |
icon: Icon(Icons.search), | |
key: ValueKey('search'), | |
onPressed: () { | |
///dispatch search event and call searchServices() method. | |
_dispatchServiceSearchEvent(context); | |
// to show the result built in buildResults() method | |
showResults(context); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.clear), | |
key: ValueKey('clear'), | |
onPressed: () { | |
//clears the textfield | |
query = ''; | |
}, | |
) | |
]; | |
} | |
@override | |
Widget buildLeading(BuildContext context) { | |
return IconButton( | |
key: ValueKey('back'), | |
icon: AnimatedIcon( | |
icon: AnimatedIcons.menu_arrow, | |
progress: transitionAnimation, | |
), | |
onPressed: () { | |
close(context, null); //close search delegate. | |
}, | |
); | |
} | |
@override | |
Widget buildResults(BuildContext context) { | |
final textTheme = Theme.of(context).textTheme; | |
//build your own list view according to you state mngt. | |
return query.isEmpty | |
? SizedBox.shrink() | |
: BlocBuilder<ServiceBloc, ServiceState>( | |
builder: (context, state) { | |
List<String> services = []; | |
if (state is ServiceSearchLoading) | |
return Center(child: Text('Searching for services...', style: textTheme.headline3, key: ValueKey('searching'))); | |
else if (state is ServiceSearchLoaded) services = state.searchedServices; | |
return _buildSearchText(textTheme, services); | |
}, | |
); | |
} | |
@override | |
Widget buildSuggestions(BuildContext context) { | |
final textTheme = Theme.of(context).textTheme; | |
if (query.isEmpty) return Center(child: Text('Search services by name...', style: textTheme.headline3)); | |
return Container(); | |
} | |
Widget _buildSearchText(TextTheme textTheme, List<String> services) { | |
if (services.isEmpty) | |
return Center(child: Text('No services found with matching name "$query".', style: textTheme.headline3)); | |
return ListView.builder( | |
itemCount: services.length, | |
itemBuilder: (context, index) => ListTile( | |
onTap: () { | |
close(context, services); | |
// navigate to service detail screen. | |
}, | |
title: Text( | |
services[index], | |
style: textTheme.headline3, | |
), | |
), | |
); | |
} | |
void _dispatchServiceSearchEvent(BuildContext context) { | |
//add event for search | |
//Change this to your own implementation of bloc or provider. | |
BlocProvider.of<SquadBloc>(context).add(SearchServiceEvent(index: 'services', query: query.trim().toLowerCase())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment