Application/medcify/lib/pages/main/deliveryPincodes/editPincode/edit_pincode_bloc.dart

74 lines
2.3 KiB
Dart
Raw Normal View History

2022-09-26 06:33:52 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:medcify/models/pincode_item.dart';
import 'package:medcify/network/api_provider.dart';
import '../../../../components/alert.dart';
import '../../../../navigation/navigation.dart';
abstract class EditPincodeState{}
class EditPincodeInitial extends EditPincodeState{}
class EditPincodeCubit extends Cubit<EditPincodeState> {
EditPincodeCubit() : super(EditPincodeInitial());
TextEditingController pincodeController = TextEditingController();
TextEditingController deliveryFeeController = TextEditingController();
TextEditingController freeAboveController = TextEditingController();
void setData(PincodeItem pincode){
pincodeController.text = "${pincode.pincode ?? 0}";
deliveryFeeController.text = "${pincode.deliveryFee ?? 0}";
freeAboveController.text = "${pincode.freeAbove ?? 0}";
}
Future<void> updatePincode(String id) async{
if(pincodeController.text.isEmpty){
showToast("Enter pincode");
}else if(deliveryFeeController.text.isEmpty){
showToast("Enter delivery fee");
}else if(freeAboveController.text.isEmpty){
showToast("Enter free above");
}else{
Navigation.instance.navigate("/loading");
final response = await ApiProvider.instance.updatePincode(id,pincodeController.text,deliveryFeeController.text,freeAboveController.text);
Navigation.instance.goBack();
if(response.status ?? false){
BuildContext? context = Navigation.instance.navigatorKey.currentContext;
if(context != null){
Navigator.pop(context,true);
}
showToast("Pincode updated successfully");
}else{
showAlert(response.message ?? "Something went wrong");
}
}
}
void showToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey.shade200,
textColor: Colors.black,
fontSize: 14.0
);
}
void showAlert(String err){
AlertX.instance.showAlert(
title: "Error",
msg: err,
positiveButtonText: "Done",
positiveButtonPressed: (){
Navigation.instance.goBack();
}
);
}
}