Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] modify product model #44

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions lib/models/product.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
class Product {
int? id;
String? brand;
String? name;
String? description;
String? color;
String? storage;
String? grade;
String? imageUrl;
double? price;
int? stock;
int id;
String deviceType;
String name;
String description;
double price;
int stock;
Map<String, String> images; // 이미지 정보를 Map<String, String>으로 저장
String brand;
String color;
String storage;
String grade;

Product({
this.id,
this.brand,
this.name,
this.description,
this.color,
this.storage,
this.grade,
this.imageUrl,
this.price,
this.stock,
required this.id,
required this.deviceType,
required this.name,
required this.description,
required this.price,
required this.stock,
required this.images,
required this.brand,
required this.color,
required this.storage,
required this.grade,
});

Product.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
brand = json['brand'];
name = json['name'];
description = json['description'];
color = json['color'];
storage = json['storage'];
grade = json['grade'];
imageUrl = json['imageUrl'];
price = double.parse(json['price']);
stock = int.parse(json['stock']);
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
deviceType: json['deviceType'],
name: json['name'],
description: json['description'],
price: json['price'],
stock: json['stock'],
images: Map<String, String>.from(json['images']), // 직접 변환
brand: json['brand'],
color: json['color'],
storage: json['storage'],
grade: json['grade'],
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['id'] = id;
data['brand'] = brand;
data['name'] = name;
data['description'] = description;
data['color'] = color;
data['storage'] = storage;
data['grade'] = grade;
data['imageUrl'] = imageUrl;
data['price'] = price;
data['stock'] = stock;
return data;
return {
'id': id,
'deviceType': deviceType,
'name': name,
'description': description,
'price': price,
'stock': stock,
'images': images, // 직접 저장
'brand': brand,
'color': color,
'storage': storage,
'grade': grade,
};
}
}
129 changes: 43 additions & 86 deletions lib/screens/main/main_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:dio/dio.dart';
import 'package:saphy/models/product.dart';
import 'package:saphy/utils/colors.dart';
import 'package:saphy/utils/textstyles.dart';
Expand All @@ -16,87 +19,25 @@ class MainScreen extends StatefulWidget {
class _MainScreenState extends State<MainScreen> {
final NumberFormat numberFormat = NumberFormat('###,###,###,###');
final int productLength = 6;
List<Product> productList = [
// 그냥 구현용 샘플 데이터
Product(
id: 1,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Black',
storage: '128GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 250000,
stock: 50,
),
Product(
id: 2,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'White',
storage: '128GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 250000,
stock: 45,
),
Product(
id: 3,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Blue',
storage: '256GB',
grade: 'B',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 270000,
stock: 40,
),
Product(
id: 4,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Red',
storage: '256GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 280000,
stock: 35,
),
Product(
id: 5,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Green',
storage: '128GB',
grade: 'C',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 240000,
stock: 30,
),
Product(
id: 6,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Pink',
storage: '512GB',
grade: 'B',
imageUrl:
// Future<List<Product>> getProduct = getProducts();

Product product = Product(
id: 1,
name: "iPhone 14",
brand: "Apple",
images: {
"name": "iPhone 14",
'url':
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 290000,
stock: 20,
),
];
},
price: 130000.0,
description: "Latest model from Apple with advanced features.",
color: "Green",
storage: "128GB",
grade: "A",
deviceType: "phone",
stock: 10,
);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -127,12 +68,9 @@ class _MainScreenState extends State<MainScreen> {
spacing: 15,
runSpacing: 15,
children: [
for (int i = 0; i < productLength; i++)
ProductCard(
brand: productList[i].brand ?? "",
name: productList[i].name ?? "",
imageUrl: productList[i].imageUrl ?? "",
price: productList[i].price ?? 0),
ProductCard(
product: product,
)
],
),
),
Expand All @@ -141,4 +79,23 @@ class _MainScreenState extends State<MainScreen> {
),
);
}

Future<List<Product>> getProducts() async {
final dio = Dio();
try {
final response = await dio.get('https://saphy.site/item-wishes');
if (response.statusCode == 200) {
List<Product> products = (response.data as List)
.map((item) => Product.fromJson(item))
.toList();
return products;
} else {
throw Exception('Failed to load products');
}
} catch (e) {
// ignore: avoid_print
print('Error: $e');
return []; // Return an empty list in case of error
}
}
}
94 changes: 3 additions & 91 deletions lib/screens/products/liked_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,6 @@ class LikedListPage extends StatefulWidget {
class _LikedListPageState extends State<LikedListPage> {
final NumberFormat numberFormat = NumberFormat('###,###,###,###');
final int productLength = 6;
List<Product> productList = [
// 그냥 구현용 샘플 데이터
Product(
id: 1,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Black',
storage: '128GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 250000,
stock: 50,
),
Product(
id: 2,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'White',
storage: '128GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 250000,
stock: 45,
),
Product(
id: 3,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Blue',
storage: '256GB',
grade: 'B',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 270000,
stock: 40,
),
Product(
id: 4,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Red',
storage: '256GB',
grade: 'A',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 280000,
stock: 35,
),
Product(
id: 5,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Green',
storage: '128GB',
grade: 'C',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 240000,
stock: 30,
),
Product(
id: 6,
brand: "Apple",
name: "iPhone 13",
description: 'Latest iPhone model with A15 Bionic chip',
color: 'Pink',
storage: '512GB',
grade: 'B',
imageUrl:
'https://i.pinimg.com/564x/f3/54/dc/f354dc1f040fc1fc4dfc4c436ad52159.jpg',
price: 290000,
stock: 20,
),
];

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -290,22 +209,15 @@ class _LikedListPageState extends State<LikedListPage> {
),
),
),
SliverPadding(
padding: const EdgeInsets.all(20),
const SliverPadding(
padding: EdgeInsets.all(20),
sliver: SliverToBoxAdapter(
child: Wrap(
direction: Axis.horizontal,
alignment: WrapAlignment.spaceBetween,
spacing: 15,
runSpacing: 15,
children: [
for (int i = 0; i < productLength; i++)
ProductCard(
brand: productList[i].brand ?? "",
name: productList[i].name ?? "",
imageUrl: productList[i].imageUrl ?? "",
price: productList[i].price ?? 0),
],
children: [],
),
),
),
Expand Down
Loading
Loading