import 'dart:io'; import 'package:dio/dio.dart'; class DownloadPage extends StatefulWidget { @override _DownloadPageState createState() => _DownloadPageState(); } class _DownloadPageState extends State<DownloadPage> { double _progress = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("下载文件")), body: Column( children: [ RaisedButton( onPressed: () async { final url = "https://example.com/file.zip"; final filename = "file.zip"; await downloadFile(url, filename); }, child: Text("下载"), ), Container( width: double.infinity, child: LinearProgressIndicator(value: _progress), ), ], ), ); } Future<void> downloadFile(String url, String filename) async { Dio dio = Dio(); try { Response response = await dio.download(url, filename, onReceiveProgress: (received, total) { setState(() { if (total != -1) { _progress = received / total; } }); }); print(response.data); } catch (e) { print(e); } } }
Flutter Dio 下载文件并显示下载进度
16 min read