addtocart
This commit is contained in:
126
lib/src/logic/provider/addTocart_provider.dart
Normal file
126
lib/src/logic/provider/addTocart_provider.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:grocery_app/src/core/network_services/service_locator.dart';
|
||||
import 'package:grocery_app/src/data/all_cart_items.dart';
|
||||
import 'package:grocery_app/src/logic/repo/product_repo.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class AddtocartProvider extends ChangeNotifier {
|
||||
String _pinCode = "Fetching...";
|
||||
bool _isLoading = false;
|
||||
bool _isDeliverable = false;
|
||||
|
||||
String get pinCode => _pinCode;
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isDeliverable => _isDeliverable;
|
||||
|
||||
Future<void> getCurrentLocation(BuildContext context) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
_pinCode = "Location services disabled.";
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LocationPermission permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
_pinCode = "Permission denied.";
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
);
|
||||
|
||||
print("Location fetched: ${position.latitude}, ${position.longitude}");
|
||||
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(
|
||||
position.latitude,
|
||||
position.longitude,
|
||||
);
|
||||
|
||||
if (placemarks.isNotEmpty) {
|
||||
_pinCode = placemarks.first.postalCode ?? "Unknown";
|
||||
print("Pincode found: $_pinCode");
|
||||
|
||||
// Now check if this pin code is deliverable
|
||||
await checkPin(context, _pinCode);
|
||||
} else {
|
||||
_pinCode = "Could not fetch pin code.";
|
||||
print("Error: No placemarks found.");
|
||||
}
|
||||
} catch (e) {
|
||||
_pinCode = "Error: ${e.toString()}";
|
||||
print("Error: ${e.toString()}");
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> checkPin(BuildContext context, pin) async {
|
||||
var data = {};
|
||||
try {
|
||||
var result = await _homeRepo.checkPin(data, pin);
|
||||
|
||||
return result.fold(
|
||||
(error) {
|
||||
_isDeliverable = false;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
(response) {
|
||||
print("kdhfgkjfkghkfghkj ${response.isDeliverable!}");
|
||||
if (response.isDeliverable!) {
|
||||
_isDeliverable = true;
|
||||
}
|
||||
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_isDeliverable = false;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
final _homeRepo = getIt<ProductRepo>();
|
||||
|
||||
AllCartItems allitem = AllCartItems();
|
||||
|
||||
bool isLoaddcartItem = true;
|
||||
Future<void> getItemCards(BuildContext context) async {
|
||||
var data = {};
|
||||
try {
|
||||
var result = await _homeRepo.getItemCards(data);
|
||||
|
||||
return result.fold(
|
||||
(error) {
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
(response) {
|
||||
allitem = response!;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:grocery_app/src/core/network_services/service_locator.dart';
|
||||
import 'package:grocery_app/src/core/routes/routes.dart';
|
||||
import 'package:grocery_app/src/data/allProduct_model.dart';
|
||||
import 'package:grocery_app/src/data/all_cart_items.dart';
|
||||
import 'package:grocery_app/src/data/banners.dart';
|
||||
import 'package:grocery_app/src/data/best_dealProduct.dart';
|
||||
import 'package:grocery_app/src/data/product_category.dart';
|
||||
@@ -273,7 +274,7 @@ class ProductProvider extends ChangeNotifier {
|
||||
Map<String, bool> isLoading = {};
|
||||
|
||||
Future<void> addToCart(BuildContext context, String productId) async {
|
||||
if (cartItems.contains(productId)) return; // Prevent duplicate additions
|
||||
//if (cartItems.contains(productId)) return; // Prevent duplicate additions
|
||||
|
||||
isLoading[productId] = true;
|
||||
notifyListeners(); // Notify UI to show loading indicator
|
||||
@@ -402,4 +403,8 @@ class ProductProvider extends ChangeNotifier {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////// all carts////////////////////////
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ import 'package:fpdart/fpdart.dart';
|
||||
import 'package:grocery_app/src/core/utils/custom_dio_exception.dart';
|
||||
import 'package:grocery_app/src/core/utils/response_type_def.dart';
|
||||
import 'package:grocery_app/src/data/allProduct_model.dart';
|
||||
import 'package:grocery_app/src/data/all_cart_items.dart';
|
||||
import 'package:grocery_app/src/data/banners.dart';
|
||||
import 'package:grocery_app/src/data/best_dealProduct.dart';
|
||||
import 'package:grocery_app/src/data/check_pin_response.dart';
|
||||
import 'package:grocery_app/src/data/product_category.dart';
|
||||
import 'package:grocery_app/src/data/wish_list_model.dart';
|
||||
import 'package:grocery_app/src/logic/services/home_locator.dart';
|
||||
@@ -85,6 +87,33 @@ class ProductRepo {
|
||||
}
|
||||
}
|
||||
|
||||
FutureResult<AllCartItems> getItemCards(data) async {
|
||||
try {
|
||||
var response = await _productService.getItemCards(data);
|
||||
|
||||
AllCartItems allCartItems = allCartItemsFromJson(response.toString());
|
||||
|
||||
return right(allCartItems);
|
||||
} on DioException catch (e) {
|
||||
print("sdkjfkjdkfjgjfdjg");
|
||||
var error = CustomDioExceptions.handleError(e);
|
||||
return left(error);
|
||||
}
|
||||
}
|
||||
|
||||
FutureResult<CheckPinResponse> checkPin(data,pin) async {
|
||||
try {
|
||||
var response = await _productService.checkPin(data,pin);
|
||||
CheckPinResponse allCartItems = checkPinResponseFromJson(response.toString());
|
||||
|
||||
return right(allCartItems);
|
||||
} on DioException catch (e) {
|
||||
|
||||
var error = CustomDioExceptions.handleError(e);
|
||||
return left(error);
|
||||
}
|
||||
}
|
||||
|
||||
FutureResult<String> addToWish(data) async {
|
||||
try {
|
||||
var response = await _productService.addToWish(data);
|
||||
@@ -134,6 +163,8 @@ class ProductRepo {
|
||||
|
||||
BannerModel bannerresponse = bannerFromJson(response.toString());
|
||||
|
||||
print("skjdgkjdsf ${bannerresponse}");
|
||||
|
||||
final String model = response.toString();
|
||||
|
||||
return right(bannerresponse);
|
||||
|
||||
@@ -50,6 +50,22 @@ class ProductService extends ApiService {
|
||||
return response;
|
||||
}
|
||||
|
||||
Future getItemCards(data) async {
|
||||
var response = await api.get(APIURL.getItemCards, data: jsonEncode(data));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Future checkPin(data,pin) async {
|
||||
var response = await api.get(APIURL.checkPin+pin, data: jsonEncode(data));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Future addToWish(data) async {
|
||||
|
||||
Reference in New Issue
Block a user