wordpress/lib/viewmodel/product_home_page_model.dart
2025-10-16 11:27:27 +05:30

125 lines
3.1 KiB
Dart

class ProductHomePageModel {
final List<BannerModel> banners;
final List<CategoryModel> categories;
final List<ProductModel> featured;
final List<ProductModel> latest;
ProductHomePageModel({
required this.banners,
required this.categories,
required this.featured,
required this.latest,
});
factory ProductHomePageModel.fromJson(Map<String, dynamic> json) {
return ProductHomePageModel(
banners: (json['banners'] is List)
? (json['banners'] as List)
.map((e) => BannerModel.fromJson(e))
.toList()
: [],
categories: (json['categories'] is List)
? (json['categories'] as List)
.map((e) => CategoryModel.fromJson(e))
.toList()
: [],
featured: (json['featured_products'] is List)
? (json['featured_products'] as List)
.map((e) => ProductModel.fromJson(e))
.toList()
: [],
latest: (json['latest_products'] is List)
? (json['latest_products'] as List)
.map((e) => ProductModel.fromJson(e))
.toList()
: [],
);
}
Map<String, dynamic> toJson() => {
'banners': banners.map((e) => e.toJson()).toList(),
'categories': categories.map((e) => e.toJson()).toList(),
'featured_products': featured.map((e) => e.toJson()).toList(),
'latest_products': latest.map((e) => e.toJson()).toList(),
};
}
/// Banner Model
class BannerModel {
final int id;
final String image;
BannerModel({required this.id, required this.image});
factory BannerModel.fromJson(Map<String, dynamic> json) {
return BannerModel(
id: json['id'] ?? 0,
image: (json['image'] is String) ? json['image'] : '',
);
}
Map<String, dynamic> toJson() => {'id': id, 'image': image};
}
/// Category Model
class CategoryModel {
final int id;
final String name;
final String slug;
final String? image; // can be null/false
CategoryModel({
required this.id,
required this.name,
required this.slug,
this.image,
});
factory CategoryModel.fromJson(Map<String, dynamic> json) {
return CategoryModel(
id: json['id'] ?? 0,
name: (json['name'] is String) ? json['name'] : '',
slug: (json['slug'] is String) ? json['slug'] : '',
image: (json['image'] is String) ? json['image'] : null,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'slug': slug,
'image': image,
};
}
/// Product Model (for featured & latest)
class ProductModel {
final int id;
final String name;
final String price;
final String image;
ProductModel({
required this.id,
required this.name,
required this.price,
required this.image,
});
factory ProductModel.fromJson(Map<String, dynamic> json) {
return ProductModel(
id: json['id'] ?? 0,
name: (json['name'] is String) ? json['name'] : '',
price: (json['price'] is String) ? json['price'] : '',
image: (json['image'] is String) ? json['image'] : '',
);
}
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'price': price,
'image': image,
};
}