Im using flutter downloder for downloading the files from url. its working fine but i want to display a progress of downloads for user.
Can some please explain me how to get the progress data.
I got the progress Thank you. class _Happy_hours_puzzlesState extends State<Happy_hours_puzzles> {
final ReceivePort _port = ReceivePort(); ValueNotifier _progress = ValueNotifier(0);
@override void initState() {
print("Init State+++++++++++++++++++++++++++++++++++++");
_progress=ValueNotifier<double>(0);
IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
_port.listen((dynamic data) {
String id = data[0];
DownloadTaskStatus status = data[1];
int progress = data[2];
_progress.value = progress.toDouble();
print("inside port progress ${_progress.value}");
print("status status status ${status}");
});
FlutterDownloader.registerCallback(downloadCallback);
super.initState();
}
@override void dispose() {
IsolateNameServer.removePortNameMapping('downloader_send_port');
_progress.dispose();
super.dispose();
}
static void downloadCallback(String id, DownloadTaskStatus status, int progress) { final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port')!; send.send([id, status, progress]); }
StatefulBuilder( builder: (BuildContext context, StateSetter setState) { print("inside StatefulBuilder progress ${_progress.value}"); return Container( height: MediaQuery.of(context).size.width * 0.3, width: MediaQuery.of(context).size.width * 0.7, child: Center( child: ClipRRect( borderRadius: BorderRadius.all(Radius.circular(10)), child: ValueListenableBuilder( builder: (BuildContext context, double value, child) { return Column( children: [ Text(puzzleName, style: TextStyle( fontFamily: "Open Sans", color: Colors.purple.shade900, fontSize: 15, fontWeight: FontWeight.bold, ), textAlign: TextAlign.center), SizedBox(height: 10,),
LinearProgressIndicator(
minHeight: 5, value: _progress.value),
SizedBox(height: 10,),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
primary:
(_progress.value<100 && _progress.value>-1)
? Colors.green: (value==100)?Colors.deepPurple
: Colors.red,
),
child: Text(
(_progress.value<100 && _progress.value>-1) ? "Downloading..." : (_progress.value==100)?"Done" :"Failed Please try again",
style: TextStyle(
fontFamily: "Open Sans",
fontSize: 12,
fontWeight: FontWeight.bold,
color: Colors.white,
)),
onPressed: (_progress.value==100 || _progress.value==-1) ? () async {
Navigator.pop(context);
}:(){print("Downloading");}
,),
],
);
// return new Text(value.toString());
},
valueListenable: _progress))),
);
})