在Flutter中导航后显示小吃店 [英] Show a snackbar after navigate in Flutter

查看:77
本文介绍了在Flutter中导航后显示小吃店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作(例如保存)后,用户返回到应用程序中的另一个页面。

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?

推荐答案

如果您为屏幕支架创建像这样的键(将这个对象放到屏幕状态类中)该怎么办:

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>();

,然后将此变量设置为Scaffold键,如下所示:

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

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

,最后显示SnackBar只需在屏幕状态类内使用代码:

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.

希望这会有所帮助

这篇关于在Flutter中导航后显示小吃店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆