当前位置:   article > 正文

flutter路由配置_flutter 路由适配

flutter 路由适配

路由配置文件

  1. import 'package:flutter/material.dart';
  2. import 'package:ichat/routers/pages/form_page.dart';
  3. import 'package:ichat/routers/pages/person_page.dart';
  4. import 'package:ichat/routers/pages/search_page.dart';
  5. import 'package:ichat/routers/router_page.dart';
  6. class RouteConfig {
  7. static String initRoute="/";
  8. static Map<String, WidgetBuilder> routes = {
  9. initRoute: (context) => const StaticRouterPage(),
  10. '/person': (context) => const PersonPage(),
  11. '/search': (context) => const SearchPage(),
  12. "/form": (context,{arguments}) => FormPage(arguments:arguments),
  13. };
  14. static var onGenerateRoute = (RouteSettings settings) {
  15. // print("$settings ,name: ${settings.name} ,arguments: ${settings.arguments}");
  16. //生成路由
  17. String? name = settings.name;
  18. var arguments = settings.arguments;
  19. Function? func = routes[name];
  20. if (func != null) {
  21. if (arguments == null) {
  22. return MaterialPageRoute(builder: (context) => func!(context));
  23. } else {
  24. return MaterialPageRoute(builder: (context) => func!(context, arguments));
  25. }
  26. }
  27. return null;
  28. };
  29. }

程序启动主函数文件

  1. import 'package:flutter/material.dart';
  2. import 'package:ichat/routers/route_config.dart';
  3. main(){
  4. runApp(
  5. MaterialApp(
  6. title: "dynamic router",
  7. debugShowCheckedModeBanner: false,
  8. theme: ThemeData(primarySwatch: Colors.blue),
  9. // home: const StaticRouterPage(), //home和 initialRoute不能同时存在,否则会报错
  10. initialRoute: RouteConfig.initRoute,
  11. routes: RouteConfig.routes,
  12. onGenerateRoute: RouteConfig.onGenerateRoute,
  13. )
  14. );
  15. }

路由使用

  1. import 'package:flutter/material.dart';
  2. import 'package:ichat/routers/pages/news_page.dart';
  3. import 'package:ichat/routers/pages/person_page.dart';
  4. import 'package:ichat/routers/pages/search_page.dart';
  5. class StaticRouterPage extends StatefulWidget {
  6. const StaticRouterPage({super.key});
  7. @override
  8. State<StaticRouterPage> createState() => _StaticRouterPageState();
  9. }
  10. class _StaticRouterPageState extends State<StaticRouterPage> {
  11. @override
  12. Widget build(BuildContext context) {
  13. return Scaffold(
  14. appBar: AppBar(title: Text("路由"),),
  15. body: Container(
  16. child: Center(
  17. child: Column(
  18. mainAxisAlignment: MainAxisAlignment.center,
  19. children: [
  20. Row(
  21. mainAxisAlignment: MainAxisAlignment.center,
  22. crossAxisAlignment: CrossAxisAlignment.center,
  23. children: [
  24. ElevatedButton(
  25. onPressed: () {
  26. Navigator.of(context).push(MaterialPageRoute(
  27. builder: (context) => const SearchPage(),
  28. ));
  29. },
  30. child: Text("搜索")),
  31. SizedBox(width: 20,),
  32. ElevatedButton(
  33. onPressed: () {
  34. Navigator.of(context).push(MaterialPageRoute(
  35. builder: (context) => const PersonPage(),
  36. ));
  37. },
  38. child: Text("我的")),
  39. SizedBox(width: 20,),
  40. ElevatedButton(
  41. onPressed: () {
  42. Navigator.of(context).push(MaterialPageRoute(
  43. builder: (context) => const NewsPage(title: "国际小新闻",newsId: 20),
  44. ));
  45. },
  46. child: Text("新闻")),
  47. ]),
  48. SizedBox(height: 20,),
  49. Row(
  50. mainAxisAlignment: MainAxisAlignment.center,
  51. crossAxisAlignment: CrossAxisAlignment.center,
  52. children: [
  53. ElevatedButton(
  54. onPressed: () {
  55. Navigator.of(context).pushNamed("/search");
  56. },
  57. child: Text("搜索")),
  58. SizedBox(width: 20,),
  59. ElevatedButton(
  60. onPressed: () {
  61. Navigator.of(context).pushNamed("/form",arguments: {
  62. "key1":"value1",
  63. "key2":"value2",
  64. });
  65. },
  66. child: Text("form")),
  67. SizedBox(width: 20,),
  68. ElevatedButton(
  69. onPressed: () {
  70. Navigator.pushNamed(context,"/person");
  71. },
  72. child: Text("我的")),
  73. // SizedBox(width: 20,),
  74. // ElevatedButton(
  75. // onPressed: () {
  76. // Navigator.of(context).push(MaterialPageRoute(
  77. // builder: (context) => const NewsPage(title: "国际小新闻",newsId: 20),
  78. // ));
  79. // },
  80. // child: Text("新闻")),
  81. ]),
  82. ]
  83. )
  84. )
  85. )
  86. );
  87. }
  88. }

路由使用的几种方式

1、push不带参数

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

2、push带参数

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

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");
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/951281
推荐阅读
相关标签
  

闽ICP备14008679号