Skip to content

Instantly share code, notes, and snippets.

@mtellect
Created June 3, 2018 09:21
Show Gist options
  • Save mtellect/05275854bdcaf287a4520425e828a9ba to your computer and use it in GitHub Desktop.
Save mtellect/05275854bdcaf287a4520425e828a9ba to your computer and use it in GitHub Desktop.
class SliverGridProductList extends StatefulWidget {
final List<Base> products;
final FbConn fbConn;
SliverGridProductList({this.products, this.fbConn});
@override
_SliverGridProductListState createState() =>
new _SliverGridProductListState();
}
class _SliverGridProductListState extends State<SliverGridProductList> {
BuildContext context;
FirebaseAuth _auth;
bool _isSignedIn = false;
@override
void initState() {
// TODO: implement initState
super.initState();
_auth = FirebaseAuth.instance;
_getCurrentUser();
}
void showInSnackBar(String value, bool loggedIn) {
loggedIn == true
? Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(value),
action: new SnackBarAction(
label: "Login",
onPressed: () {
Navigator.of(context).push(new CupertinoPageRoute(
builder: (BuildContext context) => new GirliesLogin()));
}),
))
: Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(value),
));
}
Future _getCurrentUser() async {
await _auth.currentUser().then((user) {
if (user != null) {
setState(() {
_isSignedIn = true;
AppData.currentUserID = user.uid;
});
}
});
_auth.onAuthStateChanged.listen((user) {
if (user == null) {
setState(() {
_isSignedIn = false;
AppData.currentUserID = null;
});
} else {
_isSignedIn = true;
AppData.currentUserID = user.uid;
}
});
}
@override
Widget build(BuildContext context) {
this.context = context;
return new SliverGrid(
gridDelegate: new SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 220.0,
mainAxisSpacing: 2.0,
crossAxisSpacing: 2.0,
childAspectRatio: 0.9,
),
delegate: new SliverChildBuilderDelegate(
(BuildContext context, int index) {
return new GestureDetector(
onTap: () {
Navigator.of(context).push(new CupertinoPageRoute(
builder: (BuildContext context) => new BoutiqueItem(
productCategory:
widget.fbConn.getProductCategoryAsList()[index],
productImage:
widget.fbConn.getDefaultIMGAsList()[index],
productPrice:
widget.fbConn.getProductPriceAsList()[index],
productList: widget.products,
productTitle:
widget.fbConn.getProductNameAsList()[index],
productQuantity: 1,
fbConn: widget.fbConn,
sizesAvailable:
widget.fbConn.getProductSizesAsList()[index],
colorsAvailable:
widget.fbConn.getProductColorsAsList()[index],
itemKey: widget.fbConn.getKeyIDasList()[index],
index: index,
isInFavorite:
widget.fbConn.getIsFavoriteAsList()[index],
)));
},
child: new Card(
child: new GridTile(
header: new Align(
alignment: Alignment.topRight,
child: new CupertinoButton(
padding: EdgeInsets.zero,
child: _isSignedIn == false
? new Icon(
Icons.favorite_border,
color: Colors.black54,
)
: widget.fbConn.getIsFavoriteAsList()[index] == false
? new Icon(
Icons.favorite_border,
color: Colors.black54,
)
: new Icon(
Icons.favorite,
color: MyApp.appColors[600],
),
onPressed: () {
_isSignedIn == false
? showInSnackBar(
"Please login to view your add to favorites!",
true)
: widget.fbConn.getIsFavoriteAsList()[index] == true
? setState(() {
//widget.fbConn.addFavorite(false, index);
widget.fbConn.removeFavorite(index);
showInSnackBar(
widget.fbConn
.getProductNameAsList()[index] +
" has been removed from your favorite",
false);
})
: setState(() {
widget.fbConn.addFavorite(true, index);
showInSnackBar(
widget.fbConn
.getProductNameAsList()[index] +
" has been added to your favorite",
false);
});
},
),
),
child: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.all(5.0),
child: new Image.network(
widget.fbConn.getDefaultIMGAsList()[index],
height: 120.0,
),
),
new Align(
alignment: Alignment.center,
child: new Padding(
padding: const EdgeInsets.all(5.0),
child: new Text(
"N" + widget.fbConn.getProductPriceAsList()[index],
),
),
),
new Align(
alignment: Alignment.center,
child: new Padding(
padding: const EdgeInsets.all(5.0),
child: new Text(
widget.fbConn.getProductNameAsList()[index],
),
),
),
],
) //just for testing, will fill with image later
),
),
);
},
childCount: widget.fbConn.getDataSize(),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment