我正在尝试下载邮件系统的附件。 为此,我使用的是Flutter downloader,但是我需要将令牌传递给http客户端。
我认为此插件无法解决问题。
我尝试使用dio来做到这一点。 我可以下载文件,但是我不知道如何显示Android下载指示器(参见图片)
有人对插件有任何想法,或者有什么东西可以显示这个Android指示器?
编辑:我终于找到了解决方案。 实际上,除了Flutter_downloader之外,什么都没有显示下载指示符。因此,我保留了该插件,并在标头中传递了令牌。
赞:
Map<String, String> requestHeaders = {
'Authorization': 'Bearer ' + http.cookie,
};
final assetsDir = documentsDirectory.path + '/';
final taskId = await FlutterDownloader.enqueue(
url: url,
savedDir: assetsDir,
fileName: attachment.name,
headers: requestHeaders,
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
对不起,我的英语,谢谢赖安的纠正
答案 0 :(得分:0)
对于 Flutter_Downloader
如果您使用的是 API 29+(ANDROID 10 及更高版本),请在 AndriodManifest.xml 中添加以下代码
android:requestLegacyExternalStorage="true"
像这样
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...........
<application
..............
<!-- Add This line -->
tools:replace="android:label"
android:requestLegacyExternalStorage="true"> <!-- Add This line if you are targeting android API 29+-->
<activity>
...............
</activity>
</application>
答案 1 :(得分:-1)
在 Android 上使用 Flutter 显示下载进度通知的一种方法是使用 flutter_local_notifications
插件。这是您可以试用的示例。
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
String selectedNotificationPayload;
class ReceivedNotification {
ReceivedNotification({
@required this.id,
@required this.title,
@required this.body,
@required this.payload,
});
final int id;
final String title;
final String body;
final String payload;
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Text(
'Download Progress Notification',
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await _showProgressNotification();
},
tooltip: 'Download Notification',
child: Icon(Icons.download_sharp),
),
);
}
Future<void> _showProgressNotification() async {
const int maxProgress = 5;
for (int i = 0; i <= maxProgress; i++) {
await Future<void>.delayed(const Duration(seconds: 1), () async {
final AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails('progress channel', 'progress channel',
'progress channel description',
channelShowBadge: false,
importance: Importance.max,
priority: Priority.high,
onlyAlertOnce: true,
showProgress: true,
maxProgress: maxProgress,
progress: i);
final NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'progress notification title',
'progress notification body',
platformChannelSpecifics,
payload: 'item x');
});
}
}
}
这是应用运行的样子