Skip to content

Commit

Permalink
feat : enable filter button in favlist page
Browse files Browse the repository at this point in the history
  • Loading branch information
yummjin committed Oct 20, 2024
1 parent 0bced46 commit 57a33e1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 38 deletions.
6 changes: 3 additions & 3 deletions lib/screens/main/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class _MainScreenState extends State<MainScreen> {
.toList();
return products;
} else {
throw Exception('No results found in the response');
return [];
}
} else {
throw Exception('Failed to load products');
// 로드 실패
return [];
}
} catch (e) {
print('Error: ${e.toString()}');
return [];
}
}
Expand Down
109 changes: 74 additions & 35 deletions lib/screens/products/liked_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
// import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart';
import 'package:saphy/models/product.dart';
import 'package:saphy/service/api_service.dart';
import 'package:saphy/service/authentication/secure_storage.dart';
Expand All @@ -22,13 +23,13 @@ class _LikedListPageState extends State<LikedListPage> {
late Future<List<Product>> _products;
int cnt = 0;

Future<List<Product>> getProducts() async {
Future<List<Product>> getProducts(String url) async {
String token = await readJwt();
token = token.toString().split(" ")[2];

try {
final response = await APIService.instance.request(
'https://saphy.site/item-wishes?type=ALL',
'https://saphy.site/item-wishes?type=$url',
DioMethod.get,
contentType: 'application/json',
token: "Bearer $token",
Expand All @@ -55,12 +56,12 @@ class _LikedListPageState extends State<LikedListPage> {
@override
void initState() {
super.initState();
_products = getProducts();
countProducts();
_products = getProducts("ALL");
countProducts("ALL");
}

Future<void> countProducts() async {
List<Product> products = await getProducts();
Future<void> countProducts(String url) async {
List<Product> products = await getProducts(url);
setState(() {
cnt = products.length;
});
Expand All @@ -86,26 +87,39 @@ class _LikedListPageState extends State<LikedListPage> {
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
buildFilterButton("전체"),
const SizedBox(width: 10),
buildFilterButton("스마트폰"),
const SizedBox(width: 10),
buildFilterButton("스마트폰"),
const SizedBox(width: 10),
buildFilterButton("스마트폰"),
const SizedBox(width: 10),
buildFilterButton("스마트폰"),
const SizedBox(width: 10),
buildFilterButton("스마트폰"),
const SizedBox(width: 10),
buildFilterButton("전체", "ALL"),
const SizedBox(
width: 10,
),
buildFilterButton("스마트폰", "PHONE"),
const SizedBox(
width: 10,
),
buildFilterButton("태블릿", "TABLET"),
const SizedBox(
width: 10,
),
buildFilterButton("노트북", "LAPTOP"),
const SizedBox(
width: 10,
),
buildFilterButton("음향기기", "headphone-3d"),
const SizedBox(
width: 10,
),
buildFilterButton("웨어러블", "wearable-3d"),
],
),
),
),
),
),
const SliverToBoxAdapter(
child: SizedBox(height: 10),
),
_buildSorter(),
SliverPadding(
padding: const EdgeInsets.all(20),
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
sliver: SliverToBoxAdapter(
child: Wrap(
direction: Axis.horizontal,
Expand Down Expand Up @@ -160,23 +174,48 @@ class _LikedListPageState extends State<LikedListPage> {
);
}

Container buildFilterButton(String label) {
return Container(
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: gray300, width: 1),
color: white,
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: () {},
),
],
),
),
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),
);
}

InkWell buildFilterButton(String label, String url) {
return InkWell(
onTap: () {
setState(() {
// URL에 따라 제품을 다시 가져오고, 상태를 업데이트합니다.
_products = getProducts(url);
countProducts(url); // 필터에 맞는 제품 개수를 카운트합니다.
});
},
child: 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: textStyle(15, false, null)),
),
),
),
Expand Down

0 comments on commit 57a33e1

Please sign in to comment.