Skip to content

Instantly share code, notes, and snippets.

@junjizhi
Last active July 13, 2019 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junjizhi/40c6c26edac720c24265f0ca8b1d3ab5 to your computer and use it in GitHub Desktop.
Save junjizhi/40c6c26edac720c24265f0ca8b1d3ab5 to your computer and use it in GitHub Desktop.
Flutter BLoC and Provider: A Shopping Cart Example
// This is a pseudo code to show the overall structure
// of a main page of a shopping cart app built with Flutter.
// For the complete app, please see https://github.com/junjizhi/flutter-shopping-cart
Widget build(BuildContext context) {
var bloc = Provider.of<CartBloc>(context);
int totalCount = 0;
if (bloc.cart.length > 0) {
totalCount = bloc.cart.values.reduce((a, b) => a + b);
}
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
Container(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CartPage(),
),
);
},
child: _buildCartIcon(totalCount) // icon with text overlay
)
),
)
],
body: GridView.count(
crossAxisCount: 2,
children: List.generate(6, (index) {
return GestureDetector(
onTap: () {
bloc.addToCart(index);
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/${index + 1}.jpg"),
fit: BoxFit.fitWidth,
),
borderRadius: BorderRadius.circular(12),
),
));
}),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment