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

Tho*_*ole 6 android file download indicator flutter

我正在尝试下载邮件系统的附件。为此,我使用Flutter 下载器,但我需要通过 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)
);
Run Code Online (Sandbox Code Playgroud)

抱歉我的英语不好,感谢 Ryan 的纠正

Oma*_*att 2

在 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');
      });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是应用程序运行时的样子

演示