Flutter:popAndPushNamed带有传递到新页面的参数? [英] Flutter: popAndPushNamed with arguments passing to new page?

查看:1666
本文介绍了Flutter:popAndPushNamed带有传递到新页面的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Flutter上编写应用程序。我需要做popAndPushNamed,但我还需要将参数传递给我要推送的页面。我能怎么做?是否有另一种弹出和推送方式,或者我必须寻找其他解决方案?这件事让我发疯。

I am writing an application on Flutter. I need to do popAndPushNamed but I also need to pass arguments to the Page I am pushing. How can I do? Is there another way to pop and push or I have to look for a different solution? This thing is making me insane.

谢谢!

推荐答案

要向小部件发送数据,可以使用其构造函数

For sending data to widget you can use it's constructor

String result = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondWidget("String")));

在这种情况下, result 是新字符串,将从 SecondWidget

In this case result is new string, which will be returned from SecondWidget

class SecondWidget extends StatefulWidget {
  SecondWidget(this.text);
  final String text;

SecondWidgetState 中的某个地方:

Navigator.pop(context, "New string");

这将关闭 SecondWidget 并返回结果
我认为它已经足够清楚并且会有所帮助

This will close SecondWidget and return value for result I think it's clear enough and it'll help

UPD >
如何处理您的情况(类似这样)

UPD How to do in your case (something like this)

Profile profile = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginWidget()));
if (profile != null) Navigator.of(context).push(MaterialPageRoute(builder: (context) => MainWidget(profile)));

LoginWidget

void _afterLogIn() {
  // do something
  Navigator.pop(context, this.receivedProfile);
}

MainWidget 中:

class MainWidget extends StatefulWidget {
  MainWidget(this.profile);
  Profile profile;

这篇关于Flutter:popAndPushNamed带有传递到新页面的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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