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

add screan product list to project #62

Merged
merged 1 commit into from
May 4, 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
17 changes: 17 additions & 0 deletions lib/bloc/category_product/category_product_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:apple_shop/bloc/category_product/category_product_event.dart';
import 'package:apple_shop/bloc/category_product/category_product_state.dart';
import 'package:apple_shop/data/repository/product_category_repository.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ProductCategoryBloc
extends Bloc<ProductCategoryEvent, ProductCategoryState> {
ProductCategoryBloc() : super(LoadingProductCategoeyState()) {
IProductCategoryRepostory _repository = locator.get();

on<GetProductCategoryEvent>((event, emit) async {
var product = await _repository.category_product(event.categoryId);
emit(ResultProductCategoeyState(product: product));
});
}
}
9 changes: 9 additions & 0 deletions lib/bloc/category_product/category_product_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
abstract class ProductCategoryEvent {}

class GetProductCategoryEvent extends ProductCategoryEvent {
String categoryId;

GetProductCategoryEvent({
required this.categoryId,
});
}
16 changes: 16 additions & 0 deletions lib/bloc/category_product/category_product_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:apple_shop/data/models/product_model.dart';
import 'package:dartz/dartz.dart';

abstract class ProductCategoryState {}

class InitProductCategoryState extends ProductCategoryState {}

class LoadingProductCategoeyState extends ProductCategoryState {}

class ResultProductCategoeyState extends ProductCategoryState {
Either<String, List<Product>> product;

ResultProductCategoeyState({
required this.product,
});
}
37 changes: 37 additions & 0 deletions lib/data/datasource/product_category_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:apple_shop/data/models/product_model.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/util/api_exception.dart';
import 'package:dio/dio.dart';

abstract class IProductCategoryDataSource {
Future<List<Product>> category_product(String categoryId);
}

class ProductCategoryDataSource extends IProductCategoryDataSource {
Dio _dio = locator.get();

@override
Future<List<Product>> category_product(String categoryId) async {
try {
Map<String, String> qparam = {"filter": 'category="${categoryId}"'};
var response = await _dio.get(
"collections/products/records",
queryParameters: qparam,
);

return response.data["items"].map<Product>((jsonObject) {
return Product.fromJson(jsonObject);
}).toList();
} on DioException catch (ex) {
throw ApiExceptiopn(
code: ex.response!.statusCode!,
messgae: ex.response!.data["message"],
);
} catch (ex) {
throw ApiExceptiopn(
code: 0,
messgae: "خطا محتوای متنی ندارد",
);
}
}
}
24 changes: 24 additions & 0 deletions lib/data/repository/product_category_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:apple_shop/data/datasource/product_category_data_source.dart';
import 'package:apple_shop/data/models/product_model.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/util/api_exception.dart';
import 'package:dartz/dartz.dart';

abstract class IProductCategoryRepostory {
Future<Either<String, List<Product>>> category_product(String categoryId);
}

class ProductCategoryRepository extends IProductCategoryRepostory {
IProductCategoryDataSource _dataSource = locator.get();
@override
Future<Either<String, List<Product>>> category_product(
String categoryId) async {
try {
var response = await _dataSource.category_product(categoryId);

return Right(response);
} on ApiExceptiopn catch (ex) {
return Left(ex.messgae);
}
}
}
5 changes: 5 additions & 0 deletions lib/di/service_locator.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:apple_shop/data/datasource/authentication_datasource.dart';
import 'package:apple_shop/data/datasource/banner_datasource.dart';
import 'package:apple_shop/data/datasource/product_category_data_source.dart';
import 'package:apple_shop/data/datasource/product_detail_datasource.dart';
import 'package:apple_shop/data/repository/banner_repository.dart';
import 'package:apple_shop/data/datasource/category_data_source.dart';
import 'package:apple_shop/data/datasource/product_data_source.dart';
import 'package:apple_shop/data/repository/authentication_repository.dart';
import 'package:apple_shop/data/repository/category_repository.dart';
import 'package:apple_shop/data/repository/product_category_repository.dart';
import 'package:apple_shop/data/repository/product_detail_repository.dart';
import 'package:apple_shop/data/repository/product_repository.dart';
import 'package:dio/dio.dart';
Expand All @@ -29,13 +31,16 @@ Future<void> initLocator() async {
return ProductDetailRemoteDatatSource();
});

locator.registerFactory<IProductCategoryDataSource>(() => ProductCategoryDataSource());

//Repository Resource
locator.registerFactory<IAuthenticatinRepository>(
() => AuthenticatinRepository());
locator.registerFactory<ICategoryRepository>(() => CategoryRepository());
locator.registerFactory<IBannerRepository>(() => BannerRemoteRepository());
locator.registerFactory<IProductRepository>(() => ProductRemoteRepository());
locator.registerFactory<IProductDetailRespotory>(() => ProductDetailRepository());
locator.registerFactory<IProductCategoryRepostory>(() => ProductCategoryRepository());
//components
locator.registerSingleton<SharedPreferences>(
await SharedPreferences.getInstance());
Expand Down
22 changes: 17 additions & 5 deletions lib/screan/category_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ class MyWidget extends StatelessWidget {
return SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 20),
sliver: SliverGrid(
delegate: SliverChildBuilderDelegate((context, index) {
return CashNetworkImage(url: test[index].thumbnail,radius: 0,);
}, childCount: test.length),
delegate: SliverChildBuilderDelegate(
(context, index) {
return CashNetworkImage(
url: test[index].thumbnail,
radius: 0,
);
},
childCount: test.length,
),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
Expand All @@ -99,7 +105,10 @@ class MyWidget extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 20),
sliver: SliverGrid(
delegate: SliverChildBuilderDelegate((context, index) {
return CashNetworkImage(url: test[index].thumbnail,radius: 0,);
return CashNetworkImage(
url: test[index].thumbnail,
radius: 0,
);
}, childCount: test.length),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
Expand All @@ -116,7 +125,10 @@ class MyWidget extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 20),
sliver: SliverGrid(
delegate: SliverChildBuilderDelegate((context, index) {
return CashNetworkImage(url: test[index].thumbnail,radius: 0,);
return CashNetworkImage(
url: test[index].thumbnail,
radius: 0,
);
}, childCount: test.length),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
Expand Down
5 changes: 3 additions & 2 deletions lib/screan/detail_prodoct_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ class _DetailProductScreanState extends State<DetailProductScrean> {
height: 26,
width: 26,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(5)),
color: Colors.green,
borderRadius: BorderRadius.circular(5),
),
),
),
Positioned(
Expand Down
2 changes: 1 addition & 1 deletion lib/screan/home_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class _HomeWidgetState extends State<HomeWidget> {
scrollDirection: Axis.horizontal,
itemCount: r.length,
itemBuilder: (BuildContext context, int index) {
return CategoryHorizontalItem(index, r);
return CategoryHorizontalItem(index, r,context);
},
),
),
Expand Down
109 changes: 109 additions & 0 deletions lib/screan/product_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:apple_shop/bloc/category_product/category_product_bloc.dart';
import 'package:apple_shop/bloc/category_product/category_product_event.dart';
import 'package:apple_shop/bloc/category_product/category_product_state.dart';
import 'package:apple_shop/constants/colors.dart';
import 'package:apple_shop/data/models/categori_model.dart';
import 'package:apple_shop/widgets/prodoct_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ProductListScrean extends StatefulWidget {
Category category;
ProductListScrean({
required this.category,
});
@override
State<ProductListScrean> createState() => _ProductListScreanState();
}

class _ProductListScreanState extends State<ProductListScrean> {
@override
void initState() {
BlocProvider.of<ProductCategoryBloc>(context)
.add(GetProductCategoryEvent(categoryId: widget.category.id));

super.initState();
}

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: MyColors.white,
body: BlocBuilder<ProductCategoryBloc, ProductCategoryState>(
builder: (context, state) {
return CustomScrollView(
slivers: [
SliverPadding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 20,
),
sliver: SliverToBoxAdapter(
child: Container(
height: 46,
width: 340,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
const SizedBox(
width: 16,
),
const Image(
image: AssetImage(
"assets/images/apple_haeder_logo.png"),
),
const Spacer(),
Text(
widget.category.title,
textAlign: TextAlign.center,
style: const TextStyle(
fontFamily: "sm",
fontSize: 16,
color: MyColors.blue,
),
),
const Spacer()
],
),
),
),
),
if (state is ResultProductCategoeyState) ...[
state.product.fold((l) {
return SliverToBoxAdapter(
child: Text(l),
);
}, (product) {
return Directionality(
textDirection: TextDirection.rtl,
child: SliverPadding(
padding: EdgeInsets.symmetric(horizontal: 20),
sliver: SliverGrid(
delegate: SliverChildBuilderDelegate((context, index) {
return ProdouctItem(product[index]);
// ProdouctItem(product[index])
}, childCount: product.length),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
),
),
),
);
}),
]
],
);
},
)),
);
}
}
Loading
Loading