downloadAndViewFile static method

Future<void> downloadAndViewFile(
  1. BuildContext context, {
  2. required String downloadUrl,
  3. required String fileName,
  4. required String downloadFolderName,
  5. bool displayInNativeApp = true,
  6. bool useDefaultProgressDialog = true,
  7. bool useBuiltInPreviewer = true,
  8. required dynamic onDownloadProgress(
    1. Stream<String>
    ),
  9. required DownloadFailedCallback onDownloadFailed,
  10. required DownloadCompleteCallback onDownloadComplete,
  11. void customPreviewBuilder(
    1. String,
    2. String
    )?,
  12. Widget? progressWidget,
})

Downloads the document using the provided downloadUrl.

If the file already exists at the specified path, it opens the file preview in either the native viewer or a custom viewer based on displayInNativeApp. If the file does not exist, it downloads the file to the specified path and then opens the file.

Parameters:

  • downloadUrl - The HTTPS URL for the file to be downloaded.
  • fileName - The name of the file to be downloaded.
  • downloadFolderName - The name of the folder where the file will be downloaded, usually the app name.
  • displayInNativeApp - Determines whether to open the file using the native app or a custom viewer. Defaults to true.
  • progressWidget - A widget to display a progress bar during the download process.
  • customPreviewBuilder - A custom builder widget to provide your own preview and routing. Make sure to set displayInNativeApp to false when using this.
  • useDefaultProgressDialog - Flag to enable or disable download progress Dialog

Usage:

await downloadAndViewFile(
  context,
  downloadUrl: 'https://example.com/file.pdf',
  fileName: 'file.pdf',
  downloadFolderName: 'MyApp',
  displayInNativeApp: true,
  progressWidget: CircularProgressIndicator(),
);

Implementation

///
/// Usage:
///
/// ```dart
/// await downloadAndViewFile(
///   context,
///   downloadUrl: 'https://example.com/file.pdf',
///   fileName: 'file.pdf',
///   downloadFolderName: 'MyApp',
///   displayInNativeApp: true,
///   progressWidget: CircularProgressIndicator(),
/// );
/// ```

static Future<void> downloadAndViewFile(
  BuildContext context, {
  required String downloadUrl,
  required String fileName,
  required String downloadFolderName,
  bool displayInNativeApp = true,
  bool useDefaultProgressDialog = true,
  bool useBuiltInPreviewer = true,
  required Function(Stream<String>) onDownloadProgress,
  required DownloadFailedCallback onDownloadFailed,
  required DownloadCompleteCallback onDownloadComplete,
  void Function(String, String)? customPreviewBuilder,
  Widget? progressWidget,
}) async {
  _downloadStreamController = StreamController<String>.broadcast();
  final CancelToken cancelToken = CancelToken();

  // Whether to show download progress
  if (useDefaultProgressDialog) {
    _showProgressDialog(context, cancelToken, progressWidget);
  }

  final (hasFilePath, savePath, fileExtension) =
      await DeviceDirectoryHelper.checkFilePath(
          fileName: fileName, downloadFolderName: downloadFolderName);

  if (savePath == null) {
    // Log error or show a message to the user
    return;
  }

  if (hasFilePath) {
    onDownloadComplete();

    _openDownloadedFile(
      displayInNativeApp,
      context,
      savePath,
      fileExtension,
      fileName,
      previewBuilder: customPreviewBuilder,
    );
  } else {
    try {
      _downloadViaAPI(
        downloadUrl: downloadUrl,
        savePath: savePath,
        cancelToken: cancelToken,
        onSuccess: (path) {
          onDownloadComplete();

          final String fileExtension = p.extension(savePath);

          _openDownloadedFile(
            displayInNativeApp,
            context,
            savePath,
            fileExtension,
            fileName,
          );
        },
        onFailed: (message) {
          onDownloadFailed(message);
          _closeProgressDialog(useDefaultProgressDialog, context);
        },
        onProgress: (progress) {
          _downloadStreamController?.add(progress);
        },
      );
    } catch (e) {
      _closeProgressDialog(useDefaultProgressDialog, context);
      onDownloadFailed(e.toString());
    }
  }
}