downloadAndViewFile static method
Future<void>
downloadAndViewFile(
- BuildContext context, {
- required String downloadUrl,
- required String fileName,
- required String downloadFolderName,
- bool displayInNativeApp = true,
- bool useDefaultProgressDialog = true,
- bool useBuiltInPreviewer = true,
- required dynamic onDownloadProgress(),
- required DownloadFailedCallback onDownloadFailed,
- required DownloadCompleteCallback onDownloadComplete,
- void customPreviewBuilder()?,
- 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 setdisplayInNativeApp
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());
}
}
}