温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flutter中怎么实现局部路由

发布时间:2021-08-07 15:05:59 来源:亿速云 阅读:189 作者:Leah 栏目:编程语言

Flutter中怎么实现局部路由,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Navigator的使用无非3个属性

initialRoute: 初始路由  onGenerateRoute: 匹配路由  onUnknownRoute: 404

在实现层面

首先:Navigator的高度为infinity。如果直接父级非最上级也是infinity会产生异常,例如,Scaffold -> Column -> Navigator。所以:Navigator需要附件限制高度,例如:Scaffold -> Column -> Container(height: 300) -> Navigator

然后:在onGenerateRoute属性中,使用第一个BuildContext参数,能够在MaterialApp未设置route的情况下使用Navigator.pushNamed(nContext, '/efg');跳到对应的子路由中。

最后:Navigator执行寻找路由顺序是 initialRoute -> onGenerateRoute -> onUnknownRoute,这个和React的Route是类似的。

最后附上源码

import 'package:flutter/material.dart';class NavigatorPage extends StatefulWidget { @override _NavigatorPageState createState() => _NavigatorPageState();}class _NavigatorPageState extends State<NavigatorPage> { @override Widget build(BuildContext context) {  return Scaffold(   appBar: AppBar(    title: Text('Navigator'),   ),   body: Column(    children: <Widget>[     Text('Navigator的高度为infinity'),     Text('如果直接父级非最上级也是infinity会产生异常'),     Container(      height: 333,      color: Colors.amber.withAlpha(111),      child: Navigator( // Navigator       initialRoute: '/abc',       onGenerateRoute: (val) {        RoutePageBuilder builder;        switch (val.name) {         case '/abc':          builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Column(           // 并没有在 MaterialApp 中设定 /efg 路由           // 因为Navigator的特性 使用nContext 可以跳转 /efg           children: <Widget>[            Text('呵呵呵'),            RaisedButton(             child: Text('去 /efg'),             onPressed: () {              Navigator.pushNamed(nContext, '/efg');             },            )           ],          );         break;         case '/efg':          builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Row(           children: <Widget>[            RaisedButton(             child: Text('去 /hhh'),             onPressed: () {              Navigator.pushNamed(nContext, '/hhh');             },            )           ],          );         break;         default:          builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Center(           child: RaisedButton(            child: Text('去 /abc'),            onPressed: () {             Navigator.pushNamed(nContext, '/abc');            },           )          );        }        return PageRouteBuilder(         pageBuilder: builder,         // transitionDuration: const Duration(milliseconds: 0),        );       },       onUnknownRoute: (val) {        print(val);       },       observers: <NavigatorObserver>[]      ),     ),     Text('Navigator执行寻找路由顺序'),     Text('initialRoute'),     Text('onGenerateRoute'),     Text('onUnknownRoute'),    ],   ),  ); }}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI