-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenupage.dart
116 lines (110 loc) · 3.75 KB
/
menupage.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'package:coffee_masters/datamanager.dart';
import 'package:coffee_masters/datamodel.dart';
import 'package:flutter/material.dart';
class MenuPage extends StatelessWidget {
final DataManager dataManager;
const MenuPage({Key key, @required this.dataManager}) : super(key: key);
@override
Widget build(BuildContext context) {
// var p = Product(id: 1, name: "Noodle", price: 10, image: "");
// var p1 = Product(id: 1, name: "Black Coffee", price: 20, image: "");
return FutureBuilder(
future: dataManager.getMenu(),
builder: ((context, snapshot) {
if (snapshot.hasData) {
// var categories = snapshot.data as List<Category>;
// return Text("There are ${categories.length}");
var categories = snapshot.data as List<Category>;
return ListView.builder(
itemCount: categories.length,
itemBuilder: ((context, index) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(categories[index].name),
),
ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: categories[index].products.length,
itemBuilder: (context, prodIndex) {
var product = categories[index].products[prodIndex];
return ProductItem(
product: product,
onAdd: (addedProduct) {
dataManager.cartAdd(addedProduct);
});
},
)
],
);
}),
);
} else {
if (snapshot.hasError) {
return const Text("Error fetching Data");
} else {
return const CircularProgressIndicator();
}
}
}),
);
}
}
class ProductItem extends StatelessWidget {
final Product product;
// onAdd function, callback function, Widget will lift up value here upon trigger
final Function onAdd;
const ProductItem({
Key key,
@required this.product,
@required this.onAdd,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation: 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.network(
product.imageUrl,
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(product.name),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$${product.price} MMK"),
),
],
),
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: ElevatedButton(
onPressed: () {
onAdd(product);
},
child: const Text("Add"),
),
)
],
)
],
)),
),
);
}
}