flutter 路由守卫

main.dart文件

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   return MaterialApp(
     title: 'Flutter Demo',
     //路由守卫 路由执行顺序 home ->  route -> onGenerateRoute -> onUnknownRoute
      onGenerateRoute: (RouteSettings routeSettings) {
        WidgetBuilder builder;
        // 获取路由名称 routeSettings.name
        // 获取路由参数 routeSettings.arguments
        //拦截路由名称 跳转到对应组件
        switch (routeSettings.name) {
          case 'cateLiveList':    //跳转到CateLiveList页面
            builder = (BuildContext context) => CateLiveList(data: routeSettings.arguments);
            break;
          default:
            throw new Exception('Invalid route: ${routeSettings.name}');
        }
        return CupertinoPageRoute(builder: builder, settings: routeSettings);
      }
   )  
 }

//跳转
Navigator.of(context).pushNamed('cateLiveList', arguments: {
 'title': '传参数',
});

//CateLiveList页面接收参数
class CateLiveList extends StatefulWidget {
  final arguments;  //定义一个变量
  //重构
  CateLiveList({Key key, this.arguments}) : super(key: key);
  ...

你可能感兴趣的:(Flutter)