0

I have a task that will run in the background (in an isolate) and when it finishes, I want to show a Snackbar. However, the user may navigate to a different screen from the one where the task was initiated. How do I show an 'app-level' Snackbar, not bound to any particular screen?

Edit: I found this: How to show a SnackBar from async executions when no context is available?, has some good information and option 1 (Display errors in page scaffolds) seems to be what I want, but I need to implement all by myself. I was hoping for something built in into Flutter.

wujek
  • 10,112
  • 12
  • 52
  • 88
  • Does this answer your question? [How to show snackBar without Scaffold](https://stackoverflow.com/questions/57267165/how-to-show-snackbar-without-scaffold) – Akif Oct 13 '20 at 16:15
  • Not really. My app does have a Scaffold. The answer in that question is something I know how to do, it simply shows how to show a Snackbar. I don't know which screen will be active at the time the task ends, so I would either have to have a 'listener' for that and show the Snackbar (which is very repetitive code, the same in each screen) or do something else, that I don't know of yet and is the topic of my question. – wujek Oct 13 '20 at 16:20
  • I have no problem with showing a Snackbar. My problem is that all the solutions that occurred to me involved repeating the Snackbar-showing code in every single screen. I would have hoped Flutter has some magic way of simply showing a Snackbar no matter which screen is open (i.e. a 'global' Snackbar), but I guess it doesn't. – wujek Oct 14 '20 at 07:29
  • @pskink I have no code yet, but you can see the general idea below in the answer by Robert Sandberg (https://stackoverflow.com/a/64343142/1385578). – wujek Oct 14 '20 at 22:04
  • Right, I will try it out, thanks. However, saying 'just retrieve top level ScaffoldState' it not very specific - how do I get it? Do I need some kind of a GlobalKey or something? Where would I look up the top level state? In the background task? – wujek Oct 15 '20 at 06:46
  • No, not really, I'm fairly new to Flutter, and pretty much all the work I've done with it has been non-UI parts of the app. – wujek Oct 15 '20 at 09:46

1 Answers1

0

I'd give this a try where you go for an abstract base class where you implement the 'listener', which then all your pages extend from instead of e.g. StatelessWidget.

Instead of overriding the normal build method, just override your new special build.

Pseudo code (here with BlocListener):

abstract class MySnackbarShowingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocListener<SubjectBloc, SubjectState>(
      listener: (context, state) {
        // TODO: implement snackbar display
      },
      child: mySpecialBuild(context),
    );
  }

  Widget mySpecialBuild(BuildContext context);
}

class MySpecificPage extends MySnackbarShowingPage {
  @override
  Widget mySpecialBuild(BuildContext context) {
    // TODO: implement mySpecialBuild as your normal page does today
  }
}
Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30
  • Thanks. I know I can use inheritance for it (although extending anything other than Stateless/fulWidget seems odd, but not wrong) or maybe even mixins, but I was hoping for some build-in mechanism in Flutter. And coincidently, the 'listener' would be BlocListener in my case, I'm using flutter_bloc ;d – wujek Oct 13 '20 at 21:33
  • Fair enough. It would however solve your issue with having duplicated code. FYI I tried it using BlocListener and Flushbar. Seems to work! – Robert Sandberg Oct 13 '20 at 21:37