Skip to content

Instantly share code, notes, and snippets.

@junjizhi
Last active July 13, 2019 21:18
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/47a2f29f4fe690ce9e7a5e9f25ce49b9 to your computer and use it in GitHub Desktop.
Save junjizhi/47a2f29f4fe690ce9e7a5e9f25ce49b9 to your computer and use it in GitHub Desktop.
Flutter BLoC and Provider: A Shopping Cart Example - Shopping Cart Page
// https://github.com/junjizhi/flutter-shopping-cart
class CartPage extends StatelessWidget {
CartPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
var bloc = Provider.of<CartBloc>(context);
var cart = bloc.cart;
return Scaffold(
appBar: AppBar(
title: Text("Shopping Cart"),
),
body: ListView.builder(
itemCount: cart.length,
itemBuilder: (context, index) {
int giftIndex = cart.keys.toList()[index];
int count = cart[giftIndex];
return ListTile(
leading: Container(
height: 70,
width: 70,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/${giftIndex + 1}.jpg"),
fit: BoxFit.fitWidth,
),
borderRadius: BorderRadius.circular(12),
),
),
title: Text('Item Count: $count'),
trailing: RaisedButton(
child: Text('Clear'),
color: Theme.of(context).buttonColor,
elevation: 1.0,
splashColor: Colors.blueGrey,
onPressed: () {
bloc.clear(giftIndex);
},
),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment