Skip to content

Instantly share code, notes, and snippets.

@ghulamostafa
Created April 20, 2021 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghulamostafa/e0f97992e70ae25783027900ab4b5204 to your computer and use it in GitHub Desktop.
Save ghulamostafa/e0f97992e70ae25783027900ab4b5204 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
class DownloadFileScreen extends StatefulWidget {
@override
_DownloadFileScreenState createState() => _DownloadFileScreenState();
}
class _DownloadFileScreenState extends State<DownloadFileScreen> {
String filePath;
int state = 0; //0: Not downloaded, 1: downloading, 2: downloaded
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ListView(
children: [
MyMediaDownloadableWidget('https://placeimg.com/640/480/any', 'any8.png'),
MyMediaDownloadableWidget('https://placeimg.com/640/480/any', 'any9.png'),
MyMediaDownloadableWidget('https://placeimg.com/640/480/any', 'any10.png'),
],
)
);
}
filePathSet(path){
setState(() {
filePath = path;
});
}
downloadUsingDio(String mediaURL, String fileName, Function filePathSet) async {
Dio dio = Dio();
await getApplicationDocumentsDirectory().then((appDir) async {
if(!File(appDir.path + '/' + fileName).existsSync())
{
print('Downloading');
var filePath = await File(appDir.path + '/' + fileName).create(recursive: true);
await dio.download(mediaURL, filePath.path);
filePathSet(filePath.path);
}
else{
print('Already downloaded');
filePathSet(File(appDir.path + '/' + fileName).path);
}
});
}
}
class MyMediaDownloadableWidget extends StatefulWidget {
@override
_MyMediaDownloadableWidgetState createState() => _MyMediaDownloadableWidgetState();
final String mediaURL;
final String fileName;
MyMediaDownloadableWidget(this.mediaURL, this.fileName);
}
class _MyMediaDownloadableWidgetState extends State<MyMediaDownloadableWidget> {
int currentStatus = 0;
String filePath;
@override
void initState() {
// TODO: implement initState
super.initState();
downloadUsingDio(widget.mediaURL, widget.fileName, filePathSet);
}
@override
Widget build(BuildContext context) {
if(currentStatus == 0){
return ElevatedButton(
onPressed: (){
print('download file');
downloadUsingDio(widget.mediaURL, widget.fileName, filePathSet);
},
child: Text('Download')
);
} else if(currentStatus == 1){
return Column(
children: [
CircularProgressIndicator()
],
);
} else {
return Image.file(File(filePath));
}
}
filePathSet(path){
setState(() {
filePath = path;
});
}
downloadUsingDio(String mediaURL, String fileName, Function filePathSet) async {
Dio dio = Dio();
setState(() {
currentStatus = 1;
});
await getApplicationDocumentsDirectory().then((appDir) async {
if(!File(appDir.path + '/' + fileName).existsSync())
{
print('Downloading');
var filePath = await File(appDir.path + '/' + fileName).create(recursive: true);
await dio.download(mediaURL, filePath.path);
filePathSet(filePath.path);
setState(() {
currentStatus = 2;
});
}
else{
print('Already downloaded');
filePathSet(File(appDir.path + '/' + fileName).path);
setState(() {
currentStatus = 2;
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment