【问题标题】:Flutter - download file with android download indicatorFlutter - 使用 android 下载指示器下载文件
【发布时间】:2019-08-24 19:43:57
【问题描述】:

我正在尝试下载邮件系统的附件。 为此,我使用了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)
);

对不起我的英语并感谢 Ryan 的更正

【问题讨论】:

标签: android file download flutter indicator


【解决方案1】:

使用 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');
      });
    }
  }
}

这是应用运行的样子

【讨论】:

    【解决方案2】:

    对于 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2018-06-10
      相关资源
      最近更新 更多