一、配置路由

  1. import 'package:flutter/material.dart';
  2. import 'pages/Tabs.dart';
  3. import 'pages/Search.dart';
  4. import 'pages/Form.dart';
  5. void main() => runApp(MyApp());
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. initialRoute: '/',
  11. routes: {
  12. '/':(contxt)=>Tabs(),
  13. '/search':(contxt) =>SearchPage(),
  14. '/form': (context) => FormPage(),
  15. },
  16. );
  17. }
  18. }

二、路由跳转

  1. ElevatedButton(
  2. child: const Text("搜索一下"),
  3. onPressed: (){
  4. Navigator.pushNamed(context,'/search'); // 去配置的路由中寻找对应的组件,找到了就进行跳转
  5. }
  6. )

三、命名路由跳转传值

官方文档: https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

routes.dart

  1. import 'package:flutter/material.dart';
  2. import 'package:untitled/pages/Search.dart';
  3. import 'package:untitled/pages/Tabs.dart';
  4. import 'package:untitled/pages/product.dart';
  5. import 'package:untitled/pages/product_details.dart';
  6. import 'package:untitled/pages/user/login.dart';
  7. import 'package:untitled/pages/user/register1.dart';
  8. import 'package:untitled/pages/user/register2.dart';
  9. import '../pages/formPage.dart';
  10. final routes = {
  11. '/': (context) => Tabs(),
  12. '/form': (context,{arguments}) => formPage(arguments: arguments),
  13. '/search': (context,{arguments}) => Search(arguments: arguments),
  14. '/product': (context,{arguments}) => productPage(arguments: arguments),
  15. '/product_detail': (context,{arguments}) => productDetailPage(arguments: arguments),
  16. '/user/login' : (context) => Login(),
  17. '/user/register/first': (context) => RegisterFirst(),
  18. '/user/register/second': (context) => RegisterSecond()
  19. };
  20. // 命令路由传参数的统一命名方式
  21. var onGenerateRoute = (RouteSettings settings) {
  22. // 统一处理
  23. final String? name = settings.name;
  24. final Function? pageContentBuilder = routes[name];
  25. if (pageContentBuilder != null) {
  26. if (settings.arguments != null) {
  27. final Route route = MaterialPageRoute(
  28. builder: (context) => pageContentBuilder(context,
  29. arguments: settings.arguments)
  30. );
  31. return route;
  32. }else{
  33. final Route route = MaterialPageRoute(
  34. builder: (context) => pageContentBuilder(context)
  35. );
  36. return route;
  37. }
  38. }
  39. };

main.dart

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import './routes/routes.dart';
  4. void main() => runApp(MyApp());
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. initialRoute: '/',
  10. // routes: routes,
  11. onGenerateRoute: onGenerateRoute, // 将routes.dart中的onGenerateRoute赋值过来
  12. );
  13. }
  14. }

跳转传值实例

cate.dart

  1. import 'package:flutter/material.dart';
  2. import '../formPage.dart';
  3. class Cate extends StatefulWidget {
  4. const Cate({ Key? key }) : super(key: key);
  5. @override
  6. _CateState createState() => _CateState();
  7. }
  8. class _CateState extends State<Cate> {
  9. @override
  10. Widget build(BuildContext context) {
  11. return Center(
  12. child: TextButton(
  13. onPressed: (){
  14. Navigator.pushNamed(context, '/form',arguments: {
  15. 'name': 'yxr'
  16. });
  17. },
  18. child: Text("表单"),
  19. )
  20. );
  21. }
  22. }

formPage.dart

  1. import 'package:flutter/material.dart';
  2. class formPage extends StatelessWidget {
  3. // const formPage({ Key? key }) : super(key: key);
  4. final arguments; // 定义arguments参数
  5. formPage({this.arguments}); // 在构造函数中来接收传入的参数
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text("表单"),
  11. ),
  12. body: Container(
  13. child: Text("表单页面${arguments != null ? arguments['name'] : '0'}"),
  14. )
  15. );
  16. }
  17. }