Created
November 2, 2019 06:57
-
-
Save kishansinhparmar/642cbb3ada39d41131a456e2b19328d9 to your computer and use it in GitHub Desktop.
Products endpoint result screen
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 'dart:io'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_renoon/common/mycolors.dart'; | |
import 'package:flutter_renoon/home/model/ProductFilters.dart'; | |
import 'package:flutter_renoon/home/model/ProductsResponse.dart'; | |
import 'package:flutter_renoon/home/rebuildtest/product_filter_param.dart'; | |
import 'package:flutter_renoon/login/signup_screen.dart'; | |
import 'package:flutter_renoon/myapp_screen.dart'; | |
import 'package:flutter_renoon/renoon_widgets/follow_widget.dart'; | |
import 'package:flutter_renoon/renoon_widgets/grid_product_widget.dart'; | |
import 'package:flutter_renoon/renoon_widgets/product_element_filter_widget.dart'; | |
import 'package:flutter_renoon/renoon_widgets/sort_button_widget.dart'; | |
import 'package:flutter_renoon/renoon_widgets/start_widget.dart'; | |
import 'package:flutter_renoon/search/bloc/products_endpoint_result_bloc.dart'; | |
import 'package:flutter_renoon/search/filter_screen.dart'; | |
import 'package:flutter_renoon/search/product_details_screen.dart'; | |
import 'package:flutter_renoon/utils/const.dart'; | |
import 'package:flutter_renoon/utils/my_flutter_app_icons.dart'; | |
import 'package:flutter_renoon/utils/scale_transition.dart'; | |
class ProductsEndPointResult extends StatefulWidget { | |
ProductFilters productFilters; | |
ProductsEndPointResult(this.productFilters); | |
@override | |
_ProductsEndPointResultState createState() => _ProductsEndPointResultState(); | |
} | |
class _ProductsEndPointResultState extends State<ProductsEndPointResult> | |
with SingleTickerProviderStateMixin { | |
bool isDataLoaded = false; | |
bool _isFeatured = true; | |
bool _isNewest = false; | |
bool _isPriceLow = false; | |
bool _isPriceHigh = false; | |
bool _isPopup = true; | |
bool _isChecked = false; | |
int _currentIndex = 1; | |
TabController _tabController; | |
List<PElement> pElements = List<PElement>(); | |
bool _isLoginUser = false; | |
var selectedLocationIndices = Set<int>(); | |
var favProductIds = List<String>(); | |
bool fromTheSamePage = true; | |
int masterLowerSliderPrice = 0; | |
int masterHigherSliderPrice = 0; | |
ProductsEndPointResultBloc productEndPointResultBloc; | |
ScrollController _scrollController = ScrollController(); | |
// List<Product> _gridProducts = List<Product>(); | |
ProductsResponse _productsResponse = ProductsResponse(); | |
void _toggleState(int index, String productId) { | |
if (favProductIds.contains(productId)) { | |
selectedLocationIndices.remove(index); | |
productEndPointResultBloc.removeToFav(productId, FollowStatus.INACTIVE); | |
} else { | |
selectedLocationIndices.add(index); | |
productEndPointResultBloc.addToFav(productId, FollowStatus.INACTIVE); | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
print("ProductsEndPointResult: initState"); | |
productEndPointResultBloc = ProductsEndPointResultBloc(); | |
_productsResponse.products = List<Product>(); | |
_productsResponse.materials = List<PMaterials>(); | |
_productsResponse.brands = List<PBrands>(); | |
_productsResponse.categories = List<PCategories>(); | |
_productsResponse.colors = List<PColors>(); | |
_productsResponse.sizes = List<PSizes>(); | |
_isLoginUser = productEndPointResultBloc.isUserLogin(); | |
if (_isLoginUser) { | |
productEndPointResultBloc.fetchMyFavProductIds(); | |
productEndPointResultBloc.productIdsStream.listen((wishList) { | |
favProductIds = wishList.favProducts; | |
print("FavProids: $favProductIds"); | |
productEndPointResultBloc | |
.fetchProductsByFilter(widget.productFilters.filterStr); | |
}); | |
} else { | |
//Fetch filter products | |
productEndPointResultBloc | |
.fetchProductsByFilter(widget.productFilters.filterStr); | |
} | |
_tabController = new TabController(length: 4, vsync: this); | |
_tabController.addListener(() { | |
print("Tab swipe"); | |
setState(() { | |
_currentIndex = _tabController.index; | |
}); | |
}); | |
_scrollController.addListener(() { | |
if (_scrollController.position.pixels == | |
_scrollController.position.maxScrollExtent) { | |
print("scroll listner"); | |
_loadMore(); | |
} | |
}); | |
} | |
_loadMore() { | |
print("Load more data..."); | |
productEndPointResultBloc.fetchMoreProductsByFilter( | |
widget.productFilters.filterStr, _productsResponse.products.length); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Scaffold( | |
appBar: PreferredSize( | |
child: StreamBuilder<bool>( | |
initialData: false, | |
stream: productEndPointResultBloc.productsStateStream, | |
builder: (context, snapshot) { | |
return _buildToolbar( | |
widget.productFilters.title != null | |
? widget.productFilters.title | |
: "Search", | |
snapshot.hasData || widget.productFilters.onceVar, | |
); | |
}), | |
preferredSize: Size(MediaQuery.of(context).size.width, 60), | |
), | |
body: StreamBuilder<ProductsResponse>( | |
stream: productEndPointResultBloc.productsStream, | |
builder: (context, snapshot) { | |
if (snapshot.data != null && snapshot.hasData) { | |
//Once screen loaded | |
if (widget.productFilters.count == 0 && | |
snapshot.data.lowerPrice != null && | |
snapshot.data.upperPrice != null && | |
masterLowerSliderPrice == 0 && | |
masterHigherSliderPrice == 0) { | |
masterLowerSliderPrice = snapshot.data.lowerPrice; | |
masterHigherSliderPrice = snapshot.data.upperPrice; | |
} else if (widget.productFilters.count == 0 && | |
snapshot.data.lowerPrice != null && | |
snapshot.data.upperPrice != null && | |
masterLowerSliderPrice != 0 && | |
masterHigherSliderPrice != 0) { | |
masterLowerSliderPrice = | |
snapshot.data.lowerPrice < masterLowerSliderPrice | |
? snapshot.data.lowerPrice | |
: masterLowerSliderPrice; | |
masterHigherSliderPrice = | |
snapshot.data.upperPrice > masterHigherSliderPrice | |
? snapshot.data.upperPrice | |
: masterHigherSliderPrice; | |
} else if (widget.productFilters.count != 0 && | |
snapshot.data.lowerPrice != null && | |
snapshot.data.upperPrice != null && | |
masterLowerSliderPrice != 0 && | |
masterHigherSliderPrice != 0) { | |
masterLowerSliderPrice = | |
snapshot.data.lowerPrice < masterLowerSliderPrice | |
? snapshot.data.lowerPrice | |
: masterLowerSliderPrice; | |
masterHigherSliderPrice = | |
snapshot.data.upperPrice > masterHigherSliderPrice | |
? snapshot.data.upperPrice | |
: masterHigherSliderPrice; | |
} | |
widget.productFilters.lowerPrice = | |
snapshot.data.lowerPrice == null | |
? masterLowerSliderPrice | |
: snapshot.data.lowerPrice; | |
widget.productFilters.upperPrice = | |
snapshot.data.upperPrice == null | |
? masterHigherSliderPrice | |
: snapshot.data.upperPrice; | |
if (snapshot.data.products.length > 0) { | |
_productsResponse.products.addAll(snapshot.data.products); | |
} | |
//Temp code | |
_productsResponse.materials.addAll(snapshot.data.materials); | |
_productsResponse.brands.addAll(snapshot.data.brands); | |
_productsResponse.categories.addAll(snapshot.data.categories); | |
_productsResponse.colors.addAll(snapshot.data.colors); | |
_productsResponse.sizes.addAll(snapshot.data.sizes); | |
if (snapshot.data.total != null && snapshot.data.total > 0) { | |
saveTopTwoElementsWithSelected(snapshot.data); | |
} | |
return Stack( | |
children: <Widget>[ | |
//Main search result | |
ListView( | |
controller: _scrollController, | |
children: <Widget>[ | |
_productsTopElements(false), | |
//Products endpoint result count | |
Padding( | |
padding: | |
const EdgeInsets.fromLTRB(0.0, 4.0, 0.0, 4.0), | |
child: Align( | |
alignment: Alignment.center, | |
child: Text( | |
_productsResponse.products.length > 0 | |
? "${_productsResponse.products.length} results" | |
: "0 result", | |
style: Theme.of(context).textTheme.title.copyWith( | |
color: kBlack, | |
fontSize: 14, | |
), | |
), | |
), | |
), | |
//Grid products will show here. | |
_productsResponse.products.length > 0 | |
? _buildProductGrid(false) | |
: Padding( | |
padding: const EdgeInsets.only(top: 32), | |
child: Center( | |
child: Column( | |
crossAxisAlignment: | |
CrossAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
"Nothing here yet.", | |
style: TextStyle(fontSize: 15), | |
), | |
Text( | |
"Search something else.", | |
style: TextStyle(fontSize: 15), | |
), | |
SizedBox( | |
height: 32, | |
), | |
StartWidget( | |
title: "SEARCH", | |
myOnTap: () { | |
widget.productFilters.colors.clear(); | |
widget.productFilters.categories | |
.clear(); | |
widget.productFilters.materials | |
.clear(); | |
widget.productFilters.materials | |
.clear(); | |
widget.productFilters.brands.clear(); | |
widget.productFilters.sizes.clear(); | |
widget.productFilters.countries | |
.clear(); | |
widget.productFilters.count = 0; | |
Navigator.of(context).pop(); | |
}, | |
isLeadingIcon: true, | |
leadingIcon: Icon( | |
RenoonIcons.magnifying_glass, | |
size: 18, | |
color: kMaroon, | |
), | |
), | |
], | |
), | |
), | |
), | |
SizedBox(height: 32), | |
//Grid search items | |
], | |
), | |
// buildPopupStack(), | |
_productsResponse.products.length > 0 | |
? buildSortPopupMenu() | |
: SizedBox(), | |
], | |
); | |
} else { | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
}), | |
bottomNavigationBar: Container( | |
height: 54, | |
child: BottomAppBar( | |
child: BottomNavigationBar( | |
fixedColor: kBlack, | |
currentIndex: _currentIndex, | |
type: BottomNavigationBarType.fixed, | |
items: _bottomTabs(), | |
showSelectedLabels: true, | |
showUnselectedLabels: false, | |
onTap: (index) { | |
setState(() { | |
Navigator.of(context).pushReplacement( | |
MaterialPageRoute(builder: (context) => MyApp(index))); | |
_currentIndex = index; | |
_tabController.index = index; | |
/*if (index == 0 || index == 1) { | |
_showToolbar = true; | |
_favTabStyle = false; | |
_toolbarTitle = "renoon"; | |
} else if (index == 2) { | |
_showToolbar = true; | |
_favTabStyle = true; | |
_toolbarTitle = "Favorite"; | |
} else { | |
_showToolbar = false; | |
_toolbarTitle = "renoon"; | |
} | |
print("Current tab change ${_tabController.previousIndex} -> ${_tabController.index}");*/ | |
}); | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
Align buildSortPopupMenu() { | |
return Align( | |
alignment: Alignment.bottomCenter, | |
child: Container( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
_popupCard(), | |
SortButtonWidget( | |
myOnTap: () { | |
_setPopupVisibility(); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
_tabController.dispose(); | |
_scrollController.dispose(); | |
pElements.clear(); | |
productEndPointResultBloc.dispose(); | |
print("ProductEndPointResult: dispose"); | |
} | |
void _setSelected() { | |
setState(() { | |
if (_isChecked == true) { | |
_isChecked = false; | |
} else { | |
_isChecked = true; | |
} | |
}); | |
} | |
void _setPopupVisibility() { | |
setState(() { | |
if (_isPopup == true) { | |
_isPopup = false; | |
} else { | |
_isPopup = true; | |
} | |
}); | |
} | |
_bottomTabs() { | |
List<BottomNavigationBarItem> list = new List<BottomNavigationBarItem>(); | |
list.add(BottomNavigationBarItem( | |
icon: Icon( | |
RenoonIcons.r_brand, | |
size: 20, | |
color: kBlack, | |
), | |
title: Text("Home"))); | |
list.add(BottomNavigationBarItem( | |
icon: Icon( | |
RenoonIcons.magnifying_glass, | |
size: 20, | |
color: kBlack, | |
), | |
title: Text("Search"))); | |
list.add(BottomNavigationBarItem( | |
icon: Icon( | |
RenoonIcons.star, | |
size: 20, | |
color: kBlack, | |
), | |
title: Text("Favourites"))); | |
list.add(BottomNavigationBarItem( | |
icon: Icon( | |
RenoonIcons.user, | |
size: 20, | |
color: kBlack, | |
), | |
title: Text("Account"))); | |
return list; | |
} | |
saveTopTwoElementsWithSelectedWithInfinitive() { | |
print("saveTopTwoElements"); | |
pElements.clear(); | |
if (pElements.length == 0) { | |
//This function will display top 2 products elements from these 4 elements "colors", "categories", "materials", "brands" | |
//Adding materials | |
if (_productsResponse.materials.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterMaterial)) { | |
_productsResponse.materials.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.materials = productsResponse.materials.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.materials[0].id.id, | |
type: kFilterMaterial, | |
text: _productsResponse.materials[0].id.vanityName, | |
count: _productsResponse.materials[0].count, | |
isSelected: _productsResponse.materials[0].selected, | |
)); | |
if (_productsResponse.materials.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.materials[1].id.id, | |
type: kFilterMaterial, | |
text: _productsResponse.materials[1].id.vanityName, | |
count: _productsResponse.materials[1].count, | |
isSelected: _productsResponse.materials[1].selected, | |
)); | |
} | |
} | |
//Adding brands | |
if (_productsResponse.brands.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterBrand)) { | |
_productsResponse.brands.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.brands = productsResponse.brands.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.brands[0].id.id, | |
type: kFilterBrand, | |
text: _productsResponse.brands[0].id.vanityName, | |
count: _productsResponse.brands[0].count, | |
isSelected: _productsResponse.brands[0].selected, | |
)); | |
if (_productsResponse.brands.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.brands[1].id.id, | |
type: kFilterBrand, | |
text: _productsResponse.brands[1].id.vanityName, | |
count: _productsResponse.brands[1].count, | |
isSelected: _productsResponse.brands[1].selected, | |
)); | |
} | |
} | |
//Adding colors | |
if (_productsResponse.colors.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterColor)) { | |
_productsResponse.colors.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.colors = productsResponse.colors.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.colors[0].id, | |
type: kFilterColor, | |
text: _productsResponse.colors[0].id, | |
count: _productsResponse.colors[0].count, | |
isSelected: _productsResponse.colors[0].selected, | |
)); | |
if (_productsResponse.colors.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.colors[1].id, | |
type: kFilterColor, | |
text: _productsResponse.colors[1].id, | |
count: _productsResponse.colors[1].count, | |
isSelected: _productsResponse.colors[1].selected, | |
)); | |
} | |
} | |
//Adding categories | |
if (_productsResponse.categories.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterCategory)) { | |
_productsResponse.categories.sort((b, a) => a.count.compareTo(b.count)); | |
_productsResponse.categories = | |
_productsResponse.categories.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.categories[0].id, | |
type: kFilterCategory, | |
text: _productsResponse.categories[0].id, | |
count: _productsResponse.categories[0].count, | |
isSelected: _productsResponse.categories[0].selected, | |
)); | |
if (_productsResponse.categories.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.categories[1].id, | |
type: kFilterCategory, | |
text: _productsResponse.categories[1].id, | |
count: _productsResponse.categories[1].count, | |
isSelected: _productsResponse.categories[1].selected, | |
)); | |
} | |
} | |
} else { | |
//Adding materials | |
if (_productsResponse.materials.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterMaterial)) { | |
_productsResponse.materials.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.materials = productsResponse.materials.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.materials[0].id.id, | |
type: kFilterMaterial, | |
text: _productsResponse.materials[0].id.vanityName, | |
count: _productsResponse.materials[0].count, | |
isSelected: _productsResponse.materials[0].selected, | |
)); | |
if (_productsResponse.materials.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.materials[1].id.id, | |
type: kFilterMaterial, | |
text: _productsResponse.materials[1].id.vanityName, | |
count: _productsResponse.materials[1].count, | |
isSelected: _productsResponse.materials[1].selected, | |
)); | |
} | |
} | |
//Adding brands | |
if (_productsResponse.brands.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterBrand)) { | |
_productsResponse.brands.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.brands = productsResponse.brands.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.brands[0].id.id, | |
type: kFilterBrand, | |
text: _productsResponse.brands[0].id.vanityName, | |
count: _productsResponse.brands[0].count, | |
isSelected: _productsResponse.brands[0].selected, | |
)); | |
if (_productsResponse.brands.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.brands[1].id.id, | |
type: kFilterBrand, | |
text: _productsResponse.brands[1].id.vanityName, | |
count: _productsResponse.brands[1].count, | |
isSelected: _productsResponse.brands[1].selected, | |
)); | |
} | |
} | |
//Adding colors | |
if (_productsResponse.colors.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterColor)) { | |
_productsResponse.colors.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.colors = productsResponse.colors.reversed.toList(); | |
_productsResponse.colors.forEach((c) => {}); | |
pElements.add(PElement( | |
id: _productsResponse.colors[0].id, | |
type: kFilterColor, | |
text: _productsResponse.colors[0].id, | |
count: _productsResponse.colors[0].count, | |
isSelected: _productsResponse.colors[0].selected, | |
)); | |
if (_productsResponse.colors.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.colors[1].id, | |
type: kFilterColor, | |
text: _productsResponse.colors[1].id, | |
count: _productsResponse.colors[1].count, | |
isSelected: _productsResponse.colors[1].selected, | |
)); | |
} | |
} | |
//Adding categories | |
if (_productsResponse.categories.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterCategory)) { | |
_productsResponse.categories.sort((b, a) => a.count.compareTo(b.count)); | |
_productsResponse.categories = | |
_productsResponse.categories.reversed.toList(); | |
pElements.add(PElement( | |
id: _productsResponse.categories[0].id, | |
type: kFilterCategory, | |
text: _productsResponse.categories[0].id, | |
count: _productsResponse.categories[0].count, | |
isSelected: _productsResponse.categories[0].selected, | |
)); | |
if (_productsResponse.categories.length > 1) { | |
pElements.add(PElement( | |
id: _productsResponse.categories[1].id, | |
type: kFilterCategory, | |
text: _productsResponse.categories[1].id, | |
count: _productsResponse.categories[1].count, | |
isSelected: _productsResponse.categories[1].selected, | |
)); | |
} | |
} | |
} | |
} | |
saveTopTwoElementsWithSelected(ProductsResponse productsResponse) { | |
print("saveTopTwoElements"); | |
pElements.clear(); | |
if (pElements.length == 0) { | |
//This function will display top 2 products elements from these 4 elements "colors", "categories", "materials", "brands" | |
//Adding materials | |
if (productsResponse.materials.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterMaterial)) { | |
productsResponse.materials.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.materials = productsResponse.materials.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.materials[0].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[0].id.vanityName, | |
count: productsResponse.materials[0].count, | |
isSelected: productsResponse.materials[0].selected, | |
)); | |
if (productsResponse.materials.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.materials[1].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[1].id.vanityName, | |
count: productsResponse.materials[1].count, | |
isSelected: productsResponse.materials[1].selected, | |
)); | |
} | |
} | |
//Adding brands | |
if (productsResponse.brands.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterBrand)) { | |
productsResponse.brands.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.brands = productsResponse.brands.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.brands[0].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[0].id.vanityName, | |
count: productsResponse.brands[0].count, | |
isSelected: productsResponse.brands[0].selected, | |
)); | |
if (productsResponse.brands.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.brands[1].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[1].id.vanityName, | |
count: productsResponse.brands[1].count, | |
isSelected: productsResponse.brands[1].selected, | |
)); | |
} | |
} | |
//Adding colors | |
if (productsResponse.colors.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterColor)) { | |
productsResponse.colors.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.colors = productsResponse.colors.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.colors[0].id, | |
type: kFilterColor, | |
text: productsResponse.colors[0].id, | |
count: productsResponse.colors[0].count, | |
isSelected: productsResponse.colors[0].selected, | |
)); | |
if (productsResponse.colors.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.colors[1].id, | |
type: kFilterColor, | |
text: productsResponse.colors[1].id, | |
count: productsResponse.colors[1].count, | |
isSelected: productsResponse.colors[1].selected, | |
)); | |
} | |
} | |
//Adding categories | |
if (productsResponse.categories.length > 0 && | |
!widget.productFilters.exceptElements.contains(kFilterCategory)) { | |
productsResponse.categories.sort((b, a) => a.count.compareTo(b.count)); | |
productsResponse.categories = | |
productsResponse.categories.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.categories[0].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[0].id, | |
count: productsResponse.categories[0].count, | |
isSelected: productsResponse.categories[0].selected, | |
)); | |
if (productsResponse.categories.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.categories[1].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[1].id, | |
count: productsResponse.categories[1].count, | |
isSelected: productsResponse.categories[1].selected, | |
)); | |
} | |
} | |
} | |
} | |
saveTopTwoElementsNew(ProductsResponse productsResponse) { | |
print("saveTopTwoElements"); | |
// if (!fromTheSamePage) { | |
pElements.clear(); | |
// } | |
if (pElements.length == 0) { | |
//This function will display top 2 products elements from these 4 elements "colors", "categories", "materials", "brands" | |
//Adding materials | |
if (productsResponse.materials.length > 0 && | |
widget.productFilters.materials.length == 0) { | |
productsResponse.materials.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.materials = productsResponse.materials.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.materials[0].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[0].id.vanityName, | |
count: productsResponse.materials[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.materials.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.materials[1].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[1].id.vanityName, | |
count: productsResponse.materials[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding brands | |
if (productsResponse.brands.length > 0 && | |
widget.productFilters.brands.length == 0) { | |
productsResponse.brands.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.brands = productsResponse.brands.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.brands[0].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[0].id.vanityName, | |
count: productsResponse.brands[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.brands.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.brands[1].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[1].id.vanityName, | |
count: productsResponse.brands[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding colors | |
if (productsResponse.colors.length > 0 && | |
widget.productFilters.colors.length == 0) { | |
productsResponse.colors.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.colors = productsResponse.colors.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.colors[0].id, | |
type: kFilterColor, | |
text: productsResponse.colors[0].id, | |
count: productsResponse.colors[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.colors.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.colors[1].id, | |
type: kFilterColor, | |
text: productsResponse.colors[1].id, | |
count: productsResponse.colors[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding categories | |
if (productsResponse.categories.length > 0 && | |
widget.productFilters.categories.length == 0) { | |
productsResponse.categories.sort((b, a) => a.count.compareTo(b.count)); | |
productsResponse.categories = | |
productsResponse.categories.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.categories[0].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[0].id, | |
count: productsResponse.categories[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.categories.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.categories[1].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[1].id, | |
count: productsResponse.categories[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
} | |
} | |
saveTopTwoElements(ProductsResponse productsResponse) { | |
print("saveTopTwoElements"); | |
if (!fromTheSamePage) { | |
pElements.clear(); | |
} | |
if (pElements.length == 0) { | |
//This function will display top 2 products elements from these 4 elements "colors", "categories", "materials", "brands" | |
//Adding materials | |
if (productsResponse.materials.length > 0 && | |
widget.productFilters.exceptElements.contains(kFilterMaterial)) { | |
productsResponse.materials.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.materials = productsResponse.materials.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.materials[0].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[0].id.vanityName, | |
count: productsResponse.materials[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.materials.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.materials[1].id.id, | |
type: kFilterMaterial, | |
text: productsResponse.materials[1].id.vanityName, | |
count: productsResponse.materials[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding brands | |
if (productsResponse.brands.length > 0 && | |
widget.productFilters.exceptElements.contains(kFilterBrand)) { | |
productsResponse.brands.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.brands = productsResponse.brands.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.brands[0].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[0].id.vanityName, | |
count: productsResponse.brands[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.brands.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.brands[1].id.id, | |
type: kFilterBrand, | |
text: productsResponse.brands[1].id.vanityName, | |
count: productsResponse.brands[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding colors | |
if (productsResponse.colors.length > 0 && | |
widget.productFilters.exceptElements.contains(kFilterColor)) { | |
productsResponse.colors.sort((b, a) => a.count.compareTo(b.count)); | |
// productsResponse.colors = productsResponse.colors.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.colors[0].id, | |
type: kFilterColor, | |
text: productsResponse.colors[0].id, | |
count: productsResponse.colors[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.colors.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.colors[1].id, | |
type: kFilterColor, | |
text: productsResponse.colors[1].id, | |
count: productsResponse.colors[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
//Adding categories | |
if (productsResponse.categories.length > 0 && | |
widget.productFilters.exceptElements.contains(kFilterCategory)) { | |
productsResponse.categories.sort((b, a) => a.count.compareTo(b.count)); | |
productsResponse.categories = | |
productsResponse.categories.reversed.toList(); | |
pElements.add(PElement( | |
id: productsResponse.categories[0].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[0].id, | |
count: productsResponse.categories[0].count, | |
isSelected: false, | |
)); | |
if (productsResponse.categories.length > 1) { | |
pElements.add(PElement( | |
id: productsResponse.categories[1].id, | |
type: kFilterCategory, | |
text: productsResponse.categories[1].id, | |
count: productsResponse.categories[1].count, | |
isSelected: false, | |
)); | |
} | |
} | |
} | |
} | |
_productsTopElements(bool isShimmer) { | |
return Padding( | |
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.fromLTRB(0, 0, 0, 20), | |
child: Container( | |
height: 50, | |
child: ListView.builder( | |
itemCount: pElements.length, | |
scrollDirection: Axis.horizontal, | |
itemBuilder: (BuildContext context, int index) { | |
return Padding( | |
padding: const EdgeInsets.fromLTRB(0, 16, 0, 0), | |
child: ProductElementFilterWidget( | |
pElement: pElements[index], | |
isSelected: pElements[index].isSelected, | |
myOwnTap: () { | |
pElements[index].isSelected = | |
!pElements[index].isSelected; | |
print( | |
"Click on pElements ${pElements[index].type} ${pElements[index].id} ${pElements[index].isSelected}"); | |
if (pElements[index].type == kFilterMaterial && | |
pElements[index].isSelected) { | |
widget.productFilters.materials.add(FColors( | |
id: pElements[index].id, | |
name: "$index", | |
isSelected: true)); | |
} else if (pElements[index].type == kFilterMaterial && | |
!pElements[index].isSelected) { | |
/*widget.productFilters.materials.remove(FColors( | |
id: pElements[index].id, isSelected: true));*/ | |
widget.productFilters.materials.removeWhere( | |
(item) => item.id == pElements[index].id); | |
} | |
if (pElements[index].type == kFilterBrand && | |
pElements[index].isSelected) { | |
widget.productFilters.brands | |
.add(FColors(id: pElements[index].id)); | |
} else if (pElements[index].type == kFilterBrand && | |
!pElements[index].isSelected) { | |
/*widget.productFilters.brands.remove(FColors( | |
id: pElements[index].id, isSelected: true));*/ | |
widget.productFilters.brands.removeWhere( | |
(item) => item.id == pElements[index].id); | |
} | |
if (pElements[index].type == kFilterColor && | |
pElements[index].isSelected) { | |
widget.productFilters.colors.add( | |
FColors( | |
id: pElements[index].id, | |
name: pElements[index].id, | |
isSelected: true), | |
); | |
++widget.productFilters.count; | |
} else if (pElements[index].type == kFilterColor && | |
!pElements[index].isSelected) { | |
widget.productFilters.colors.removeWhere((item) => | |
item.id.toLowerCase() == | |
pElements[index].id.toLowerCase()); | |
--widget.productFilters.count; | |
} | |
if (pElements[index].type == kFilterCategory && | |
pElements[index].isSelected) { | |
widget.productFilters.categories | |
.add(FColors(id: pElements[index].id)); | |
} else if (pElements[index].type == kFilterCategory && | |
!pElements[index].isSelected) { | |
widget.productFilters.categories.removeWhere( | |
(item) => item.id == pElements[index].id); | |
} | |
_productsResponse.products.clear(); | |
productEndPointResultBloc.setProductNull(); | |
productEndPointResultBloc.fetchProductsByFilter( | |
widget.productFilters.filterStr); | |
}, | |
isLastIndex: (pElements.length - 1) == index, | |
isShimmer: false, | |
), | |
); | |
}, | |
), | |
), | |
), | |
], | |
), | |
); | |
} | |
_buildProductGrid(bool isShimmer) { | |
return Padding( | |
padding: const EdgeInsets.only(right: 6), | |
child: GridView.builder( | |
physics: NeverScrollableScrollPhysics(), | |
shrinkWrap: true, | |
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 2, | |
crossAxisSpacing: 0, | |
childAspectRatio: Platform.isIOS ? 0.58 : 0.56, | |
), | |
itemCount: _productsResponse.products.length, | |
itemBuilder: (BuildContext ctxt, int index) { | |
return GridProduct( | |
key: UniqueKey(), | |
iconState: | |
favProductIds.contains(_productsResponse.products[index].id) | |
? FollowStatus.ACTIVE | |
: FollowStatus.INACTIVE, | |
isShimmer: isShimmer, | |
product: _productsResponse.products[index], | |
myOnTap: () { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (context) => ProductDetails( | |
_productsResponse.products[index].id, | |
_productsResponse.products[index].title, | |
productType: _productsResponse.products[index].productType, | |
), | |
), | |
); | |
}, | |
iconTap: () { | |
if (_isLoginUser) { | |
setState(() { | |
_toggleState(index, _productsResponse.products[index].id); | |
}); | |
} else { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (context) => SignUp(), | |
), | |
); | |
} | |
}, | |
itemPadding: (_productsResponse.products.length - 1 == index) | |
? const EdgeInsets.fromLTRB(8, 0, 8, 12) | |
: const EdgeInsets.fromLTRB(8, 0, 0, 12), | |
); | |
}, | |
), | |
); | |
} | |
_buildToolbar(String title, bool isFilterShow) { | |
print("show filter icon: $isFilterShow"); | |
return Container( | |
height: 90, | |
child: AppBar( | |
elevation: 4, | |
centerTitle: true, | |
title: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
title, | |
style: Theme.of(context) | |
.textTheme | |
.title | |
.copyWith(fontSize: 24, fontStyle: FontStyle.normal), | |
), | |
), | |
actions: <Widget>[ | |
isFilterShow | |
? GestureDetector( | |
onTap: () async { | |
//Actual filter parameters | |
ProductFilterParam productFilterParam = | |
ProductFilterParam(); | |
ProductFilterParam p2 = productFilterParam.rebuild( | |
(b) => b | |
..count = widget.productFilters.count | |
..lowerPrice = widget.productFilters.lowerPrice | |
..upperPrice = widget.productFilters.upperPrice | |
..saleOnly = widget.productFilters.saleOnly | |
..countries.addAll(widget.productFilters.countries) | |
..colors.addAll(widget.productFilters.colors) | |
..sizes.addAll(widget.productFilters.sizes) | |
..brands.addAll(widget.productFilters.brands) | |
..categories.addAll(widget.productFilters.categories) | |
..materials.addAll(widget.productFilters.materials), | |
); | |
//Make copy of actual filter parameters | |
ProductFilterParam productFilterParam2 = | |
ProductFilterParam(); | |
ProductFilterParam p3 = productFilterParam2.rebuild( | |
(b) => b | |
..count = widget.productFilters.count | |
..lowerPrice = widget.productFilters.lowerPrice | |
..upperPrice = widget.productFilters.upperPrice | |
..saleOnly = widget.productFilters.saleOnly | |
..countries.addAll(widget.productFilters.countries) | |
..colors.addAll(widget.productFilters.colors) | |
..sizes.addAll(widget.productFilters.sizes) | |
..brands.addAll(widget.productFilters.brands) | |
..categories.addAll(widget.productFilters.categories) | |
..materials.addAll(widget.productFilters.materials), | |
); | |
var filterResult = await Navigator.push( | |
context, | |
MySlideTransitionRoute( | |
//Go to filter | |
builder: (context) => Filter( | |
productFilterParam: p2, | |
productFilterParamCopy: p3, | |
masterLowerSliderPrice: | |
masterLowerSliderPrice.toDouble(), | |
masterHigherSliderPrice: | |
masterHigherSliderPrice.toDouble(), | |
), | |
), | |
); | |
fromTheSamePage = false; | |
_productsResponse.products.clear(); | |
productEndPointResultBloc.setProductNull(); | |
productEndPointResultBloc | |
.fetchProductsByFilter(filterResult[1].filterStr); | |
_isFeatured = true; | |
_isNewest = false; | |
_isPriceLow = false; | |
_isPriceHigh = false; | |
widget.productFilters.count = filterResult[1].count; | |
widget.productFilters.lowerPrice = | |
filterResult[1].lowerPrice; | |
widget.productFilters.upperPrice = | |
filterResult[1].upperPrice; | |
widget.productFilters.saleOnly = filterResult[1].saleOnly; | |
widget.productFilters.countries = filterResult[1].countries; | |
widget.productFilters.colors = filterResult[1].colors; | |
widget.productFilters.sizes = filterResult[1].sizes; | |
widget.productFilters.categories = | |
filterResult[1].categories; | |
widget.productFilters.materials = filterResult[1].materials; | |
widget.productFilters.brands = filterResult[1].brands; | |
}, | |
child: Center( | |
child: Stack( | |
children: <Widget>[ | |
Align( | |
alignment: Alignment.center, | |
child: Icon( | |
RenoonIcons.filter, | |
color: kBlack, | |
size: 20, | |
), | |
), | |
widget.productFilters.count > 0 | |
? Align( | |
alignment: Alignment.topRight, | |
child: Container( | |
margin: | |
const EdgeInsets.only(top: 12, left: 12), | |
decoration: BoxDecoration( | |
color: kBlack, | |
borderRadius: | |
BorderRadius.all(Radius.circular(16)), | |
), | |
width: 16, | |
height: 16, | |
child: Center( | |
child: Text( | |
widget.productFilters.count.toString(), | |
style: TextStyle(color: Colors.white), | |
), | |
), | |
), | |
) | |
: SizedBox(), | |
], | |
), | |
), | |
) | |
: SizedBox( | |
height: 0, | |
), | |
SizedBox( | |
width: 12, | |
), | |
], | |
), | |
); | |
} | |
Widget _popupCard() { | |
return Offstage( | |
offstage: _isPopup, | |
child: Container( | |
width: 230, | |
height: 230, | |
child: Card( | |
color: Colors.white, | |
elevation: 5.0, | |
borderOnForeground: false, | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(30.0), | |
), | |
child: Padding( | |
padding: const EdgeInsets.all(12.0), | |
child: Container( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.start, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
GestureDetector( | |
onTap: () { | |
setState(() { | |
_isFeatured = true; | |
_isNewest = false; | |
_isPriceHigh = false; | |
_isPriceLow = false; | |
}); | |
}, | |
child: Container( | |
width: double.maxFinite, | |
decoration: BoxDecoration( | |
border: BorderDirectional( | |
bottom: BorderSide( | |
color: kBottomBorder, width: 1))), | |
child: Row( | |
children: <Widget>[ | |
Container( | |
margin: EdgeInsets.all(8.0), | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
color: kCircularImageBoarder), | |
child: Padding( | |
padding: const EdgeInsets.all(10.0), | |
child: _isFeatured | |
? Icon( | |
Icons.check, | |
size: 12.0, | |
color: Colors.white, | |
) | |
: Icon( | |
Icons.check_box_outline_blank, | |
size: 12.0, | |
color: kCircularImageBackground, | |
), | |
), | |
), | |
SizedBox(width: 12.0), | |
Text( | |
"Featured", | |
style: TextStyle( | |
color: Colors.black, | |
fontWeight: FontWeight.w700), | |
), | |
], | |
)), | |
), | |
GestureDetector( | |
onTap: () { | |
// setState(() { | |
productEndPointResultBloc.setProductNull(); | |
productEndPointResultBloc.fetchProductsByFilter( | |
widget.productFilters.filterStr + "&sort=latest"); | |
_isNewest = true; | |
_isFeatured = false; | |
_isPriceHigh = false; | |
_isPriceLow = false; | |
// }); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
border: BorderDirectional( | |
bottom: BorderSide( | |
color: kBottomBorder, width: 1))), | |
child: Row( | |
children: <Widget>[ | |
Container( | |
margin: EdgeInsets.all(8.0), | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
color: kCircularImageBoarder), | |
child: Padding( | |
padding: const EdgeInsets.all(10.0), | |
child: _isNewest | |
? Icon( | |
Icons.check, | |
size: 12.0, | |
color: Colors.white, | |
) | |
: Icon( | |
Icons.check_box_outline_blank, | |
size: 12.0, | |
color: kCircularImageBoarder, | |
), | |
), | |
), | |
SizedBox(width: 12.0), | |
Text( | |
"Newest", | |
style: TextStyle( | |
color: Colors.black, | |
fontWeight: FontWeight.w700), | |
), | |
], | |
)), | |
), | |
GestureDetector( | |
onTap: () { | |
// setState(() { | |
productEndPointResultBloc.setProductNull(); | |
productEndPointResultBloc.fetchProductsByFilter( | |
widget.productFilters.filterStr + "&sort=-price"); | |
_isPriceLow = true; | |
_isNewest = false; | |
_isFeatured = false; | |
_isPriceHigh = false; | |
// }); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
border: BorderDirectional( | |
bottom: BorderSide( | |
color: kBottomBorder, width: 1))), | |
child: Row( | |
children: <Widget>[ | |
Container( | |
margin: EdgeInsets.all(8.0), | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
color: kCircularImageBoarder), | |
child: Padding( | |
padding: const EdgeInsets.all(10.0), | |
child: _isPriceLow | |
? Icon( | |
Icons.check, | |
size: 12.0, | |
color: Colors.white, | |
) | |
: Icon( | |
Icons.check_box_outline_blank, | |
size: 12.0, | |
color: kCircularImageBoarder, | |
), | |
), | |
), | |
SizedBox(width: 12.0), | |
Text( | |
"Price - Low to High", | |
style: TextStyle( | |
color: Colors.black, | |
fontWeight: FontWeight.w700), | |
), | |
], | |
)), | |
), | |
GestureDetector( | |
onTap: () { | |
// setState(() { | |
_productsResponse.products.clear(); | |
productEndPointResultBloc.setProductNull(); | |
productEndPointResultBloc.fetchProductsByFilter( | |
widget.productFilters.filterStr + "&sort=price"); | |
_isPriceHigh = true; | |
_isPriceLow = false; | |
_isNewest = false; | |
_isFeatured = false; | |
// }); | |
}, | |
child: Container( | |
child: Row( | |
children: <Widget>[ | |
Container( | |
margin: EdgeInsets.all(8.0), | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
color: kCircularImageBoarder), | |
child: Padding( | |
padding: const EdgeInsets.all(10.0), | |
child: _isPriceHigh | |
? Icon( | |
Icons.check, | |
size: 12.0, | |
color: Colors.white, | |
) | |
: Icon( | |
Icons.check_box_outline_blank, | |
size: 12.0, | |
color: kCircularImageBoarder, | |
), | |
), | |
), | |
SizedBox(width: 12.0), | |
Text( | |
"Price - High to Low", | |
style: TextStyle( | |
color: Colors.black, | |
fontWeight: FontWeight.w700), | |
), | |
], | |
)), | |
), | |
], | |
), | |
decoration: BoxDecoration( | |
borderRadius: new BorderRadiusDirectional.only( | |
topStart: Radius.elliptical(15, 15), | |
topEnd: Radius.elliptical(15, 15), | |
bottomEnd: Radius.elliptical(15, 15), | |
bottomStart: Radius.elliptical(15, 15)), | |
)), | |
)), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment