Skip to content

Instantly share code, notes, and snippets.

@DevKhalyd
Last active September 22, 2020 02:49
Show Gist options
  • Save DevKhalyd/72bfdc27f5420a0fc7a5fe74721ad278 to your computer and use it in GitHub Desktop.
Save DevKhalyd/72bfdc27f5420a0fc7a5fe74721ad278 to your computer and use it in GitHub Desktop.
Search Delegate Custom
import 'package:flutter/material.dart';
///This class customize the default class SearchDelegate to use less code in each implementation
///
///The `Custom` sufix refers to properties from this class
///
///`Constructor class`
///
/// ```dart
///SearchDelegateCustom(
/// {@required this.listOfData,
/// this.searchFieldLabel = 'Búscar',
/// this.buildActionsCustom,
/// this.buildLeadingCustom})
/// ```
///
abstract class SearchDelegateCustom<T> extends SearchDelegate<T> {
final String searchFieldLabel;
final List<Widget> buildActionsCustom;
final Widget buildLeadingCustom;
///The data to search
final List<T> listOfData;
SearchDelegateCustom(
{@required this.listOfData,
this.searchFieldLabel = 'Búscar',
this.buildActionsCustom,
this.buildLeadingCustom})
: assert(listOfData != null),
super(
searchFieldLabel: searchFieldLabel,
keyboardType: TextInputType.text);
///`Description:` Suggestions shown in the body of the search page while the user types a
/// query into the search field.
///
///
///Tipically this method should return a [ListView] with a [ListTile]
///
///
///When [onTap] is called [query] should be `updated with` the
///corresponding `suggestion` and the results page should be shown
/// by calling [showResults].
///
///* Copied from SearchDelegate docs
///
///This can be done. But if you rather make your own flow. You can use [close]
///method to return the value selected by the user
///
///`NOTE:`Suggestions and Results methods have similar behaviors. The same method can be used in both methods
Widget buildSuggestionsCustom(BuildContext context, List<T> listOfData);
///`Description:` The results shown after the user submits a search from the search page.
///
///Tipically this method should return a [ListView] with a [ListTile]
///
//////When [onTap] is called [close] should be called
/// with the selected result as argument.
///`NOTE:`Results and Suggestions methods have similar behaviors. The same method can be used in both methods
Widget buildResultsCustom(BuildContext context, List<T> listOfData);
@override
List<Widget> buildActions(BuildContext context) =>
buildActionsCustom ??
[
query.isNotEmpty
? IconButton(
icon: Icon(Icons.clear),
onPressed: () => query = '',
)
: Container(),
];
@override
Widget buildLeading(BuildContext context) =>
buildLeadingCustom ??
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => close(context, null),
);
@override
Widget buildResults(BuildContext context) =>
buildResultsCustom(context, listOfData);
@override
Widget buildSuggestions(BuildContext context) =>
buildSuggestionsCustom(context, listOfData);
}
//The code from above is enough to implement something good
//But if you want to use just the listOfData, buildResults and buildSuggestions params
//You can use the next solution or create your own one
///This class is used to create instances of SearchDelegate and reuse code
class _SearchDelegateUseCase<T> extends SearchDelegateCustom {
///These functions allow to display the data in the UI
BuildDelegateResultSuggestion buildResult, buildSuggestion;
List<T> listOfData;
_SearchDelegateUseCase(
this.buildResult, this.buildSuggestion, this.listOfData)
: assert(buildResult != null),
assert(buildSuggestion != null),
assert(listOfData != null),
super(
listOfData: listOfData,
);
@override
Widget buildResultsCustom(BuildContext context, List list) =>
buildResult(context, list);
@override
Widget buildSuggestionsCustom(BuildContext context, List list) =>
buildSuggestion(context, list);
}
///Allow to build the result o suggestion part
typedef Widget BuildDelegateResultSuggestion<T>(
BuildContext context, List<T> listOfData);
///Get the function that allow to call the SearchDelegate
///
///Also this allows reuse the code
mixin SearchMixin {
///Call this function to create a delegate customizer for the search
_SearchDelegateUseCase getDelegate<T>({
@required List<T> listOfData,
@required BuildDelegateResultSuggestion<T> result,
@required BuildDelegateResultSuggestion<T> suggestion,
}) {
return _SearchDelegateUseCase(result, suggestion, listOfData);
}
}
//Or you can use Provider to listen to the changes instead of listOfData
//You can access through the context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment