Last active
April 10, 2022 16:22
-
-
Save tranquangchau/c5cd30bc8f043796078ce0da5d0263f9 to your computer and use it in GitHub Desktop.
https://github.com/vicradon/shopping-cart-app (flutter 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
///https://dartpad.dev/?id=c5cd30bc8f043796078ce0da5d0263f9 | |
///https://github.com/vicradon/shopping-cart-app | |
import 'package:flutter/material.dart'; | |
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; | |
final Map allItems = { | |
'shop_items': [ | |
{'name': '(chau-test)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': [] | |
}; | |
void getCart(){ | |
return allItems['shop_items']; | |
} | |
void addToCart(item) { | |
allItems['shop_items'].remove(item); | |
allItems['cart_items'].add(item); | |
cartStreamController.sink.add(allItems); | |
} | |
void removeFromCart(item) { | |
allItems['cart_items'].remove(item); | |
allItems['shop_items'].add(item); | |
cartStreamController.sink.add(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 | |
} | |
} | |
final bloc = CartItemsBloc(); | |
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 StreamBuilder( | |
initialData: bloc.allItems, | |
stream: bloc.getStream, | |
builder: (context, snapshot) { | |
// print(snapshot); | |
// return ShopItems(); | |
return bloc.allItems["shop_items"].length >0 | |
? shopItemsListBuilder(snapshot) | |
: Center(child: Text("All items in shop have been taken")); | |
}, | |
); | |
} | |
} | |
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: () {}, | |
); | |
}, | |
); | |
} | |
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 build(context); | |
print("bloc.allItems"); | |
print(bloc.allItems['cart_items'].length); | |
var vLang = bloc.allItems['cart_items'].length; | |
//var vLang = 1; | |
return vLang > 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")); | |
}, | |
), | |
); | |
} | |
} | |
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: () {}, | |
); | |
}, | |
); | |
} | |
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( | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: MaterialColor(0xFFFF7000, color), | |
), | |
initialRoute: '/', | |
routes: { | |
'/': (BuildContext context) => ShopItems(), | |
'/checkout': (BuildContext context) => Checkout() | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment