-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from 2024-Saphy/feat/SAPHY-33-product-favlist
[FEAT] : Product API integration
- Loading branch information
Showing
18 changed files
with
582 additions
and
434 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
description: This file stores settings for Dart & Flutter DevTools. | ||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states | ||
extensions: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,203 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:saphy/utils/colors.dart'; | ||
import 'package:saphy/utils/textstyles.dart'; | ||
import 'package:saphy/widgets/product_card.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:saphy/models/product.dart'; | ||
|
||
class ItemListPage extends StatefulWidget { | ||
final String name, url; | ||
const ItemListPage({super.key, required this.url, required this.name}); | ||
|
||
@override | ||
State<ItemListPage> createState() => _ItemListPageState(); | ||
} | ||
|
||
class _ItemListPageState extends State<ItemListPage> { | ||
late Future<List<Product>> _products; | ||
int cnt = 0; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_products = getProducts(); | ||
countProducts(); | ||
} | ||
|
||
Future<void> countProducts() async { | ||
List<Product> products = await getProducts(); | ||
setState(() { | ||
cnt = products.length; | ||
}); | ||
} | ||
|
||
Future<List<Product>> getProducts() async { | ||
final dio = Dio(); | ||
try { | ||
final response = await dio | ||
.get('https://saphy.site/api/items?deviceType=${widget.url}&size=20'); | ||
if (response.statusCode == 200) { | ||
final data = response.data as Map<String, dynamic>; | ||
if (data['results'] != null) { | ||
List<Product> products = (data['results'] as List) | ||
.map((item) => Product.fromJson(item)) | ||
.toList(); | ||
return products; | ||
} else { | ||
throw Exception('No results found in the response'); | ||
} | ||
} else { | ||
throw Exception('Failed to load products'); | ||
} | ||
} catch (e) { | ||
return []; | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
backgroundColor: const Color(0xfff4f4f4), | ||
appBar: _appBar(), | ||
body: CustomScrollView( | ||
slivers: [ | ||
_buildHeader(), | ||
const SliverToBoxAdapter( | ||
child: SizedBox(height: 10), | ||
), | ||
_buildSorter(), | ||
_buildProductGrid(), | ||
], | ||
), | ||
); | ||
} | ||
|
||
AppBar _appBar() { | ||
return AppBar( | ||
automaticallyImplyLeading: false, | ||
backgroundColor: altWhite, | ||
title: Padding( | ||
padding: const EdgeInsets.only(top: 10.0), | ||
child: Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
IconButton( | ||
icon: const Icon( | ||
Icons.arrow_back_ios, | ||
size: 25, | ||
), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
}, | ||
), | ||
IconButton( | ||
icon: const Icon( | ||
Icons.search, | ||
size: 25, | ||
), | ||
onPressed: () {}, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
SliverToBoxAdapter _buildHeader() { | ||
return SliverToBoxAdapter( | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric( | ||
horizontal: 30.0, | ||
), | ||
child: Text( | ||
widget.name, | ||
style: titleText(), | ||
), | ||
), | ||
); | ||
} | ||
|
||
SliverPadding _buildSorter() { | ||
return SliverPadding( | ||
padding: const EdgeInsets.symmetric(horizontal: 30), | ||
sliver: SliverToBoxAdapter( | ||
child: Row( | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
Text( | ||
'상품 $cnt', | ||
style: subTitleText(), | ||
), | ||
IconButton( | ||
icon: const Icon(Icons.sort_outlined), | ||
onPressed: () {}, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
SliverPadding _buildProductGrid() { | ||
return SliverPadding( | ||
padding: const EdgeInsets.only(bottom: 50, right: 20, left: 20, top: 10), | ||
sliver: SliverToBoxAdapter( | ||
child: Wrap( | ||
direction: Axis.horizontal, | ||
alignment: WrapAlignment.spaceBetween, | ||
spacing: 15, | ||
runSpacing: 15, | ||
children: [ | ||
FutureBuilder( | ||
future: _products, | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const Center(child: CircularProgressIndicator()); | ||
} else if (snapshot.hasError) { | ||
return Text('Error: ${snapshot.error.toString()}'); | ||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) { | ||
return const Center(child: Text('상품이 없습니다')); | ||
} else { | ||
final products = snapshot.data!; | ||
return Wrap( | ||
direction: Axis.horizontal, | ||
alignment: WrapAlignment.spaceBetween, | ||
spacing: 15, | ||
runSpacing: 15, | ||
children: products | ||
.map((product) => ProductCard(product: product)) | ||
.toList(), | ||
); | ||
} | ||
}) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
Container buildFilterButton(String label) { | ||
return Container( | ||
height: 45, | ||
decoration: BoxDecoration( | ||
borderRadius: BorderRadius.circular(20), | ||
border: Border.all(color: gray300, width: 1), | ||
color: white, | ||
), | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric(horizontal: 13.0, vertical: 8), | ||
child: Center( | ||
child: Text( | ||
label, | ||
style: const TextStyle( | ||
fontFamily: "Pretendard", | ||
fontSize: 15, | ||
fontWeight: FontWeight.bold), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.