Last active
April 17, 2020 07:12
-
-
Save vicradon/7585bdacc9445af09880bd643149fa3a to your computer and use it in GitHub Desktop.
The code snippets for the shopping cart app
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 'package:flutter/material.dart'; | |
void main() => runApp(App()); | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
initialRoute: '/', | |
routes: { | |
'/': (BuildContext context) => ShopItems(), | |
'/checkout': (BuildContext context) => Checkout() | |
}, | |
); | |
} | |
} |
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 'package:flutter/material.dart'; | |
import 'package:shopping_cart_app/pages/checkout.dart'; | |
import 'package:shopping_cart_app/pages/shop_items.dart'; | |
void main() => runApp(App()); | |
... |
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 'package:flutter/material.dart'; | |
class ShopItems extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Shopping Cart App'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.shopping_cart), | |
onPressed: () => Navigator.pushNamed(context, '/checkout'), | |
) | |
], | |
), | |
body: ShopItemsWidget(), | |
); | |
} | |
} | |
class ShopItemsWidget extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return Center(child: Text("All items in shop have been taken")); | |
} | |
} |
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 'package:flutter/material.dart'; | |
class Checkout extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Checkout')), | |
body: Center(child: Text("You haven't taken any item yet")) | |
); | |
} | |
} |
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 'package:shopping_cart_app/pages/checkout.dart'; | |
import 'package:shopping_cart_app/pages/shop_items.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(App()); | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
Map<int, Color> color = { | |
50: Color.fromRGBO(255, 144, 0, .1), | |
100: Color.fromRGBO(255, 144, 0, .2), | |
200: Color.fromRGBO(255, 144, 0, .3), | |
300: Color.fromRGBO(255, 144, 0, .4), | |
400: Color.fromRGBO(255, 144, 0, .5), | |
500: Color.fromRGBO(255, 144, 0, .6), | |
600: Color.fromRGBO(255, 144, 0, .7), | |
700: Color.fromRGBO(255, 144, 0, .8), | |
800: Color.fromRGBO(255, 144, 0, .9), | |
900: Color.fromRGBO(255, 144, 0, 1), | |
}; | |
return MaterialApp( | |
theme: ThemeData( | |
primarySwatch: MaterialColor(0xFFFF7000, color), | |
), | |
initialRoute: '/', | |
routes: { | |
'/': (BuildContext context) => ShopItems(), | |
'/checkout': (BuildContext context) => Checkout() | |
}, | |
); | |
} | |
} |
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
/// The [dart:async] is necessary for using streams | |
import 'dart:async'; | |
class CartItemsBloc { | |
/// The [cartStreamController] is an object of the StreamController class | |
/// .broadcast enables the stream to be read in multiple screens of our app | |
final cartStreamController = StreamController.broadcast(); | |
/// The [getStream] getter would be used to expose our stream to other classes | |
Stream get getStream => cartStreamController.stream; | |
/// The [allItems] Map would hold all the data this bloc provides | |
final Map allItems = {}; | |
/// The [dispose] method is used | |
/// to automatically close the stream when the widget is removed from the widget tree | |
void dispose() { | |
cartStreamController.close(); // close our StreamController | |
} | |
} |
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
... | |
Stream get getStream => cartStreamController.stream; | |
/// The [allItems] Map would hold all the data this bloc provides | |
final Map allItems = { | |
'shop items': [ | |
{'name': 'App dev kit', 'price': 20, 'id': 1}, | |
{'name': 'App consultation', 'price': 100, 'id': 2}, | |
{'name': 'Logo Design', 'price': 10, 'id': 3}, | |
{'name': 'Code review', 'price': 90, 'id': 4}, | |
], | |
'cart items': [] | |
}; | |
... |
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
], | |
'cart items': [] | |
}; | |
/// [addToCart] adds items from the shop to the cart | |
void addToCart(item) { | |
allItems['shop items'].remove(item); | |
allItems['cart items'].add(item); | |
cartStreamController.sink.add(allItems); | |
} | |
/// [removeFromCart] removes items from the cart, back to the shop | |
void removeFromCart(item) { | |
allItems['cart items'].remove(item); | |
allItems['shop items'].add(item); | |
cartStreamController.sink.add(allItems); | |
} | |
... |
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
... | |
/// The [dispose] method is used | |
/// to automatically close the stream when the widget is removed from the widget tree | |
void dispose() { | |
cartStreamController.close(); // close our StreamController | |
} | |
} | |
final bloc = CartItemsBloc(); // add to the end of the file |
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 'package:flutter/material.dart'; | |
import 'package:shopping_cart_app/bloc/cart_items_bloc.dart'; | |
... |
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
class ShopItemsWidget extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return StreamBuilder( | |
initialData: bloc.allItems, // The bloc was already instantiated. | |
stream: bloc.getStream, // The stream we'd be listing to | |
builder: (context, snapshot) { | |
// snapshot contains the data of the bloc | |
return Center(child: Text("All items in shop have been taken")); | |
}, | |
); | |
} | |
} |
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
... | |
Widget shopItemsListBuilder(snapshot) { | |
return ListView.builder( | |
itemCount: snapshot.data["shop items"].length, | |
itemBuilder: (BuildContext context, i) { | |
final shopList = snapshot.data["shop items"]; | |
return ListTile( | |
title: Text(shopList[i]['name']), | |
subtitle: Text("\$${shopList[i]['price']}"), | |
trailing: IconButton( | |
icon: Icon(Icons.add_shopping_cart), | |
onPressed: () { | |
bloc.addToCart(shopList[i]); | |
}, | |
), | |
onTap: () {}, | |
); | |
}, | |
); | |
} |
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 'package:flutter/material.dart'; | |
import 'package:shopping_cart_app/bloc/cart_items_bloc.dart'; // do as before | |
... |
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
class Checkout extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Checkout')), | |
body: StreamBuilder( | |
stream: bloc.getStream, | |
initialData: bloc.allItems, | |
builder: (context, snapshot) { | |
return snapshot.data['cart items'].length > 0 | |
? Column( | |
children: <Widget>[ | |
/// The [checkoutListBuilder] has to be fixed | |
/// in an expanded widget to ensure it | |
/// doesn't occupy the whole screen and leaves | |
/// room for the the RaisedButton | |
Expanded(child: checkoutListBuilder(snapshot)), | |
RaisedButton( | |
onPressed: () {}, | |
child: Text("Checkout"), | |
color: Theme.of(context).primaryColor, | |
), | |
SizedBox(height: 40) | |
], | |
) | |
: Center(child: Text("You haven't taken any item yet")); | |
}, | |
), | |
); | |
} | |
} |
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
... | |
Widget checkoutListBuilder(snapshot) { | |
return ListView.builder( | |
itemCount: snapshot.data["cart items"].length, | |
itemBuilder: (BuildContext context, i) { | |
final cartList = snapshot.data["cart items"]; | |
return ListTile( | |
title: Text(cartList[i]['name']), | |
subtitle: Text("\$${cartList[i]['price']}"), | |
trailing: IconButton( | |
icon: Icon(Icons.remove_shopping_cart), | |
onPressed: () { | |
bloc.removeFromCart(cartList[i]); | |
}, | |
), | |
onTap: () {}, | |
); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment