Skip to content

Commit e966ef3

Browse files
committed
Basic Provider Listeners setup. And debugging
1 parent 1f722a7 commit e966ef3

File tree

11 files changed

+180
-97
lines changed

11 files changed

+180
-97
lines changed

assets/fonts/Anton-Regular.ttf

72.8 KB
Binary file not shown.

assets/fonts/Lato-Bold.ttf

69.4 KB
Binary file not shown.

assets/fonts/Lato-Regular.ttf

71.2 KB
Binary file not shown.

lib/main.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import 'package:flutter/material.dart';
22

3+
//Third party package imports
4+
import 'package:provider/provider.dart';
5+
6+
// Screen Imports
7+
import './screens/products_overview_screen.dart';
8+
import './screens/product_detail_screen.dart';
9+
10+
// Provider Imports
11+
import './providers/products.dart';
12+
313
void main() => runApp(MyApp());
414

515
class MyApp extends StatelessWidget {
616
@override
717
Widget build(BuildContext context) {
8-
return MaterialApp(
9-
title: 'MyShop',
10-
theme: ThemeData(
11-
primarySwatch: Colors.blue,
12-
),
13-
home: MyHomePage(),
14-
);
15-
}
16-
}
17-
18-
class MyHomePage extends StatelessWidget {
19-
@override
20-
Widget build(BuildContext context) {
21-
return Scaffold(
22-
appBar: AppBar(
23-
title: Text('MyShop'),
24-
),
25-
body: Center(
26-
child: Text('Let\'s build a shop!'),
18+
return ChangeNotifierProvider(
19+
create: (ctx) => Products(),
20+
child: MaterialApp(
21+
title: 'MyShop',
22+
theme: ThemeData(
23+
primarySwatch: Colors.purple,
24+
accentColor: Colors.orange,
25+
fontFamily: 'Lato',
26+
),
27+
home: ProductsOverViewScreen(),
28+
routes: {
29+
ProductDetailScreen.routeName: (ctx) => ProductDetailScreen(),
30+
},
2731
),
2832
);
2933
}

lib/providers/products.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
// Model Imports
4+
import '../models/product.dart';
5+
6+
class Products with ChangeNotifier {
7+
List<Product> _items = [
8+
Product(
9+
id: 'p1',
10+
title: 'Red Shirt',
11+
description: 'A red shirt - it is pretty red!',
12+
price: 29.99,
13+
imageUrl:
14+
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
15+
),
16+
Product(
17+
id: 'p2',
18+
title: 'Trousers',
19+
description: 'A nice pair of trousers.',
20+
price: 59.99,
21+
imageUrl:
22+
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
23+
),
24+
Product(
25+
id: 'p3',
26+
title: 'Yellow Scarf',
27+
description: 'Warm and cozy - exactly what you need for the winter.',
28+
price: 19.99,
29+
imageUrl:
30+
'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
31+
),
32+
Product(
33+
id: 'p4',
34+
title: 'A Pan',
35+
description: 'Prepare any meal you want.',
36+
price: 49.99,
37+
imageUrl:
38+
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
39+
),
40+
];
41+
42+
List<Product> get items {
43+
return [..._items];
44+
}
45+
46+
void addProduct() {
47+
notifyListeners();
48+
}
49+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ProductDetailScreen extends StatelessWidget {
4+
static const routeName = '/product-detail';
5+
6+
@override
7+
Widget build(BuildContext context) {
8+
final productId = ModalRoute.of(context).settings.arguments as String;
9+
return Scaffold(
10+
appBar: AppBar(
11+
title: Text('Title'),
12+
),
13+
);
14+
}
15+
}
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,16 @@
11
import 'package:flutter/material.dart';
22

3-
// Models Imports
4-
import '../models/product.dart';
5-
63
// Widgets Imports
7-
import '../widgets/product_item.dart';
4+
import '../widgets/products_grid.dart';
85

96
class ProductsOverViewScreen extends StatelessWidget {
10-
final List<Product> loadedProducts = [
11-
Product(
12-
id: 'p1',
13-
title: 'Red Shirt',
14-
description: 'A red shirt - it is pretty red!',
15-
price: 29.99,
16-
imageUrl:
17-
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
18-
),
19-
Product(
20-
id: 'p2',
21-
title: 'Trousers',
22-
description: 'A nice pair of trousers.',
23-
price: 59.99,
24-
imageUrl:
25-
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
26-
),
27-
Product(
28-
id: 'p3',
29-
title: 'Yellow Scarf',
30-
description: 'Warm and cozy - exactly what you need for the winter.',
31-
price: 19.99,
32-
imageUrl:
33-
'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
34-
),
35-
Product(
36-
id: 'p4',
37-
title: 'A Pan',
38-
description: 'Prepare any meal you want.',
39-
price: 49.99,
40-
imageUrl:
41-
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
42-
),
43-
];
44-
457
@override
468
Widget build(BuildContext context) {
479
return Scaffold(
4810
appBar: AppBar(
4911
title: Text('Amazine'),
5012
),
51-
body: GridView.builder(
52-
padding: const EdgeInsets.all(10),
53-
itemCount: loadedProducts.length,
54-
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
55-
crossAxisCount: 2,
56-
childAspectRatio: 3 / 2,
57-
crossAxisSpacing: 10,
58-
mainAxisSpacing: 10),
59-
itemBuilder: (ctx, i) => ProductItem(loadedProducts[i].id,
60-
loadedProducts[i].title, loadedProducts[i].imageUrl)),
13+
body: ProductsGrid(),
6114
);
6215
}
6316
}

lib/widgets/product_item.dart

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
11
import 'package:flutter/material.dart';
22

3+
// Screens Imports
4+
import '../screens/product_detail_screen.dart';
5+
36
class ProductItem extends StatelessWidget {
47
// Product Properties
5-
68
final String id;
79
final String title;
8-
// final String description;
910
final String imageUrl;
10-
// final double price;
1111

12+
// Constructor
1213
ProductItem(this.id, this.title, this.imageUrl);
1314

1415
@override
1516
Widget build(BuildContext context) {
16-
return GridTile(
17-
child: Image.network(
18-
imageUrl,
19-
fit: BoxFit.cover,
20-
),
21-
footer: GridTileBar(
22-
backgroundColor: Colors.black54,
23-
leading: IconButton(onPressed: () {}, icon: Icon(Icons.favorite)),
24-
title: Text(
25-
title,
26-
textAlign: TextAlign.center,
17+
return ClipRRect(
18+
borderRadius: BorderRadius.circular(10),
19+
child: GridTile(
20+
child: InkWell(
21+
onTap: () {
22+
Navigator.of(context).pushNamed(
23+
ProductDetailScreen.routeName,
24+
arguments: id,
25+
);
26+
},
27+
child: Image.network(
28+
imageUrl,
29+
fit: BoxFit.cover,
30+
),
2731
),
28-
trailing: IconButton(
29-
onPressed: () {},
30-
icon: Icon(Icons.shopping_cart),
32+
footer: GridTileBar(
33+
backgroundColor: Colors.black87,
34+
leading: IconButton(
35+
onPressed: () {},
36+
icon: Icon(
37+
Icons.favorite,
38+
color: Theme.of(context).accentColor,
39+
)),
40+
title: Text(
41+
title,
42+
textAlign: TextAlign.center,
43+
),
44+
trailing: IconButton(
45+
onPressed: () {},
46+
icon: Icon(
47+
Icons.shopping_cart,
48+
color: Theme.of(context).accentColor,
49+
),
50+
),
3151
),
3252
),
3353
);

lib/widgets/products_grid.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter/material.dart';
2+
3+
// Third party packages
4+
import 'package:provider/provider.dart';
5+
6+
// Providers Import
7+
import '../providers/products.dart';
8+
9+
// Widgets Import
10+
import '../widgets/product_item.dart';
11+
12+
class ProductsGrid extends StatelessWidget {
13+
@override
14+
Widget build(BuildContext context) {
15+
final productsData = Provider.of<Products>(context);
16+
final products = productsData.items;
17+
return GridView.builder(
18+
padding: const EdgeInsets.all(10),
19+
itemCount: products.length,
20+
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
21+
crossAxisCount: 2,
22+
childAspectRatio: 3 / 2,
23+
crossAxisSpacing: 10,
24+
mainAxisSpacing: 10),
25+
itemBuilder: (ctx, i) => ProductItem(
26+
products[i].id, products[i].title, products[i].imageUrl));
27+
}
28+
}

pubspec.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,27 @@ packages:
8181
url: "https://pub.dartlang.org"
8282
source: hosted
8383
version: "1.3.0"
84+
nested:
85+
dependency: transitive
86+
description:
87+
name: nested
88+
url: "https://pub.dartlang.org"
89+
source: hosted
90+
version: "1.0.0"
8491
path:
8592
dependency: transitive
8693
description:
8794
name: path
8895
url: "https://pub.dartlang.org"
8996
source: hosted
9097
version: "1.8.0"
98+
provider:
99+
dependency: "direct main"
100+
description:
101+
name: provider
102+
url: "https://pub.dartlang.org"
103+
source: hosted
104+
version: "4.3.3"
91105
sky_engine:
92106
dependency: transitive
93107
description: flutter
@@ -151,3 +165,4 @@ packages:
151165
version: "2.1.0"
152166
sdks:
153167
dart: ">=2.12.0 <3.0.0"
168+
flutter: ">=1.16.0"

0 commit comments

Comments
 (0)