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 total price to busket page with bloc #66

Merged
merged 1 commit into from
May 7, 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
9 changes: 7 additions & 2 deletions lib/bloc/busket/busket_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:apple_shop/bloc/busket/busket_event.dart';
import 'package:apple_shop/bloc/busket/busket_state.dart';
import 'package:apple_shop/data/datasource/busket_repository.dart';
import 'package:apple_shop/data/repository/busket_repository.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Expand All @@ -9,9 +9,14 @@ class BusketBloc extends Bloc<BusketEvent, BusketState> {

BusketBloc() : super(InitialBusketState()) {
on<FetchBusketEvent>((event, emit) async {

var response = await _repository.fetchBucketList();
var total_price = await _repository.getTotalPrice();

emit(ResponsebusketState(response: response));
emit(ResponsebusketState(
response: response,
total_price: total_price,
));
});
}
}
3 changes: 2 additions & 1 deletion lib/bloc/busket/busket_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class InitialBusketState extends BusketState {}

class ResponsebusketState extends BusketState {
Either<String, List<Bucket>> response;
int total_price;

ResponsebusketState({
required this.response,
required this.total_price,
});

}
2 changes: 1 addition & 1 deletion lib/bloc/product/product_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:apple_shop/bloc/product/product_event.dart';
import 'package:apple_shop/bloc/product/product_state.dart';
import 'package:apple_shop/data/datasource/busket_repository.dart';
import 'package:apple_shop/data/repository/busket_repository.dart';
import 'package:apple_shop/data/repository/product_detail_repository.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down
13 changes: 11 additions & 2 deletions lib/data/datasource/busket_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import 'package:hive/hive.dart';
abstract class IBusketDataSource {
Future<void> addProductToBusket(Product product);
Future<List<Bucket>> fetchBucketList();
Future<int> getTotalPrice();
}

class BusketDataSource extends IBusketDataSource {
@override
Future<void> addProductToBusket(Product product) async {
var box = Hive.box<Bucket>("BucketBox");

var item = Bucket(
id: product.id,
collectionId: product.collectionId,
Expand All @@ -26,10 +28,17 @@ class BusketDataSource extends IBusketDataSource {

@override
Future<List<Bucket>> fetchBucketList() async {

var box = Hive.box<Bucket>("BucketBox");
List<Bucket> response = box.values.toList();
return response;

}

@override
Future<int> getTotalPrice() async {
var box = Hive.box<Bucket>("BucketBox").values.toList();
var totalPrice = box.fold(0, (calculator, product) {
return calculator + product.realPrice!;
});
return totalPrice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import 'package:dartz/dartz.dart';
abstract class IBusketRepository {
Future<Either<String, String>> addProductToBusket(Product product);
Future<Either<String, List<Bucket>>> fetchBucketList();
Future<int> getTotalPrice();


}

class BusketRepository extends IBusketRepository {
Expand All @@ -31,4 +34,13 @@ class BusketRepository extends IBusketRepository {
return const Left("خطایی رخ داده است");
}
}

@override
Future<int> getTotalPrice() async {

var response=_dataSuorce.getTotalPrice();
return response;

}

}
2 changes: 1 addition & 1 deletion lib/di/service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:apple_shop/bloc/busket/busket_bloc.dart';
import 'package:apple_shop/data/datasource/authentication_datasource.dart';
import 'package:apple_shop/data/datasource/banner_datasource.dart';
import 'package:apple_shop/data/datasource/busket_datasource.dart';
import 'package:apple_shop/data/datasource/busket_repository.dart';
import 'package:apple_shop/data/repository/busket_repository.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';
Expand Down
30 changes: 16 additions & 14 deletions lib/screan/order_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,25 @@ class OrderScrean extends StatelessWidget {
}
],
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: MyColors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
if (state is ResponsebusketState) ...{
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: MyColors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
onPressed: () {},
child: const Text(
"ادامه فرآیند خرید",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: "sm",
onPressed: () {},
child: Text(
"${state.total_price}",
style: const TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: "sm",
),
),
),
),
}
],
);
},
Expand Down
Loading