openAttachmentDialog method

void openAttachmentDialog({
  1. List<String>? type,
  2. required String fileType,
})

Opens a dialog to pick a file with specific extensions.

type is a list of allowed file extensions. If not provided, defaults to common document extensions.

Implementation

void openAttachmentDialog(
    {List<String>? type, required String fileType}) async {
  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions:
        type ?? ['pdf', 'docx', 'xlsx', 'pptx', 'doc', 'xls', 'ppt', 'txt'],
  );

  if (result != null) {
    File file = File(result.files.single.path!);
    int sizeBytes = file.lengthSync();
    double sizeMb = sizeBytes / (1024 * 1024);
    callback.success(
      fileData: ReturnModel(
        filePath: file.path,
        fileName: file.path.split('/').last,
        fileExtension: getFileExtension(file.path),
        size: sizeMb,
      ),
      type: fileType,
    );
  } else {
    callback.error('No file selected.');
  }
}