fetching by subcategories

This commit is contained in:
meylis98 2023-03-01 00:58:40 +05:00
parent 8bc1d2c4d7
commit b1f9e7a533
7 changed files with 128 additions and 36 deletions

View File

@ -1,4 +1,5 @@
import 'package:adaptix/adaptix.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:sapaly_shop/services/app_constants.dart';
import 'package:sapaly_shop/themes/app_theme.dart';
@ -39,10 +40,16 @@ class ProductCard extends StatelessWidget {
topLeft: Radius.circular(15.adaptedPx()),
topRight: Radius.circular(15.adaptedPx()),
),
child: Image.network(
imageUrl,
child: CachedNetworkImage(
imageUrl: imageUrl,
fit: BoxFit.fitWidth,
alignment: Alignment.center,
placeholder: (context, url) => Container(
height: AppConstants.deviceHeight(context) / 7,
),
errorWidget: (context, url, error) => Container(
height: AppConstants.deviceHeight(context) / 7,
),
height: AppConstants.deviceHeight(context) / 7,
width: double.infinity,
),
@ -55,17 +62,21 @@ class ProductCard extends StatelessWidget {
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
price,
maxLines: 2,
softWrap: true,
style:
Theme.of(context).primaryTextTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w600,
color: AppTheme.blackColor,
fontSize: 13.adaptedPx(),
),
),
price != null
? Text(
price,
maxLines: 2,
softWrap: true,
style: Theme.of(context)
.primaryTextTheme
.bodyMedium
?.copyWith(
fontWeight: FontWeight.w600,
color: AppTheme.blackColor,
fontSize: 13.adaptedPx(),
),
)
: Container(),
SizedBox(width: 5.adaptedPx()),
discountPriceValue == 0
? Container()

View File

@ -5,11 +5,13 @@ class CategoryModel {
required this.id,
required this.name,
required this.translations,
required this.icon,
});
final int id;
final String name;
final List<Translation> translations;
final Iconca icon;
factory CategoryModel.fromJson(
Map<String, dynamic> json, String currentLang) {
@ -28,10 +30,31 @@ class CategoryModel {
translations: (json['translations'] as List)
.map((e) => Translation.fromJson(e))
.toList(),
icon: Iconca.fromJson(json["icon"] ?? ""),
);
}
}
class Iconca {
Iconca({
required this.id,
required this.path,
});
final int id;
final String path;
factory Iconca.fromJson(Map<String, dynamic> json) => Iconca(
id: json["id"] ?? 0,
path: json["path"],
);
Map<String, dynamic> toJson() => {
"id": id,
"path": path,
};
}
class Translation {
Translation({
required this.locale,

View File

@ -0,0 +1,57 @@
import 'dart:convert';
class SubcategoryModel {
SubcategoryModel({
required this.id,
required this.name,
required this.translations,
});
final int id;
final String name;
final List<Translation> translations;
factory SubcategoryModel.fromJson(
Map<String, dynamic> json, String currentLang) {
final List<Translation> translations = (json['translations'] as List)
.map((e) => Translation.fromJson(e))
.toList();
String language = json['name'] ?? '';
for (Translation item in translations) {
if (item.locale == currentLang) language = item.attributeData['name'];
}
return SubcategoryModel(
id: json["id"] ?? '',
name: language,
translations: (json['translations'] as List)
.map((e) => Translation.fromJson(e))
.toList(),
);
}
}
class Translation {
Translation({
required this.locale,
required this.modelId,
required this.attributeData,
});
final String locale;
final String modelId;
final Map attributeData;
factory Translation.fromJson(Map<String, dynamic> jsn) => Translation(
locale: jsn["locale"],
modelId: jsn["model_id"],
attributeData: json.decode(jsn["attribute_data"]),
);
Map<String, dynamic> toJson() => {
"locale": locale,
"model_id": modelId,
"attribute_data": attributeData,
};
}

View File

@ -90,17 +90,17 @@ class _CategoryScreenState extends State<CategoryScreen> {
),
child: Column(
children: [
// Expanded(
// flex: 3,
// child: CachedNetworkImage(
// imageUrl: categories[index].icon.path,
// width: MediaQuery.of(context).size.width / 7,
// fit: BoxFit.contain,
// progressIndicatorBuilder:
// (context, url, progress) => const Center(
// child: CircularProgressIndicator()),
// ),
// ),
Expanded(
flex: 3,
child: CachedNetworkImage(
imageUrl: categories[index].icon.path,
width: MediaQuery.of(context).size.width / 7,
fit: BoxFit.contain,
progressIndicatorBuilder:
(context, url, progress) => const Center(
child: CircularProgressIndicator()),
),
),
Expanded(
flex: 1,
child: Text(

View File

@ -7,6 +7,7 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:sapaly_shop/models/settings_model.dart';
import 'package:sapaly_shop/models/sliders_model.dart';
import 'package:sapaly_shop/models/subcategory_model.dart';
import 'package:sapaly_shop/screens/drawer/sapaly_drawer.dart';
import 'package:sapaly_shop/screens/home/product.dart';
import 'package:sapaly_shop/screens/home/search.dart';
@ -30,7 +31,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
int categoryId = 0;
bool hasError = false;
TabController? tabController;
List<CategoryModel> categories = [];
List<SubcategoryModel> categories = [];
List<SlidersModel> sliders = [];
Future<void> getSliders() async {
@ -46,9 +47,9 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
}
}
Future<void> getCategories() async {
Future<void> getSubCategories() async {
try {
var list = await API().getCategories();
var list = await API().getSubCategories();
if (mounted) {
setState(() {
@ -68,7 +69,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
@override
void initState() {
getSliders();
getCategories();
getSubCategories();
super.initState();
}
@ -91,11 +92,11 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
actionOnTap: () {},
),
body: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
vertical: AppConstants.verticalPadding(context) / 2,
vertical: AppConstants.verticalPadding(context),
horizontal: AppConstants.horizontalPadding(context),
),
physics: const BouncingScrollPhysics(),
child: Column(
children: [
Container(
@ -233,7 +234,6 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
SizedBox(height: 15.adaptedPx()),
SizedBox(
height: AppConstants.deviceHeight(context),
width: double.infinity,
child: tabController != null
? TabBarView(
controller: tabController,

View File

@ -31,7 +31,6 @@ class _ProductsState extends State<Products> {
@override
void initState() {
getProductsByCategory(widget.id);
print('category id :${widget.id}');
super.initState();
}
@ -44,6 +43,7 @@ class _ProductsState extends State<Products> {
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 4 / 5,
),
itemCount: products.length,
itemBuilder: (context, index) {

View File

@ -10,6 +10,7 @@ import 'package:sapaly_shop/services/app_constants.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/product_model.dart';
import '../models/subcategory_model.dart';
class API {
static Future<dynamic> _getData(String path) async {
@ -58,13 +59,13 @@ class API {
}
}
Future<List<CategoryModel>> getSubCategories(int id) async {
Future<List<SubcategoryModel>> getSubCategories() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String currentLanguage = prefs.getString('language') ?? 'ru';
try {
final categories = await _getData("v1/category/sub/$id");
final categories = await _getData("v1/category/sub/19");
return (categories["data"] as List)
.map((item) => CategoryModel.fromJson(item, currentLanguage))
.map((item) => SubcategoryModel.fromJson(item, currentLanguage))
.toList();
} catch (e) {
debugPrint('Category ' + e.toString());
@ -76,7 +77,7 @@ class API {
SharedPreferences prefs = await SharedPreferences.getInstance();
String currentLanguage = prefs.getString('language') ?? 'ru';
try {
final categories = await _getData("v1/category/sub/19");
final categories = await _getData("v1/categories");
return (categories["data"] as List)
.map((item) => CategoryModel.fromJson(item, currentLanguage))