Skip to content

Commit

Permalink
PetShop: Implement search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ujwalp15 committed Nov 28, 2020
1 parent 2247199 commit 9ca87a9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 46 deletions.
1 change: 1 addition & 0 deletions assets/images/noSearch.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 83 additions & 46 deletions lib/screens/tab_screens/search_screens/search_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ class SearchScreen extends StatefulWidget {

class _SearchScreenState extends State<SearchScreen>
with SingleTickerProviderStateMixin {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final PageStorageBucket searchBucket = PageStorageBucket();

// TextEditingController for searching products
TextEditingController _searchController = TextEditingController();
Iterable<ProdProducts> searchList = [];
var prods;

TabController _tabController;
final _tabItems = [
"Search",
"All",
"Dogs",
"Cats",
"Fish",
"Birds",
"Reptiles",
"Others",
];

@override
void initState() {
checkInternetConnectivity().then((value) => {
Expand All @@ -66,41 +86,62 @@ class _SearchScreenState extends State<SearchScreen>
vsync: this,
);

// Add listener to searchController
_searchController.addListener((_onSearchChanged));
super.initState();
}

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final PageStorageBucket searchBucket = PageStorageBucket();
@override
void dispose() {
_searchController.removeListener((_onSearchChanged));
_searchController.dispose();
_tabController.dispose();
super.dispose();
}

TabController _tabController;
final _tabItems = [
"All",
"Dogs",
"Cats",
"Fish",
"Birds",
"Reptiles",
"Others",
];
// Add callback which is called whenever a text is added to search field
_onSearchChanged() {
searchResultsList(prods);
}

// Search for the text and match with products
searchResultsList(List<ProdProducts> allList) {
List<ProdProducts> showResults = [];

if (_searchController.text != "") {
for (var prod in allList) {
var title = prod.name.toLowerCase();
if (title.contains(_searchController.text.toLowerCase())) {
showResults.add(prod);
}
}
} else {
showResults = allList;
}
setState(() {
searchList = showResults;
});
}

@override
Widget build(BuildContext context) {
ProductsNotifier productsNotifier = Provider.of<ProductsNotifier>(context);
var prods = productsNotifier.productsList;
prods = productsNotifier.productsList;

CartNotifier cartNotifier = Provider.of<CartNotifier>(context);
var cartList = cartNotifier.cartList;
var cartProdID = cartList.map((e) => e.productID);

//Tab Items
final _tabBody = [
buildAllBody(prods, cartProdID),
buildDogBody(prods, cartProdID),
buildCatBody(prods, cartProdID),
buildDogBody(prods, cartProdID),
buildCatBody(prods, cartProdID),
buildDogBody(prods, cartProdID),
buildCatBody(prods, cartProdID),
buildSearchBody(prods, cartProdID),
buildTypeBody(prods, cartProdID, "all"),
buildTypeBody(prods, cartProdID, "cat"),
buildTypeBody(prods, cartProdID, "dog"),
buildTypeBody(prods, cartProdID, "cat"),
buildTypeBody(prods, cartProdID, "dog"),
buildTypeBody(prods, cartProdID, "cat"),
buildTypeBody(prods, cartProdID, "dog"),
];

return Scaffold(
Expand All @@ -118,7 +159,7 @@ class _SearchScreenState extends State<SearchScreen>
height: 40.0,
child: searchTextField(
true,
null,
_searchController,
null,
"Search for products...",
null,
Expand Down Expand Up @@ -178,46 +219,42 @@ class _SearchScreenState extends State<SearchScreen>
}

//Build Tabs
Widget buildAllBody(prods, cartProdID) {
Iterable<ProdProducts> all = prods.reversed;
Widget buildTypeBody(prods, cartProdID, type) {
Iterable<ProdProducts> typeProds =
type == "all" ? prods.reversed : prods.where((e) => e.pet == type);
CartNotifier cartNotifier =
Provider.of<CartNotifier>(context, listen: false);
ProductsNotifier productsNotifier =
Provider.of<ProductsNotifier>(context, listen: false);

return SearchTabWidget(
prods: all,
prods: typeProds,
cartNotifier: cartNotifier,
productsNotifier: productsNotifier,
cartProdID: cartProdID,
);
}

Widget buildDogBody(prods, cartProdID) {
Iterable<ProdProducts> dog = prods.where((e) => e.pet == "dog");
// Search Tab
Widget buildSearchBody(prods, cartProdID) {
CartNotifier cartNotifier =
Provider.of<CartNotifier>(context, listen: false);
ProductsNotifier productsNotifier = Provider.of<ProductsNotifier>(context);

return SearchTabWidget(
prods: dog,
cartNotifier: cartNotifier,
productsNotifier: productsNotifier,
cartProdID: cartProdID,
);
}

Widget buildCatBody(prods, cartProdID) {
Iterable<ProdProducts> cat = prods.where((e) => e.pet == "cat");
CartNotifier cartNotifier =
Provider.of<CartNotifier>(context, listen: false);
ProductsNotifier productsNotifier = Provider.of<ProductsNotifier>(context);

return SearchTabWidget(
prods: cat,
cartNotifier: cartNotifier,
productsNotifier: productsNotifier,
cartProdID: cartProdID,
);
if (searchList.isEmpty) {
return emptyScreen(
"assets/images/noSearch.svg",
"No Search Query",
"Type a product name in the searchbar above.",
);
} else {
return SearchTabWidget(
key: UniqueKey(),
prods: searchList,
cartNotifier: cartNotifier,
productsNotifier: productsNotifier,
cartProdID: cartProdID,
);
}
}
}

1 comment on commit 9ca87a9

@thenifemi
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work💯

Please sign in to comment.