Thomas Nicole
Thomas Nicole

Reputation: 785

Flutter - download file with android download indicator

I am trying to download an attachment for a mailing system. To do that I am using Flutter downloader but I need to pass my token with my http client.

I think this plugin doesn't take care of it.

I have tried to do this using dio. I can download files but I don't know how I can display the Android download indicator (cf. image)

enter image description here

Does somebody have any idea of a plug-in or something to display this Android indicator ?

EDIT: I finally have found a solution. Actually, there are nothing to display the download indicator but Flutter_downloader. So I have kept this plugin and I have passed my token in headers.

Like this :

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

Sorry for my english and thanks to Ryan for the correction

Upvotes: 6

Views: 10867

Answers (2)

Arshad Barves A
Arshad Barves A

Reputation: 23

For Flutter_Downloader

If you are using API 29+(ANDROID 10 and Above) add the below code in AndriodManifest.xml

android:requestLegacyExternalStorage="true"

Like this

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

Upvotes: 1

Omatt
Omatt

Reputation: 10473

One way to display download progress notification on Android with Flutter is by using flutter_local_notifications plugin. Here's a sample that you can try out.

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

Here's how the app looks like running

demo

Upvotes: 2

Related Questions