2024-08-23 18:33:40 +08:00
|
|
|
import 'dart:io';
|
2024-08-04 08:21:14 +08:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-05-11 17:02:34 +08:00
|
|
|
class Picker {
|
2024-08-04 08:21:14 +08:00
|
|
|
Future<PlatformFile?> pickerFile() async {
|
2024-07-07 10:02:10 +08:00
|
|
|
final filePickerResult = await FilePicker.platform.pickFiles(
|
|
|
|
|
withData: true,
|
|
|
|
|
allowMultiple: false,
|
2024-08-04 08:21:14 +08:00
|
|
|
initialDirectory: await appPath.getDownloadDirPath(),
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
return filePickerResult?.files.first;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-23 18:33:40 +08:00
|
|
|
Future<String?> saveFile(String fileName, Uint8List bytes) async {
|
2024-08-04 08:21:14 +08:00
|
|
|
final path = await FilePicker.platform.saveFile(
|
|
|
|
|
fileName: fileName,
|
|
|
|
|
initialDirectory: await appPath.getDownloadDirPath(),
|
2024-08-23 18:33:40 +08:00
|
|
|
bytes: Platform.isAndroid ? bytes : null,
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
2024-08-23 18:33:40 +08:00
|
|
|
if (!Platform.isAndroid && path != null) {
|
|
|
|
|
final file = await File(path).create(recursive: true);
|
|
|
|
|
await file.writeAsBytes(bytes);
|
|
|
|
|
}
|
2024-08-04 08:21:14 +08:00
|
|
|
return path;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-05-11 17:02:34 +08:00
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<String?> pickerConfigQRCode() async {
|
2024-05-11 17:02:34 +08:00
|
|
|
final xFile = await ImagePicker().pickImage(source: ImageSource.gallery);
|
|
|
|
|
final bytes = await xFile?.readAsBytes();
|
2024-06-03 18:02:05 +08:00
|
|
|
if (bytes == null) return null;
|
2024-05-11 17:02:34 +08:00
|
|
|
final result = await other.parseQRCode(bytes);
|
|
|
|
|
if (result == null || !result.isUrl) {
|
2024-06-03 18:02:05 +08:00
|
|
|
throw appLocalizations.pleaseUploadValidQrcode;
|
2024-05-11 17:02:34 +08:00
|
|
|
}
|
2024-06-03 18:02:05 +08:00
|
|
|
return result;
|
2024-05-11 17:02:34 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-05-11 17:02:34 +08:00
|
|
|
|
|
|
|
|
final picker = Picker();
|