0

Inside my app the user can download some data. If that finished I would like to show a Snackbar.

I know I can show it inside a Widget with:

ScaffoldMessenger.of(context).showSnackBar(snackBar);

The thing is that the user can change Screen during the download. But the user should still be prompted with the snackbar no matter the page he is currently on.

What is the best way to achieve this? I couldn't find anything on this... Let me know if you need more info!

Chris
  • 1,828
  • 6
  • 40
  • 108

2 Answers2

1

Not sure if this is what you are looking for but using the package below might be the simplest way to achieve it.

flutter_local_notifications: ^5.0.0+1 // I used this version to test the concept.

Edit the LocalNotificationService configuration to your need, like to silently notify the user.

Setup the package from flutter_local_notification.

  // Setting up flutter_local_notifications: ^5.0.0+1 as a Service

    import 'package:timezone/timezone.dart' as tz;
    import 'package:timezone/data/latest.dart' as tz;
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';

    class LocalNotificationService {
      static final instance = LocalNotificationService();

      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();

      initialize() async {
        tz.initializeTimeZones();
        var initializationSettingsAndroid =
            AndroidInitializationSettings('ic_notification');
        var initializationSettingsIOS = IOSInitializationSettings(
          onDidReceiveLocalNotification: onDidReceiveLocalNotification,
        );
        var initializationSettings = InitializationSettings(
          iOS: initializationSettingsIOS,
          android: initializationSettingsAndroid,
        );
        await flutterLocalNotificationsPlugin.initialize(
          initializationSettings,
          onSelectNotification: (String payload) { 
            // Do something on notification click
            return null;
          } 
        );
      }

      Future<void> scheduleNotification({
        int id,
        String body,
        String title,
        String payload,
        DateTime scheduledNotificationDateTime,
      }) async {
        final epoch = scheduledNotificationDateTime.microsecondsSinceEpoch;
        var androidSpecifics = AndroidNotificationDetails(
          '$epoch',
          title,
          body,
          priority: Priority.low,
          importance: Importance.min,
        );
        var iOSSpecifics = IOSNotificationDetails();
        NotificationDetails platformChannelSpecifics = NotificationDetails(
          iOS: iOSSpecifics,
          android: androidSpecifics,
        );
        final tzTime = tz.TZDateTime.fromMicrosecondsSinceEpoch(tz.local, epoch);
        await flutterLocalNotificationsPlugin.zonedSchedule(
          id,
          title,
          body,
          tzTime,
          platformChannelSpecifics,
          payload: payload,
          androidAllowWhileIdle: true,
          uiLocalNotificationDateInterpretation:
              UILocalNotificationDateInterpretation.wallClockTime,
        );
      }

      Future onDidReceiveLocalNotification(
          int i, String string1, String string2, String string3) async {}

      Future cancelAllNotifications() async {
        await flutterLocalNotificationsPlugin.cancelAll();
      }

      Future cancelNotification({int withId}) async {
        await flutterLocalNotificationsPlugin.cancel(withId);
      }
    }

Call this function from anywhere,

    LocalNotificationService.instance.scheduleNotification(
      body: "This is test notification",
      title: "Testing",
      id: 1222,
      scheduledNotificationDateTime: DateTime.now(),
    );
Alish Giri
  • 1,855
  • 18
  • 21
0

You use the downloading function on the new dart file and put the snack bar on that page after the download succeeded you can call it. that's working now properly or use the mounting method to call the snack bar function.

Use the flush bar package to give the best output.

Mahendran K
  • 188
  • 1
  • 13
  • I dont want to call `showSnackBar` on every single Page. I would like to be able to call it globally and only once. – Chris Jun 17 '21 at 11:58
  • you mean snackbar will appear until the download completes and you will be able to change the pages and the snackbar wil still appearing even page is changed? – Mehran Ullah Jun 17 '21 at 12:10
  • @Chris Check the above your solution is there – Mahendran K Jul 01 '22 at 06:40