Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a snackbar after navigate in Flutter

Tags:

flutter

dart

After an action (for example, "save"), user is returned to another page in the app.

I want to show a snackbar on the new page to confirm the action, but don't want it to show if the user navigated there without the action. So for example, if user saves "thing", the app sends them to "things" main page and shows the "saved" snackbar, but if they go to "things" main page some other way, I don't want the "saved" snackbar to be there.

Here is my code on the panel that saves, but the destination page does not show the snackbar — regardless where I place the snackbar (before or after navigation), it does not execute the snackbar.

Future<Null> saveThatThing() async {   thing.save();   thing = new Thing();   Navigator.of(context).push(getSavedThingRoute());   Scaffold.of(context).showSnackBar(     new SnackBar(       content: new Text('You saved the thing!'),     ),   ); } 

What is the best way to do this?

like image 652
Deborah Avatar asked Dec 20 '17 21:12

Deborah


People also ask

How do I display SnackBar Flutter?

The ElevatedButton is a child widget placed on the return Center . Then, we use the ScaffoldMessenger class to display the SnackBar. Clicking on the button will show the SnackBar with the following message: “Hi, I am a SnackBar!”

How do you show messages on Flutter?

In its on the pressed property, we have to use the showDialog widget of flutter. It takes context and a builder. In builder, we provide the AlertDialog widget with title, content(Description of a title), and actions (Yes or no buttons), and our alert dialog box is ready to use.

How do you customize SnackBar in Flutter?

We can create flutter snackbar in our application by calling its constructor and there is only one required property to create a snackbar which is content. Usually, we will use a Text widget for content since we need to show some message to the user. We can use other widgets instead if we want.


2 Answers

What about if you create key for the screen Scaffold like this (put this object inside the screen state class):

final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>(); 

and then set this variable to the Scaffold key like this:

return new Scaffold(   key: scaffoldKey,   appBar: new AppBar(     .......,   ), 

and at the end to show the SnackBar just use somewhere inside the screen state class this code :

scaffoldKey.currentState     .showSnackBar(new SnackBar(content: new Text("Hello"))); 

or you can wrap the latest code in method like this :

void showInSnackBar(String value) { scaffoldKey.currentState     .showSnackBar(new SnackBar(content: new Text(value)));} 

and then just call this method and pass the snack bar message inside.

If you want to show the snack bar message on the next screen, after you navigate away from the initial context, just call the snack bar method on the next screen, not on the first screen.

Hope this will help

like image 100
Stoycho Andreev Avatar answered Oct 18 '22 06:10

Stoycho Andreev


You have to return a result to the previous page which will represent the action shown on the page.

In the 1st page When you are navigating to the page change a few things.

bool result=await Navigator.of(context).push(/*Wherever you want*/); 

Then in the second page when you are returning to the previous page send some result to the 1st page.

Navigator.of(context).pop(true); 

if the work is not success you can return false.

You can return any type of object as result

Then In the 1st page you can check for the result and show the snackBar accordingly

bool result=await Navigator.of(context).push(/*Wherever you want*/); if(result!=null && result==true){     //Show SnackBar }else{     //Some other action if your work is not done } 
like image 35
Guru Prasad mohapatra Avatar answered Oct 18 '22 05:10

Guru Prasad mohapatra