Application/medcify/lib/pages/main/changeUpiQr/change_upi_qr_bloc.dart

89 lines
2.4 KiB
Dart
Raw Normal View History

2022-09-26 06:33:52 +00:00
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:medcify/network/api_provider.dart';
import '../../../components/alert.dart';
import '../../../navigation/navigation.dart';
abstract class ChangeUpiQrState{}
class ChangeUpiQrInitial extends ChangeUpiQrState{}
class ChangeUpiQrLoading extends ChangeUpiQrState{}
class ChangeUpiQrSuccess extends ChangeUpiQrState{}
class ChangeUpiQrFailure extends ChangeUpiQrState{
String error;
ChangeUpiQrFailure(this.error);
}
class ChangeUpiQrCubit extends Cubit<ChangeUpiQrState> {
ChangeUpiQrCubit() : super(ChangeUpiQrInitial());
String? image;
ImagePicker picker = ImagePicker();
Future<void> fetchUpiQr() async{
emit(ChangeUpiQrLoading());
final response = await ApiProvider.instance.fetchUpiQrImage();
if(response.status ?? false){
image = response.image ?? "";
emit(ChangeUpiQrSuccess());
}else{
emit(ChangeUpiQrFailure(response.message ?? "Something went wrong"));
}
}
Future<void> getImage(int pos) async{
final XFile? value = await picker.pickImage(source: (pos == 0) ? ImageSource.camera : ImageSource.gallery);
if(value != null){
final bytes = (await value.readAsBytes()).lengthInBytes;
final kb = bytes / 1024;
final mb = kb / 1024;
if(mb <= 1){
updateImage(value.path);
}else{
showToast("Select image below 1mb");
}
}
}
Future<void> updateImage(String val) async{
Navigation.instance.navigate("/loading");
final response = await ApiProvider.instance.updateUpiQrImage(val);
Navigation.instance.goBack();
if(response.status ?? false){
fetchUpiQr();
}else{
showAlert(response.message ?? "Something went wrong");
}
}
void showAlert(String err){
AlertX.instance.showAlert(
title: "Error",
msg: err,
positiveButtonText: "Done",
positiveButtonPressed: (){
Navigation.instance.goBack();
}
);
}
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
);
}
}