fl_downloader 1.1.0 fl_downloader: ^1.1.0 copied to clipboard
A plugin to download files using the native capabilities. URLSession on iOS and DownloadManager on Android
import 'dart:async';
import 'package:fl_downloader/fl_downloader.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final TextEditingController urlController = TextEditingController(
text:
'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
);
int progress = 0;
late StreamSubscription progressStream;
@override
void initState() {
FlDownloader.initialize();
progressStream = FlDownloader.progressStream.listen((event) {
if (event.status == DownloadStatus.successful) {
setState(() {
progress = event.progress;
});
FlDownloader.openFile(
filePath: event.filePath,
);
} else if (event.status == DownloadStatus.running) {
setState(() {
progress = event.progress;
});
}
});
super.initState();
}
@override
void dispose() {
progressStream.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('FlDownloader example app'),
),
body: Column(
children: [
if (progress > 0 && progress < 100)
LinearProgressIndicator(
value: progress / 100,
color: Colors.orange,
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: urlController,
),
),
),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.file_download),
onPressed: () async {
final permission = await FlDownloader.requestPermission();
if (permission == StoragePermissionStatus.granted) {
await FlDownloader.download(
urlController.text,
fileName: 'teste.pdf',
);
} else {
debugPrint('Permission denied =(');
}
},
),
),
);
}
}