fast_download_manager 1.2.0
fast_download_manager: ^1.2.0 copied to clipboard
A lightweight Flutter package for downloading files with real-time progress tracking.
import 'package:fast_download_manager/fast_download_manager.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(
const MyApp(),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Fast Download Manager',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
),
useMaterial3: true,
),
home: const DownloadScreen(),
);
}
}
class DownloadScreen extends StatefulWidget {
const DownloadScreen({
super.key,
});
@override
State<DownloadScreen> createState() => _DownloadScreenState();
}
class _DownloadScreenState extends State<DownloadScreen> {
double progress = 0;
double speed = 0;
String speedText = '0 B/s';
String status = 'Ready';
String? downloadId;
bool isDownloading = false;
bool isCompleted = false;
Future<void> downloadFile() async {
final dir = await getApplicationDocumentsDirectory();
setState(() {
progress = 0;
speed = 0;
speedText = '0 B/s';
status = 'Starting...';
isDownloading = true;
isCompleted = false;
});
try {
final file = await FastDownloader.download(
url:
'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
savePath: '${dir.path}/sample.pdf',
retryCount: 3,
retryDelay: const Duration(seconds: 2),
onStart: (id) {
if (!mounted) return;
setState(() {
downloadId = id;
status = 'Downloading...';
});
},
onProgress: (value) {
if (!mounted) return;
setState(() {
progress = value.progress;
speed = value.speed;
speedText = value.speedFormatted;
status = '${value.receivedFormatted} / '
'${value.totalFormatted}';
});
},
onStatus: (value) {
if (!mounted) return;
setState(() {
status = _statusText(value);
});
},
onComplete: (file) {
if (!mounted) return;
setState(() {
status = 'Download Completed';
isCompleted = true;
});
},
onError: (error) {
if (!mounted) return;
setState(() {
status = 'Download Failed';
});
},
);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Saved: ${file.path}',
),
),
);
} on DownloadException catch (e) {
if (!mounted) return;
setState(() {
status = 'Download Failed';
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.message),
),
);
} catch (e) {
if (!mounted) return;
setState(() {
status = 'Download Failed';
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
),
);
} finally {
// Don't use `return` inside finally.
if (mounted) {
setState(() {
isDownloading = false;
});
}
}
}
void cancelDownload() {
final id = downloadId;
if (id == null) {
return;
}
FastDownloader.cancel(id);
setState(() {
status = 'Cancelling...';
});
}
String _statusText(
DownloadStatus status,
) {
switch (status) {
case DownloadStatus.downloading:
return 'Downloading...';
case DownloadStatus.retrying:
return 'Retrying...';
case DownloadStatus.completed:
return 'Download Completed';
case DownloadStatus.cancelled:
return 'Download Cancelled';
case DownloadStatus.failed:
return 'Download Failed';
}
}
@override
Widget build(
BuildContext context,
) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Fast Download Manager',
),
centerTitle: true,
),
body: Center(
child: Card(
margin: const EdgeInsets.all(20),
child: Padding(
padding: const EdgeInsets.all(20),
child: SizedBox(
width: 340,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons.download_rounded,
size: 70,
color: Colors.blue,
),
const SizedBox(
height: 20,
),
LinearProgressIndicator(
value: progress,
),
const SizedBox(
height: 12,
),
Text(
'${(progress * 100).toStringAsFixed(0)}%',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
const SizedBox(
height: 8,
),
Text(
status,
textAlign: TextAlign.center,
),
const SizedBox(
height: 8,
),
if (isDownloading)
Text(
speedText,
style: const TextStyle(
fontWeight: FontWeight.w600,
),
),
const SizedBox(
height: 25,
),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: isDownloading ? null : downloadFile,
icon: Icon(
isDownloading ? Icons.downloading : Icons.download,
),
label: Text(
isDownloading ? 'Downloading...' : 'Download File',
),
),
),
if (isDownloading)
Padding(
padding: const EdgeInsets.only(
top: 10,
),
child: SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: cancelDownload,
icon: const Icon(
Icons.cancel,
),
label: const Text(
'Cancel Download',
),
),
),
),
if (isCompleted)
const Padding(
padding: EdgeInsets.only(
top: 20,
),
child: Column(
children: [
Icon(
Icons.check_circle,
color: Colors.green,
size: 45,
),
SizedBox(
height: 8,
),
Text(
'File downloaded successfully',
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
),
),
),
),
);
}
}