- 添加依赖
dependencies:
inn_router: any
- 安装
flutter pub get
- 导入和配置
import 'package:inn_router/inn_router.dart';
Widget build(BuildContext context) {
return MaterialApp(
title: '',
navigatorKey: IRouter.navKey, //添加
);
}
- 配置路由表
在lib中创建router文件夹,如果项目比较大,可以按模块来创建,一个模块一个文件。
路径命名推荐:/模块名/功能名
import './page1.dart';
import 'package:inn_router/inn_router.dart';
class Routes {
static String path = "/router";
static IRouterClass get page1 => IRouterClass(widget: Page1(), path: "$path/page1");
static IRouterClass page2(String parameter) => IRouterClass(widget: Page1(parameter:parameter), path: "$path/page2");
}
- 使用
IRouter.push(Routes.page1);
IRouter.push(Routes.page2("我是参数"));
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
path | String | 必须 | 跳转路径 |
widget | Widget | 必须 | 页面 |
title | String | 可选 | 标题 |
roles | List | 可选 | 角色权限 |
transition | Route Function(IRouterClass) | 可选 | 单个路由过度动画 |
notAllowFun | List | 可选 | 权限不足回调,返回flase不跳转,true跳转 |
icon | int | 可选 | 图标 |
color | int | 可选 | 颜色 |
IRouter.roles = ["admin"];
IRouter.transition = IRouterTransition.cupertino;
class SlideRight extends PageRouteBuilder {
final IRouterClass route;
SlideRight({this.route})
: super(
transitionDuration: Duration(milliseconds: 300),
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
route.widget,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
SlideTransition(
position: Tween<Offset>(begin: Offset(-1, 0), end: Offset.zero).animate(animation),
child: child,
),
);
}
// 全局使用
IRouter.transition = (IRouterClass route) => SlideRight(route: route);
// 单个路由使用
IRouterClass(widget: Page1(), path: "$path/material", title: "material", transition: (IRouterClass route) => SlideRight(route: route));
IRouter.push(Routes.page1);
IRouter.pop();
IRouter.popUntil(Routes.page1);
IRouter.removeRoute(Routes.page1);
IRouter.pushReplacementNamed(Routes.page1);
IRouter.popAndPushNamed(Routes.page1);
IRouter.pushNamedAndRemoveUntil(Routes.page1, Routes.page2);