flutter go_router 官方路由(一)基本使用

1 项目中添加最新的依赖
go_router: ^13.1.0

如下图所示,我当前使用的flutter版本为3.16.0
在这里插入图片描述

然后修改应用的入口函数如下:

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
   
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
   
  const MyApp({
   super.key});
  
  
  Widget build(BuildContext context) {
   
    return MaterialApp.router(
      routerConfig: _router,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
    );
    // return MaterialApp(
    //   title: 'Flutter Demo',
    //   theme: ThemeData(
    //     colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
    //     useMaterial3: true,
    //   ),
    //   home:  MyHomePage(),
    // );
  }
}

其实就是将原来的入口 MaterialApp 使用 MaterialApp.router 替换了,参数 routerConfig 就是配置的所有的路由页面,比如我这里的配置如下:


final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
   
        return MyHomePage();
      },
      routes: <RouteBase>[
        GoRoute(
          path: 'two',
          builder: (BuildContext context, GoRouterState state) {
   
            return TwoPage();
          },
        ),
      ],
    ),
  ],
);

“/” 配置的就是加载的默认的启动的首页面,然后我这里面相当于是配置了两个基本的页面。

2 实现基本的页面交互

在页面MyHomePage放一个按钮,点击按钮打开 TwoPage 第二个页面,打开页面的核心代码如下:
使用 Navigator 正常操作如下“

    Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
   
      return TwoPage();
    }));

使用 go_router 操作如下:

context.go("/two");

然后在页面 TwoPage 中点击按钮返回上一个页面​:

使用 Navigator 正常操作如下:

 Navigator.of(context).pop();

​用 go_router 操作如下:

context.pop();
3 页面跳转传参数

基本简单参数传递,比如一个 userId,页面路由定义如下:ThreePage就是我定义的目标页面,它需要一个参数 userId。

GoRoute(
    path: 'three/:userId',
    builder: (BuildContext context, GoRouterState state) {
   
      // 使用 state.params 获取路由参数的值
      final userId = (state.pathParameters['userId']!);
      return ThreePage(userId:userId);
    },
  ),

点击一个按钮跳转目标页面,我这里传的参数 userId的值为 123

context.go("/three/123");

复杂传参数 请看后续

相关推荐

  1. 使用

    2024-02-05 06:20:02       30 阅读

最近更新

  1. ArduPilot开源代码之AP_OpticalFlow_MSP

    2024-02-05 06:20:02       0 阅读
  2. API分页处理指南:Postman中的高效数据浏览技巧

    2024-02-05 06:20:02       1 阅读
  3. 对称加密与非对称加密如何实现密钥交换

    2024-02-05 06:20:02       1 阅读
  4. 各种音频处理器

    2024-02-05 06:20:02       1 阅读
  5. this指针

    2024-02-05 06:20:02       1 阅读
  6. Object.defineProperty与Proxy对比【简单易懂】

    2024-02-05 06:20:02       1 阅读

热门阅读

  1. Chrome扩展开发纪要

    2024-02-05 06:20:02       38 阅读
  2. centos7安装google chrome和chromium

    2024-02-05 06:20:02       31 阅读
  3. Pinia的使用与原理

    2024-02-05 06:20:02       30 阅读
  4. CentOS磁盘扩容

    2024-02-05 06:20:02       38 阅读
  5. shell脚本初始化mysql密码

    2024-02-05 06:20:02       28 阅读
  6. 开源软件,推动技术创新

    2024-02-05 06:20:02       33 阅读
  7. Redis实现:每个进程每30秒执行一次任务

    2024-02-05 06:20:02       34 阅读
  8. 2024 极术通讯-安谋科技2023精彩征程回顾

    2024-02-05 06:20:02       30 阅读