Flutter中怎么实现局部路由-创新互联

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

成都创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元阜城做网站,已为上家服务,为阜城各地企业和个人服务,联系电话:028-86922220

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 { @override Widget build(BuildContext context) {  return Scaffold(   appBar: AppBar(    title: Text('Navigator'),   ),   body: Column(    children: [     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 animation, Animation secondaryAnimation) => Column(           // 并没有在 MaterialApp 中设定 /efg 路由           // 因为Navigator的特性 使用nContext 可以跳转 /efg           children: [            Text('呵呵呵'),            RaisedButton(             child: Text('去 /efg'),             onPressed: () {              Navigator.pushNamed(nContext, '/efg');             },            )           ],          );         break;         case '/efg':          builder = (BuildContext nContext, Animation animation, Animation secondaryAnimation) => Row(           children: [            RaisedButton(             child: Text('去 /hhh'),             onPressed: () {              Navigator.pushNamed(nContext, '/hhh');             },            )           ],          );         break;         default:          builder = (BuildContext nContext, Animation animation, Animation 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: []      ),     ),     Text('Navigator执行寻找路由顺序'),     Text('initialRoute'),     Text('onGenerateRoute'),     Text('onUnknownRoute'),    ],   ),  ); }}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


网站题目:Flutter中怎么实现局部路由-创新互联
URL链接:http://pwwzsj.com/article/cdedjp.html