Skip to content

Commit 33ceee9

Browse files
committed
user products 화면에서 refresh 기능 추가 #14
1 parent e20763c commit 33ceee9

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

shop_app/lib/providers/products.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Products with ChangeNotifier {
2121
}
2222

2323
Future<void> fetchAndSetProducts() async {
24-
const url = '';
24+
const url =
25+
'https://flutter-shop-app-c87f7-default-rtdb.firebaseio.com/products.json';
2526
try {
2627
final response = await http.get(Uri.parse(url));
2728
final extractedData = json.decode(response.body) as Map<String, dynamic>;
@@ -46,7 +47,8 @@ class Products with ChangeNotifier {
4647
}
4748

4849
Future<void> addProduct(Product product) async {
49-
const url = '';
50+
const url =
51+
'https://flutter-shop-app-c87f7-default-rtdb.firebaseio.com/products.json';
5052
try {
5153
final response = await http.post(Uri.parse(url),
5254
body: json.encode({

shop_app/lib/screens/user_product_screen.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import 'package:shop_app/widgets/user_product_item.dart';
88
class UserProductsScreen extends StatelessWidget {
99
static const String routeId = '/user_products';
1010

11+
Future<void> _refreshProudcts(BuildContext context) async {
12+
await Provider.of<Products>(context, listen: false).fetchAndSetProducts();
13+
}
14+
1115
@override
1216
Widget build(BuildContext context) {
1317
final productsData = Provider.of<Products>(context);
@@ -22,20 +26,23 @@ class UserProductsScreen extends StatelessWidget {
2226
}),
2327
],
2428
),
25-
body: Padding(
26-
padding: EdgeInsets.all(8),
27-
child: ListView.builder(
28-
itemCount: productsData.items.length,
29-
itemBuilder: (_, index) => Column(children: [
30-
UserProductItem(
31-
productsData.items[index].id,
32-
productsData.items[index].title,
33-
productsData.items[index].imageUrl,
34-
),
35-
Divider(
36-
height: 5,
37-
)
38-
]),
29+
body: RefreshIndicator(
30+
onRefresh: () => _refreshProudcts(context),
31+
child: Padding(
32+
padding: EdgeInsets.all(8),
33+
child: ListView.builder(
34+
itemCount: productsData.items.length,
35+
itemBuilder: (_, index) => Column(children: [
36+
UserProductItem(
37+
productsData.items[index].id,
38+
productsData.items[index].title,
39+
productsData.items[index].imageUrl,
40+
),
41+
Divider(
42+
height: 5,
43+
)
44+
]),
45+
),
3946
),
4047
),
4148
drawer: AppDrawer(),

0 commit comments

Comments
 (0)