download_assets
取得資源並加入應用程式的儲存庫以供使用。
此套件使用 Flutter 開發,可將所有未包含在您的應用程式中的資源下載並解壓縮至本機儲存空間。
{"sdk":"flutter"}^5.9.0^1.9.1^4.0.7^2.7.1+1^2.1.5^2.5.4^1.0.4^6.0.0{"sdk":"flutter"}以下為英文專案原文快照,最新內容請造訪 GitHub。
The download_assets package is a powerful library that facilitates the process of downloading assets into the application. With this library, you can easily download files, images, videos, and other resources in your app, providing a smooth and efficient user experience.
https://media.giphy.com/media/SYDVx5BJFGrnSaPBtQ/giphy.gif
Init method for setting up the assetsDir, which is required to be called during app initialization.
await downloadAssetsController.init();
Starts the asset download process.
await downloadAssetsController.startDownload(
onCancel: () {
//TODO: implement cancel here
},
assetsUrls: [
AssetUrl(url: 'https://github.com/edjostenes/download_assets/raw/main/download/image_1.png'),
AssetUrl(url: 'https://github.com/edjostenes/download_assets/raw/main/download/assets.zip'),
AssetUrl(url: 'https://github.com/edjostenes/download_assets/raw/main/download/image_2.png'),
AssetUrl(url: 'https://github.com/edjostenes/download_assets/raw/main/download/image_3.png'),
],
onProgress: (progressValue) {
//TODO: Implement progress here
},
onDone: () {
//TODO: Implement progress here
}
);
You can now specify the file name and extension with the AssetUrl class in case you don't have control over the URL format.
class AssetUrl {
final String url;
final String? fileName;
const AssetUrl({
required this.url,
this.fileName,
});
}
Removes all downloaded assets from local storage.
await downloadAssetsController.clearAssets();
await downloadAssetsController.getAssetFromWeb('<file_name>.<file_extension>');
Note: This method is only available on web version
Path to the files.
File('${downloadAssetsController.assetsDir}/<file_name>.<file_extension>');
Returns true if the assetsDir path exists.
return await downloadAssetsController.assetsDirAlreadyExists();
Returns true if the file exists.
return await downloadAssetsController.assetsFileExists(<file_name>);
Uncompress delegates are responsible for implementing the logic to uncompress different types of files, such as ZIP, RAR, etc. By providing a list of delegates, it becomes possible to support multiple file formats during the download.
To implement your own uncompression delegate, you should implement the interface below.
/// Abstract class representing a delegate for asset decompression.
abstract class UncompressDelegate {
const UncompressDelegate();
/// Gets the file extension associated with the delegate.
String get extension;
/// uncompress the asset located at [compressedFilePath] to the specified [assetsDir].
/// [compressedFilePath] -> The path to the compressed asset file.
/// [assetsDir] -> The directory where the uncompressed asset should be stored.
Future uncompress(String compressedFilePath, String assetsDir);
}
Note: This kind of delegate is essential for enabling the download process to handle various file formats by implementing the necessary decompression logic. This flexibility allows users to download and work with a wide range of compressed file types effortlessly. UnzipDelegate() is already included, which handles the decompression of ZIP files.
You can find an example here: https://github.com/edjostenes/download_assets/tree/master/example
Contributions are welcome! If you encounter any issues or have suggestions for improvement, please create an issue on the GitHub repository.