filePicker static method

dynamic filePicker({
  1. required FileData fileData,
  2. required dynamic onSelected(
    1. FileData fileData
    ),
  3. dynamic onCancel(
    1. String message,
    2. int messageCode
    )?,
  4. int? maxFileSizeInMb,
  5. List<String>? allowedExtensions,
})

function file picker for pick any file and save to temporary cache directory

Implementation

static filePicker(
    {required FileData fileData,
    required Function(FileData fileData) onSelected,
    Function(String message, int messageCode)? onCancel,
    int? maxFileSizeInMb,
    List<String>? allowedExtensions}) async {
  List<String>? extensions = allowedExtensions
      ?.map((e) => Files.getFileExtension(e, withDot: false))
      .toList();
  FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: (allowedExtensions ?? []).isNotEmpty
          ? FileType.custom
          : FileType.any,
      allowedExtensions: extensions);
  if (result != null && result.files.single.path != null) {
    if (maxFileSizeInMb != null &&
        Files.mb(result.files.single.size) > maxFileSizeInMb) {
      dev.log(
          "[${Files._mcFPCForSize}] ${Files._fileMoreThanMB(maxFileSizeInMb)}");
      if (onCancel != null) {
        onCancel(Files._fileMoreThanMB(maxFileSizeInMb), Files._mcFPCForSize);
      }
      return;
    }
    FileData fileData = FileData(
        hasFile: true,
        fileName: result.files.single.name,
        filePath: result.files.single.path!,
        fileMimeType: Files.getMimeType(result.files.single.path!),
        path: result.files.single.path!);
    onSelected(fileData);
  } else {
    dev.log("[${Files._mcFPCForCancel}] ${Files._filePickCancel}");
    if (onCancel != null) {
      onCancel(Files._filePickCancel, Files._mcFPCForCancel);
    }
    return;
  }
}