imageDownloadProgress method
Stream<String>
imageDownloadProgress(
- String url, {
- String imageName = 'myimage',
- ImageFormat fileExtension = ImageFormat.jpeg,
- DownloadLocation location = DownloadLocation.temporaryDirectory,
override
before using homeScreen, lockScreen , bothScreen, systemScreen . we need to download image .
imageName
-> after downloading image name to be saved so when using home screen, etc we can pass this name and
location
location where the image is downloaded
Implementation
@override
Stream<String> imageDownloadProgress(String url,
{String imageName = 'myimage',
ImageFormat fileExtension = ImageFormat.jpeg,
DownloadLocation location = DownloadLocation.temporaryDirectory}) async* {
StreamController<String> streamController = new StreamController();
try {
Directory? dir;
switch (location) {
case DownloadLocation.applicationDirectory:
dir = await getApplicationSupportDirectory();
break;
case DownloadLocation.externalDirectory:
dir = await getExternalStorageDirectory();
break;
case DownloadLocation.temporaryDirectory:
default:
dir = await getTemporaryDirectory();
break;
}
dir ??= await getTemporaryDirectory();
Dio dio = Dio();
String fileName = _imageFormatToExtension(fileExtension);
print("${dir.path}/$imageName.$fileName");
dio
.download(
url,
"${dir.path}/$imageName.$fileName",
onReceiveProgress: (int received, int total) {
streamController
.add("${((received / total) * 100).toStringAsFixed(0)}%");
},
)
.then((Response response) {})
.catchError((ex) {
streamController.add(ex.toString());
streamController.close();
})
.whenComplete(() {
streamController.close();
});
yield* streamController.stream;
} catch (ex) {
streamController.addError(ex.toString());
streamController.close();
}
}