Flutter路由管理教程:push使用、pop、传递参数和返回参数
push使用
——(context).pushNamed('routeName');
这个方法只是将我们需要打开的页面推到栈顶来显示当前页面。参数为A字符串类型,传递页面对应的路由名称。路线名称必须在程序的主输入中定义。定义方法为:
void main() {
runApp(
new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder> {
'/screen1': (BuildContext context) => new Screen1(),
'/screen2' : (BuildContext context) => new Screen2(),
'/screen3' : (BuildContext context) => new Screen3(),
},
)
);
}
用法:(context).pushNamed('/screen1');直接进入screen1页面(每次创建新页面)
pushReplacementNamed——(context).pushReplacementNamed('/screen4');
表示当前页面在堆栈中的位置被跳转页面替换(导航器当前路线被替换为路线上的[routeName]) ),当新的页面到来时,之前的页面就会执行drain方法。官方描述如下:
Replace the current route of the navigator that most tightly encloses the
given context by pushing the route named [routeName] and then disposing
the previous route once the new route has finished animating in.
例如,如果您当前从第 1 页进入第 2 页,请使用 (context).pushReplacementNamed('/screen3');在第 2 页上打开第 3 页。进入第3页后,第2页将执行drain方法。当您返回到第 3 页时,您将返回到第 1 页。
用法:例如,从 SplashScreen 到 HomeScreen。它应该只显示一次,并且用户永远不应该从主屏幕返回到它。由于在本例中我们即将打开一个全新的屏幕,因此我们可能希望使用此方法来实现进入动画属性。
2.2 pushReplacement——( context, MaterialPageRoute(builder: (BuildContext context) => screen4()));
这个用法和上面一样,只是路由不同。上面是路由名称(页面对应的名称,必须在入口处定义(本文第一点)),而后者只需要刷新对应的页面即可,可以传递参数(该方法传递参数与本文后面提到的传递方法类似)。
——(context, '/screen4');
表示打开当前页面,然后跳转到指定页面(将当前路由放入导航器中,并将命名路由推送到其位置。)以下是官方说明:
Pop the current route off the navigator that most tightly encloses the
given context and push a named route in its place.
用法:例如购物应用中,有一个产品列表,用户可以通过过滤产品列表进一步选择产品。在此过程中,当用户点击过滤按钮时,会打开过滤条件选择界面。当用户单击“确定过滤器”按钮时,应该出现过滤器界面。并使用新的过滤器来访问产品列表。在这种情况下,popAndPushNamed 更合适。
——(context).pushNamedAndRemoveUntil('/screen4', (Route<dynamic> route) => false);
表示将指定页面添加到路由中,然后打开所有其他页面。 (Routeroute) => false 导致推送路由之前的所有路由被删除。这时会打开一个新的screen 4页面
Push the route with the given name onto the navigator, and then remove all
the previous routes until the `predicate` returns true.
用法:比如用户点击退出时,我们需要打开某个页面(比如点击退出后进入登录页面) ,此时用户不应点击返回。只要能打开任意页面就可以使用。
5.1 pushNamedAndRemoveUntil——(context).pushNamedAndRemoveUntil('/screen4', ('/screen1'));
指将指定页面添加到路由中,然后移除之前的路径,直到知道指定页面为止(将指定名称的路由推送到导航器中,然后移除该路径之前的所有路由,直到 'predicate ' ' 为止返回。)此时,堆栈中除 screen4 之外的页面都被销毁。单击可直接转至堆栈中的屏幕 4。 Screen4 目前正在重建
Push the route with the given name onto the navigator, and then remove all
the previous routes until the `predicate` returns true.
使用:例如购物应用程序的示例!或者任何需要支付交易程序的应用程序。因此,在这些应用程序中,一旦用户完成支付事件,任何与交易或购物车相关的屏幕都应该从堆栈中删除,并且用户应该被带到支付确认页面。单击后退按钮会将其带回到产品列表或主屏幕。用法示例:1-->2-->3、3 到 4、usage(context, "/screen4", ('/screen1'));如果此时单击第 4 页上的“返回”,程序将立即关闭。 1 -->2 -->3,3到4时使用(context, "/screen4", ('/'));此时,如果在第4页点击返回,则会立即返回到第1页。 1-->2-->1-->2-->3,使用(context,"/screen4",('/屏幕1'))从3到4;单击此时返回第 4 页将返回到第二页。 ?是的,并且可以传递参数。 (看名字就可以发现pushNamedAndRemoveUntil和pushAndRemoveUntil的区别)使用这种方法的方法如下:1-->2-->3、3到4,此时如果在第4页点击返回,就会出现您将直接返回第 1 页。
如果您使用
(
context,
MaterialPageRoute(builder: (BuildContext context) => Screen4()),
(Route<dynamic> route) => false,
);
,请在 4 之后输入。4 是唯一的页面。所有其他页面都会弹出堆栈,这与上面提到的pushNamedAndRemoveUntil一致。
——(context, ('/screen2'));
某些应用场景可能需要用户填写由三部分组成的长表单,该表单可能会显示在移动应用程序的三个连续屏幕上。现在,在表单的第三页上,用户决定取消填写表单。当用户单击“取消”时,所有先前与表单相关的屏幕都会出现,并且用户将返回到主屏幕,丢失所有与表单相关的数据(这就是我们在本例中想要的)。我们在这里不介绍任何新内容,只是回到老路线。
pop
1.(context).maybePop();
也许Pop会自动判断。如果出现当前页面,出现其他页面也没有问题。当前页面的弹出操作正在进行中。否则不会被执行。
2.(context).canPop();
canPop 判断当前页面能否进行pop操作,并返回bool值
3.(context).pop();
直接退出当前页面
传递参数和返回参数
传递参数的方式非常简单。在应该接收参数的页面上定义参数并将它们添加到其构造函数中。跳转到页面时,使用MaterialPageRoute,并在页面上传递。只需输入参数即可。
String params;
(context, new MaterialPageRoute(builder: (BuildContext context) => new mainPage(params)));
接收参数
class mainPage extends StatelessWidget {
final String userName;
mainPage();
@override
Widget build(BuildContext context) {
print(userName);
}
页面跳转,返回值:
String userName = "yinll";
(
context,
new MaterialPageRoute(
builder: (BuildContext context) =>
new Screen5(userName))).then((data){
result =data;
print(result);
});
然后在screen5中使用:(context).pop('这是第5页返回的参数');将返回值写入pop中。这时候就可以在上面获取到返回的数据了。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。