1-) FLUTTER - Dio ile pdf indirme ve OpenFile ile görüntüleme download
**** BİRİNCİ YOL FlutterDownloader ile ***
terminale yaz -> flutter pub add flutter_downloader
İNİTİALİZİNG
void callback(String id ,DownloadTaskStatus status,int progress){}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(debug: true, ignoreSsl: true); // , ignoreSsl: true
FlutterDownloader.registerCallback(callback);
runApp(const MyApp());}
PATH İÇİN
Future<String?> getDownloadPath() async {
Directory? directory;
try {
if (Platform.isIOS) {
directory = await getApplicationDocumentsDirectory();
} else {
directory = Directory('/storage/emulated/0/Download');
// Put file in global download folder, if for an unknown reason it didn't exist, we fallback
// ignore: avoid_slow_async_io
if (!await directory.exists()) directory = await getExternalStorageDirectory();
}
} catch (err, stack) {
print("Cannot get download folder path");
}
return directory?.path;
}
DOSYA İNDİRME
// String? dosyaYolu = await getDownloadPath();
String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
await FlutterDownloader.enqueue(
url:urlim ,
showNotification: true,
openFileFromNotification: true,
saveInPublicStorage: true,
savedDir: (await getExternalStorageDirectory())!.path);
**** İKİNCİ YOL CHROME YÖNLENDİRİR ***
String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
final String _url_files = "$urlim";
void _launchURL_files() async =>
await canLaunch(_url_files) ? await launch(_url_files) : throw 'Could not launch $_url_files';
_launchURL_files();
1. MobileDownloadService.dart
import 'package:dio/dio.dart'; |
2. KULLANIMI
String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
|
kaynak : https://github.com/MCarlomagno/flutter_download_button
4. InAppWebView ile
onDownloadStart: (controller,url,) async {
await FlutterDownloader.enqueue(
url: url.toString(),
showNotification: true,
openFileFromNotification: true,
savedDir: "/storage/emulated/0/Download" );},
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<provider
android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
android:authorities="${applicationId}.flutter_downloader.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
<provider
android:name="com.pichillilorenzo.flutter_inappwebview.InAppWebViewFileProvider"
android:authorities="${applicationId}.flutter_inappwebview.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
6. Android->app->src->main->res->xml->provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
SON NOT ANDROİD İÇİN BUNU KULLAN savedDir: (await getExternalStorageDirectory())!.path);
İOS İÇİN BUNU KULLAN directory = await getApplicationDocumentsDirectory();