Show a snackbar after navigate in Flutter

2020-02-25 07:08发布

问题:

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?

回答1:

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



回答2:

Since showing SackBar require an active Scaffold, you will need to pass the message to the new route page something like getSavedThingRoute(message: 'You saved the thing!') and the new page is responsible for displaying the message.

Typically I happen to use Navigation.pop({'message': 'You saved the thing!', 'error': false}) to pass the message.



回答3:

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
}


标签: flutter dart