File tree 5 files changed +41
-11
lines changed
5 files changed +41
-11
lines changed Original file line number Diff line number Diff line change @@ -35,27 +35,40 @@ class MyApp extends StatelessWidget {
35
35
}
36
36
}
37
37
38
+ enum PopupValue {
39
+ showFavorite,
40
+ showAll,
41
+ }
42
+
38
43
class MyHomePage extends StatelessWidget {
39
44
MyHomePage ({Key key, this .title}) : super (key: key);
40
45
41
46
final String title;
42
47
43
48
@override
44
49
Widget build (BuildContext context) {
50
+ final productContainer = Provider .of <Products >(context);
45
51
return Scaffold (
46
52
appBar: AppBar (
47
53
//leading: Icon(Icons.menu),
48
54
actions: < Widget > [
49
55
PopupMenuButton (
56
+ onSelected: (_popupValue) {
57
+ if (_popupValue == PopupValue .showFavorite) {
58
+ productContainer.showFavorite ();
59
+ } else {
60
+ productContainer.showAll ();
61
+ }
62
+ },
50
63
icon: Icon (Icons .more_vert),
51
64
itemBuilder: (_) => [
52
65
PopupMenuItem (
53
66
child: Text ("Only Favorite" ),
54
- value: 0 ,
67
+ value: PopupValue .showFavorite ,
55
68
),
56
69
PopupMenuItem (
57
70
child: Text ("Show All" ),
58
- value: 1 ,
71
+ value: PopupValue .showAll ,
59
72
),
60
73
],
61
74
),
Original file line number Diff line number Diff line change @@ -14,12 +14,12 @@ class Product with ChangeNotifier {
14
14
@required this .description,
15
15
@required this .price,
16
16
@required this .imageUrl,
17
- this .isFavourite = true ,
17
+ this .isFavourite = false ,
18
18
});
19
19
20
20
void toggleFavorite () {
21
- print ("this item is favorite - $isFavourite " );
22
21
isFavourite = ! isFavourite;
22
+ print ("this item is favorite - $isFavourite " );
23
23
notifyListeners ();
24
24
}
25
25
}
Original file line number Diff line number Diff line change @@ -37,16 +37,27 @@ class Products with ChangeNotifier {
37
37
),
38
38
];
39
39
40
+ var isFavoriteTapped = false ;
41
+
40
42
List <Product > get items {
43
+ if (isFavoriteTapped)
44
+ return _items.where ((product) => product.isFavourite).toList ();
41
45
return [..._items];
42
46
}
43
47
44
48
Product findById (String productId) {
45
49
return _items.firstWhere ((p) => p.id == productId);
46
50
}
47
51
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" );
50
61
notifyListeners ();
51
62
}
52
63
}
Original file line number Diff line number Diff line change @@ -11,8 +11,10 @@ class ProductTile extends StatelessWidget {
11
11
borderRadius: BorderRadius .circular (8.0 ),
12
12
child: InkWell (
13
13
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
+ );
16
18
},
17
19
child: GridTile (
18
20
child: Image .network (
@@ -23,7 +25,7 @@ class ProductTile extends StatelessWidget {
23
25
backgroundColor: Colors .black87,
24
26
leading: IconButton (
25
27
icon: Icon (
26
- product.isFavourite ? Icons .favorite_border : Icons .favorite ),
28
+ product.isFavourite ? Icons .favorite : Icons .favorite_border ),
27
29
color: Theme .of (context).accentColor,
28
30
onPressed: () {
29
31
//toggle here
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ class ProductsGrid extends StatelessWidget {
8
8
Widget build (BuildContext context) {
9
9
final productData = Provider .of <Products >(context);
10
10
final productList = productData.items;
11
+
11
12
return GridView .builder (
12
13
padding: const EdgeInsets .all (10.0 ),
13
14
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount (
@@ -18,8 +19,11 @@ class ProductsGrid extends StatelessWidget {
18
19
),
19
20
itemCount: productList.length,
20
21
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],
23
27
child: ProductTile (),
24
28
);
25
29
},
You can’t perform that action at this time.
0 commit comments