gpt4 book ai didi

android - Flutter - 使用 android 下载指示器下载文件

转载 作者:行者123 更新时间:2023-12-04 14:27:14 28 4
gpt4 key购买 nike

我正在尝试下载邮件系统的附件。
为此,我使用 Flutter downloader但我需要通过我的 http 客户端传递我的 token 。

我认为这个插件不会照顾它。

我尝试使用 dio 来做到这一点.
我可以下载文件,但我不知道如何显示 Android 下载指示器(参见图片)

enter image description here

有人知道要显示此 Android 指示器的插件或其他东西吗?

编辑:我终于找到了解决方案。
实际上,除了 Flutter_downloader 之外,没有任何东西可以显示下载指示器。所以我保留了这个插件,并在标题中传递了我的 token 。

像这样 :

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)
);

对不起我的英语,感谢瑞恩的更正

最佳答案

使用 Flutter 在 Android 上显示下载进度通知的一种方法是使用 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');
});
}
}
}
这是应用程序运行的样子
demo

关于android - Flutter - 使用 android 下载指示器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492437/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com