Show a snackbar after navigate in Flutter

折月煮酒 提交于 2019-11-30 12:52:27

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!