178 lines
5.2 KiB
Dart
178 lines
5.2 KiB
Dart
import 'dart:developer';
|
|
import 'dart:io';
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:get/get.dart' as getx;
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class Utils {
|
|
static Future<MultipartFile> networkImageToMultipartFile(
|
|
String imageUrl) async {
|
|
Dio dio = Dio();
|
|
|
|
Response<Uint8List> response = await dio.get<Uint8List>(imageUrl,
|
|
options: Options(responseType: ResponseType.bytes));
|
|
|
|
MultipartFile multipartFile = MultipartFile.fromBytes(
|
|
response.data!,
|
|
filename: imageUrl.substring(imageUrl.lastIndexOf("/") + 1),
|
|
);
|
|
|
|
return multipartFile;
|
|
}
|
|
|
|
static Future<MultipartFile> assetImageToMultipartFile(
|
|
String assetImagePath, String fileName) async {
|
|
ByteData assetByteData = await rootBundle.load(assetImagePath);
|
|
List<int> assetBytes = assetByteData.buffer.asUint8List();
|
|
|
|
MultipartFile file = MultipartFile.fromBytes(
|
|
assetBytes,
|
|
filename: fileName,
|
|
);
|
|
|
|
return file;
|
|
}
|
|
|
|
static showToast(String? msg) {
|
|
if (msg != null && msg != "null" && msg.isNotEmpty) {
|
|
Fluttertoast.showToast(
|
|
msg: msg,
|
|
toastLength: Toast.LENGTH_LONG,
|
|
);
|
|
}
|
|
}
|
|
|
|
static loader() {
|
|
getx.Get.dialog(
|
|
Dialog(
|
|
elevation: 0,
|
|
backgroundColor: Colors.transparent,
|
|
child: WillPopScope(
|
|
child: const Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(
|
|
color: Color(0xFF9A0000),
|
|
),
|
|
],
|
|
),
|
|
onWillPop: () async => false),
|
|
),
|
|
barrierDismissible: false,
|
|
);
|
|
}
|
|
|
|
static Future<void> getStoragePermission() async {
|
|
DeviceInfoPlugin plugin = DeviceInfoPlugin();
|
|
AndroidDeviceInfo android = await plugin.androidInfo;
|
|
if (android.version.sdkInt < 33) {
|
|
if (await Permission.storage.request().isGranted) {
|
|
// setState(() {
|
|
// permissionGranted = true;
|
|
// });
|
|
} else if (await Permission.storage.request().isPermanentlyDenied) {
|
|
await openAppSettings();
|
|
await showToast("Permission denied.");
|
|
}
|
|
// else if (await Permission.audio.request().isDenied) {
|
|
// // setState(() {
|
|
// // permissionGranted = false;
|
|
// // });
|
|
// }
|
|
} else {
|
|
if (await Permission.photos.request().isGranted) {
|
|
// await utils.showToast("Permission granted.");
|
|
// setState(() {
|
|
// permissionGranted = true;
|
|
// });
|
|
} else if (await Permission.photos.request().isPermanentlyDenied) {
|
|
await openAppSettings();
|
|
await showToast("Permission denied.");
|
|
} else if (await Permission.photos.request().isDenied) {
|
|
await openAppSettings();
|
|
await showToast("Permission denied.");
|
|
// setState(() {
|
|
// permissionGranted = false;
|
|
// });
|
|
}
|
|
}
|
|
}
|
|
|
|
static Future openFile({
|
|
required String url,
|
|
String? fileName,
|
|
}) async {
|
|
var status;
|
|
// Permission.manageExternalStorage.request();
|
|
final deviceInfo = await DeviceInfoPlugin().androidInfo;
|
|
if (deviceInfo.version.sdkInt > 32) {
|
|
status = await Permission.photos.request().isGranted;
|
|
|
|
await Permission.manageExternalStorage.request();
|
|
} else {
|
|
status = await Permission.storage.request().isGranted;
|
|
await Permission.manageExternalStorage.request();
|
|
}
|
|
if (status) {
|
|
await downloadFile(url, fileName!);
|
|
// if (file == null) {
|
|
// log("Error 3333333333333");
|
|
// } else {
|
|
// await OpenFile.open(file.path).then((value) {
|
|
// print("RUNNING THIS");
|
|
// if (value.type == ResultType.noAppToOpen) {
|
|
// ScaffoldMessenger.of(context).showSnackBar(Helper.displaySnackBar(
|
|
// "type",
|
|
// "error",
|
|
// "You do not have any application to open this file"));
|
|
|
|
// //Navigator.pop(context);
|
|
// } else {
|
|
// log(value.message);
|
|
// }
|
|
// });
|
|
|
|
// // if (message.toString() != "done") {
|
|
// // displaySnackBar("login", "error", message.message.toString());
|
|
// // }
|
|
// }
|
|
} else {
|
|
openAppSettings();
|
|
}
|
|
}
|
|
|
|
static Future<File?> downloadFile(String url, String name) async {
|
|
final appStorage = Directory("/storage/emulated/0/Download");
|
|
final file = File('${appStorage.path}/$name');
|
|
|
|
try {
|
|
showToast("Downloading...");
|
|
final response = await Dio().get(
|
|
url,
|
|
options: Options(
|
|
// headers: requestHeaders,
|
|
responseType: ResponseType.bytes,
|
|
followRedirects: false,
|
|
// receiveTimeout: 0,
|
|
),
|
|
);
|
|
final raf = file.openSync(mode: FileMode.write);
|
|
raf.writeFromSync(response.data);
|
|
await raf.close();
|
|
showToast("Download Completed !");
|
|
|
|
return file;
|
|
} catch (e) {
|
|
log(e.toString());
|
|
showToast("Download Error! Please try again");
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|