Food Delivery App 20 - Implementing Firebase Search and Local Search
Using the search Delegate
SearchDelegate
is a pre-defined class inside the material.dart. This
class provides us a predesigned search delegate. This class uses the
following method to control the various operation to be performed to
search for various items.
You can read about the method from the following link:
buildAction method
This method returns the list of widgets to display after the search query in the AppBar.
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.close),
onPressed: () {
close(context, null);
},
),
];
}
close(context, null)
method is used to close the searchDelegate.
buildLeading method
Return a leading widget that is displayed before the query.
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
close(context, null);
},
);
}
buildResult method
This method is used to build the result based on the keyword entered by the user.
Widget buildResults(BuildContext context) {
return Center(
child: Text(
query,
style: TextStyle(
color: Colors.blue, fontWeight: FontWeight.w900, fontSize: 30),
),
);
}
buildSuggestions method
This method builds the suggestion list base on the keyword entered by the user as a query.
Widget buildSuggestions(BuildContext context) {
List<Item> items = [
Item(title: 'apple'),
Item(title: 'mango'),
Item(title: 'banana'),
Item(title: 'pineapple'),
Item(title: 'orange'),
Item(title: 'oranges'),
];
List<Item> suggestionList = query.isEmpty
? items
: items.where((element) => element.title.startsWith(query)).toList();
return suggestionList.isEmpty
? Text("no result found")
: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(suggestionList[index].title),
onTap: () {
showResults(context);
},
);
},
itemCount: suggestionList.length,
);
}
Comments
Post a Comment