flutter 下載文件


一、Android

1.引入插件

  permission_handler: 5.0.1+1 #權限請求
  path_provider: 1.6.14 #路徑
  flutter_downloader: 1.4.4 #下載

2. androidmanifest.xml

    <!--網絡權限-->
    <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.REQUEST_INSTALL_PACKAGES" />

    <application
        android:networkSecurityConfig="@xml/network_security_config"><!--網絡安裝配置-->
        <!--下載需要的配置-->
        <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>
    </application>

3.

class _DownTestState extends State<DownTest> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: MaterialButton(
          child: Text("data",style: TextStyle(color: Colors.white),),
          color: Colors.blue,
          onPressed: (){
            ()async{
              //獲得臨時地址
              Directory tempDir = await getTemporaryDirectory();
              String tempPath = tempDir.path;
              print(tempPath);
              //請求下載地址
              final taskId = await FlutterDownloader.enqueue(
                url: 'http://192.168.18.14:5000/down/1.apk',//下載地址
                savedDir: tempPath,//保存路徑
                showNotification: true, // show download progress in status bar (for Android)
                openFileFromNotification: true, // click on notification to open downloaded file (for Android)
              );

              print("taskId:"+taskId);
            }();
          },
        ),
      ),
    );
  }

  void initState() {

    //請求權限
    ()async{
      Map<Permission, PermissionStatus> statuses = await [ Permission.storage].request();
    }();

    //初始化
    () async{
      WidgetsFlutterBinding.ensureInitialized();
      await FlutterDownloader.initialize(
          debug: true // optional: set false to disable printing logs to console
      );
      //監聽回調
      FlutterDownloader.registerCallback(downloadCallback);
    }();

  }

  //下載回調方法
  static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
    print("id:" + id);
    print("status:" + status.toString());
    print("progress:" + progress.toString());
  }
}

 

二、IOS

1.開啟background mode

 

 

 

 2.添加sqlite

 

 3.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM