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.

 

posted @ 2020-06-23 14:53  富坚老贼  阅读(2473)  评论(0编辑  收藏  举报