Skip to content

Commit

Permalink
UI created.
Browse files Browse the repository at this point in the history
  • Loading branch information
BBarisKilic committed Mar 19, 2021
1 parent 3754935 commit dc1acd3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ An app that uses 'Business Logic Component' design pattern and bloc state manage
## Through this project, I've learned:

- bloc design pattern and state management.
- singleton and factory design pattern.
-
- singleton and factory design pattern.
1 change: 1 addition & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
11 changes: 10 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:bloc_shopping/screens/cart_screen.dart';
import 'package:bloc_shopping/screens/product_list_screen.dart';
import 'package:flutter/material.dart';

void main() {
Expand All @@ -7,6 +9,13 @@ void main() {
class BLoCShopping extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp();
return MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (BuildContext context) => ProductListScreen(),
"/cart": (BuildContext context) => CartScreen()
},
initialRoute: "/",
);
}
}
44 changes: 44 additions & 0 deletions lib/screens/cart_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:bloc_shopping/blocs/cart_bloc.dart';
import 'package:flutter/material.dart';

class CartScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sepet"),
),
body: StreamBuilder(
stream: cartBloc.getStream,
initialData: cartBloc.getCart(),
builder: (context, snapshot) {
return snapshot.data.length > 0
? buildCart(snapshot)
: Center(
child: Text("Empty"),
);
},
),
);
}

Widget buildCart(AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) {
final cart = snapshot.data;
return ListTile(
title: Text(cart[index].product.name),
subtitle: Text(
cart[index].product.price.toString(),
),
trailing: IconButton(
icon: Icon(Icons.remove_shopping_cart),
onPressed: () {
cartBloc.removeFromCart(cart[index]);
},
),
);
});
}
}
61 changes: 61 additions & 0 deletions lib/screens/product_list_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:bloc_shopping/blocs/cart_bloc.dart';
import 'package:bloc_shopping/blocs/product_bloc.dart';
import 'package:bloc_shopping/models/cart_item.dart';
import 'package:flutter/material.dart';

class ProductListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
onPressed: () => Navigator.pushNamed(context, "/cart"),
icon: Icon(Icons.shopping_cart),
),
],
),
body: buildProductList(),
);
}

Widget buildProductList() {
return StreamBuilder(
initialData: productBloc.getAll(),
stream: productBloc.getStream,
builder: (context, snapshot) {
return snapshot.data.length > 0
? buildProductListItem(snapshot)
: Center(
child: Text("Loading..."),
);
},
);
}

Widget buildProductListItem(AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) {
final list = snapshot.data;

return ListTile(
title: Text(
list[index].name,
),
subtitle: Text(
list[index].price.toString(),
),
trailing: IconButton(
icon: Icon(Icons.add_shopping_cart),
onPressed: () {
cartBloc.addToCart(
CartItem(list[index], 1),
);
},
),
);
},
);
}
}

0 comments on commit dc1acd3

Please sign in to comment.