Skip to content

Commit bc74646

Browse files
committed
app init
1 parent 1559173 commit bc74646

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

lib/main.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,40 @@ class MyApp extends StatelessWidget {
3535
}
3636
}
3737

38+
enum PopupValue {
39+
showFavorite,
40+
showAll,
41+
}
42+
3843
class MyHomePage extends StatelessWidget {
3944
MyHomePage({Key key, this.title}) : super(key: key);
4045

4146
final String title;
4247

4348
@override
4449
Widget build(BuildContext context) {
50+
final productContainer = Provider.of<Products>(context);
4551
return Scaffold(
4652
appBar: AppBar(
4753
//leading: Icon(Icons.menu),
4854
actions: <Widget>[
4955
PopupMenuButton(
56+
onSelected: (_popupValue) {
57+
if (_popupValue == PopupValue.showFavorite) {
58+
productContainer.showFavorite();
59+
} else {
60+
productContainer.showAll();
61+
}
62+
},
5063
icon: Icon(Icons.more_vert),
5164
itemBuilder: (_) => [
5265
PopupMenuItem(
5366
child: Text("Only Favorite"),
54-
value: 0,
67+
value: PopupValue.showFavorite,
5568
),
5669
PopupMenuItem(
5770
child: Text("Show All"),
58-
value: 1,
71+
value: PopupValue.showAll,
5972
),
6073
],
6174
),

lib/model/product.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class Product with ChangeNotifier {
1414
@required this.description,
1515
@required this.price,
1616
@required this.imageUrl,
17-
this.isFavourite = true,
17+
this.isFavourite = false,
1818
});
1919

2020
void toggleFavorite() {
21-
print("this item is favorite - $isFavourite");
2221
isFavourite = !isFavourite;
22+
print("this item is favorite - $isFavourite");
2323
notifyListeners();
2424
}
2525
}

lib/providers/products.dart

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,27 @@ class Products with ChangeNotifier {
3737
),
3838
];
3939

40+
var isFavoriteTapped = false;
41+
4042
List<Product> get items {
43+
if (isFavoriteTapped)
44+
return _items.where((product) => product.isFavourite).toList();
4145
return [..._items];
4246
}
4347

4448
Product findById(String productId) {
4549
return _items.firstWhere((p) => p.id == productId);
4650
}
4751

48-
void addProducts() {
49-
//_items.add(value);
52+
void showFavorite() {
53+
isFavoriteTapped = true;
54+
print("show favourite flag ON");
55+
notifyListeners();
56+
}
57+
58+
void showAll() {
59+
isFavoriteTapped = false;
60+
print("show favourite flag OFF");
5061
notifyListeners();
5162
}
5263
}

lib/widgets/product_tile.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class ProductTile extends StatelessWidget {
1111
borderRadius: BorderRadius.circular(8.0),
1212
child: InkWell(
1313
onTap: () {
14-
Navigator.of(context)
15-
.pushNamed(ProductDetailScreen.routeName, arguments: product.id);
14+
Navigator.of(context).pushNamed(
15+
ProductDetailScreen.routeName,
16+
arguments: product.id,
17+
);
1618
},
1719
child: GridTile(
1820
child: Image.network(
@@ -23,7 +25,7 @@ class ProductTile extends StatelessWidget {
2325
backgroundColor: Colors.black87,
2426
leading: IconButton(
2527
icon: Icon(
26-
product.isFavourite ? Icons.favorite_border : Icons.favorite),
28+
product.isFavourite ? Icons.favorite : Icons.favorite_border),
2729
color: Theme.of(context).accentColor,
2830
onPressed: () {
2931
//toggle here

lib/widgets/products_grid.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ProductsGrid extends StatelessWidget {
88
Widget build(BuildContext context) {
99
final productData = Provider.of<Products>(context);
1010
final productList = productData.items;
11+
1112
return GridView.builder(
1213
padding: const EdgeInsets.all(10.0),
1314
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
@@ -18,8 +19,11 @@ class ProductsGrid extends StatelessWidget {
1819
),
1920
itemCount: productList.length,
2021
itemBuilder: (BuildContext context, int index) {
21-
return ChangeNotifierProvider(
22-
create: (_) => productList[index],
22+
print(
23+
"index is $index ${productList[index].title} isFavoriteStatus: ${productList[index].isFavourite}");
24+
return ChangeNotifierProvider.value(
25+
value: productList[index],
26+
//create: (_) => productList[index],
2327
child: ProductTile(),
2428
);
2529
},

0 commit comments

Comments
 (0)