flutter路由配置
创始人
2024-11-14 13:35:05
0

路由配置文件

import 'package:flutter/material.dart'; import 'package:ichat/routers/pages/form_page.dart'; import 'package:ichat/routers/pages/person_page.dart'; import 'package:ichat/routers/pages/search_page.dart'; import 'package:ichat/routers/router_page.dart'; class RouteConfig {  static String initRoute="/";   static Map routes = {     initRoute: (context) => const StaticRouterPage(),     '/person': (context) => const PersonPage(),     '/search': (context) => const SearchPage(),     "/form": (context,{arguments}) => FormPage(arguments:arguments),   };   static var onGenerateRoute = (RouteSettings settings) {     // print("$settings  ,name: ${settings.name} ,arguments: ${settings.arguments}"); //生成路由     String? name = settings.name;     var arguments = settings.arguments;     Function? func = routes[name];     if (func != null) {       if (arguments == null) {         return MaterialPageRoute(builder: (context) => func!(context));       } else {         return MaterialPageRoute(builder: (context) => func!(context, arguments));       }     }     return null;   }; }  

程序启动主函数文件

 import 'package:flutter/material.dart'; import 'package:ichat/routers/route_config.dart';   main(){     runApp(       MaterialApp(         title: "dynamic router",         debugShowCheckedModeBanner: false,         theme: ThemeData(primarySwatch: Colors.blue),         // home: const StaticRouterPage(), //home和 initialRoute不能同时存在,否则会报错         initialRoute: RouteConfig.initRoute,         routes: RouteConfig.routes,         onGenerateRoute: RouteConfig.onGenerateRoute,       )   );   } 

路由使用

import 'package:flutter/material.dart'; import 'package:ichat/routers/pages/news_page.dart'; import 'package:ichat/routers/pages/person_page.dart'; import 'package:ichat/routers/pages/search_page.dart';  class StaticRouterPage extends StatefulWidget {   const StaticRouterPage({super.key});    @override   State createState() => _StaticRouterPageState(); }  class _StaticRouterPageState extends State {   @override   Widget build(BuildContext context) {     return Scaffold(         appBar: AppBar(title: Text("路由"),),         body: Container(             child: Center(                 child: Column(                     mainAxisAlignment: MainAxisAlignment.center,                     children: [                       Row(                           mainAxisAlignment: MainAxisAlignment.center,                           crossAxisAlignment: CrossAxisAlignment.center,                           children: [                             ElevatedButton(                                 onPressed: () {                                   Navigator.of(context).push(MaterialPageRoute(                                     builder: (context) => const SearchPage(),                                   ));                                 },                                 child: Text("搜索")),                             SizedBox(width: 20,),                             ElevatedButton(                                 onPressed: () {                                   Navigator.of(context).push(MaterialPageRoute(                                     builder: (context) => const PersonPage(),                                   ));                                 },                                 child: Text("我的")),                             SizedBox(width: 20,),                             ElevatedButton(                                 onPressed: () {                                   Navigator.of(context).push(MaterialPageRoute(                                     builder: (context) => const NewsPage(title: "国际小新闻",newsId: 20),                                   ));                                 },                                 child: Text("新闻")),                           ]),                         SizedBox(height: 20,),                       Row(                           mainAxisAlignment: MainAxisAlignment.center,                           crossAxisAlignment: CrossAxisAlignment.center,                           children: [                             ElevatedButton(                                 onPressed: () {                                   Navigator.of(context).pushNamed("/search");                                 },                                 child: Text("搜索")),                             SizedBox(width: 20,),                             ElevatedButton(                                 onPressed: () {                                   Navigator.of(context).pushNamed("/form",arguments: {                                     "key1":"value1",                                     "key2":"value2",                                   });                                 },                                 child: Text("form")),                             SizedBox(width: 20,),                             ElevatedButton(                                 onPressed: () {                                   Navigator.pushNamed(context,"/person");                                 },                                 child: Text("我的")),                             // SizedBox(width: 20,),                             // ElevatedButton(                             //     onPressed: () {                             //       Navigator.of(context).push(MaterialPageRoute(                             //         builder: (context) => const NewsPage(title: "国际小新闻",newsId: 20),                             //       ));                             //     },                             //     child: Text("新闻")),                           ]),                     ]                 )                  )             )      );   } }

路由使用的几种方式

1、push不带参数

Navigator.of(context).push(MaterialPageRoute(                                     builder: (context) => const PersonPage(),                                   ));

2、push带参数

Navigator.of(context)          .push(MaterialPageRoute(builder: (context) => const NewsPage(title: "国际小新闻",newsId: 20),                                   ));

3、通过命名路由配置pushNamed

Navigator.of(context).pushNamed("/search");

4、 通过命名路由配置带参数pushNamed

Navigator.of(context).pushNamed("/form",arguments: {   "key1":"value1",   "key2":"value2", });

5、路由返回

Navigator.of(context).pop()

6、pushReplacementNamed

Navigator.of(context).pushReplacementNamed("/search");

相关内容

热门资讯

一分钟推荐“金花房卡正规购买渠... 牛魔王正厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
牛牛金花房卡是如何购买的/微信... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
给大家讲解“金花链接房卡在哪里... 九酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
房卡必备教程“微信金花房房卡怎... 新天道是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享...
一分钟了解“微信牛牛房卡哪里买... 新众乐是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...
ia实测“微信炸金花房卡怎么开... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
牛牛在哪里购买房卡/微信群牛牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
一分钟了解“金花房卡微信充值/... 九酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
正版授权“微信金花链接房卡怎么... 战神大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
正版授权“微信金花怎么玩哪里有... 大圣大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
一分钟了解“牛牛房卡批发平台/... 道游大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
牛牛金花房卡链接在哪买/微信金... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
炸金花房间链接游戏房卡/金花链... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
炸金花链接如何开房卡/炸金花房... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
秒懂教程“微信牛牛房卡在哪里买... 新永和是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...
微信拼三张房卡怎么获得/微信金... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
炸金花链接如何开房卡/牛牛金花... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
微信拼三张房卡怎么获得/链接金... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
玩家攻略,游戏推荐牛牛房卡出售... 玩家攻略,游戏推荐牛牛房卡出售老神兽/青鸟大厅/房卡链接在哪购买老神兽/青鸟大厅是一款非常受欢迎的游...
ia实测“可以自己开房炸金花房... 新星游是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享...