74 lines
1.9 KiB
Dart
74 lines
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:SEFER/constant/api_key.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:http/http.dart' as http;
|
|
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;
|
|
void choosImage(String link) async {
|
|
XFile? xFile = await ImagePicker().pickImage(source: ImageSource.gallery);
|
|
myImage = File(xFile!.path);
|
|
isloading = true;
|
|
update();
|
|
var response = await uploadImage(
|
|
myImage!,
|
|
{
|
|
'driverID': box.read(BoxName.driverID).toString(),
|
|
},
|
|
link,
|
|
);
|
|
isloading = false;
|
|
update();
|
|
if (response['status'] == 'success') {
|
|
Get.snackbar('Image Uploaded'.tr, 'Sucsses',
|
|
backgroundColor: AppColor.greenColor);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|