102 lines
2.8 KiB
Dart
102 lines
2.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:Tripz/constant/api_key.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:image_cropper/image_cropper.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:path/path.dart';
|
|
|
|
import '../../constant/box_name.dart';
|
|
import '../../constant/colors.dart';
|
|
import '../../main.dart';
|
|
|
|
class ImageController extends GetxController {
|
|
File? myImage;
|
|
bool isloading = false;
|
|
CroppedFile? croppedFile;
|
|
final picker = ImagePicker();
|
|
var image;
|
|
choosImage(String link, String imageType) async {
|
|
final pickedImage = await picker.pickImage(source: ImageSource.gallery);
|
|
image = File(pickedImage!.path);
|
|
croppedFile = await ImageCropper().cropImage(
|
|
sourcePath: image!.path,
|
|
uiSettings: [
|
|
AndroidUiSettings(
|
|
toolbarTitle: 'Cropper'.tr,
|
|
toolbarColor: AppColor.blueColor,
|
|
toolbarWidgetColor: AppColor.yellowColor,
|
|
initAspectRatio: CropAspectRatioPreset.original,
|
|
lockAspectRatio: false),
|
|
IOSUiSettings(
|
|
title: 'Cropper'.tr,
|
|
),
|
|
],
|
|
);
|
|
myImage = File(pickedImage.path);
|
|
isloading = true;
|
|
update();
|
|
// Save the cropped image
|
|
File savedCroppedImage = File(croppedFile!.path);
|
|
try {
|
|
await uploadImage(
|
|
savedCroppedImage,
|
|
{
|
|
'driverID':
|
|
box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
|
|
'imageType': imageType
|
|
},
|
|
link,
|
|
);
|
|
} catch (e) {
|
|
Get.snackbar('Image Upload Failed'.tr, e.toString(),
|
|
backgroundColor: AppColor.redColor);
|
|
} finally {
|
|
isloading = false;
|
|
update();
|
|
}
|
|
}
|
|
|
|
uploadImage(File file, Map data, String link) async {
|
|
var request = http.MultipartRequest(
|
|
'POST',
|
|
Uri.parse(link), //'https://ride.mobile-app.store/uploadImage1.php'
|
|
);
|
|
|
|
var length = await file.length();
|
|
var stream = http.ByteStream(file.openRead());
|
|
var multipartFile = http.MultipartFile(
|
|
'image',
|
|
stream,
|
|
length,
|
|
filename: basename(file.path),
|
|
);
|
|
request.headers.addAll({
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
|
|
});
|
|
// Set the file name to the driverID
|
|
request.files.add(
|
|
http.MultipartFile(
|
|
'image',
|
|
stream,
|
|
length,
|
|
filename: '${box.read(BoxName.driverID)}.jpg',
|
|
),
|
|
);
|
|
data.forEach((key, value) {
|
|
request.fields[key] = value;
|
|
});
|
|
var myrequest = await request.send();
|
|
var res = await http.Response.fromStream(myrequest);
|
|
if (res.statusCode == 200) {
|
|
return jsonDecode(res.body);
|
|
} else {
|
|
throw Exception(
|
|
'Failed to upload image: ${res.statusCode} - ${res.body}');
|
|
}
|
|
}
|
|
}
|