RoutePathCallBack typedef
RoutePathCallBack =
Widget? Function(RouteInformation routeInformation)
author:郑再红 email:1096877329@qq.com date: 2022-12-05 time: 09:12 describe 基于路由2.0实现界面跳转 支持1.0中的路由传值,回传取值 支持多路由栈架构(主路由栈 + 抽屉路由栈)
Implementation
// ============ 主路由栈使用示例 ============
//
// void initRouter() {
// router = RouterProxy.getInstance(
// pageMap: {
// '/': const HomePage(),
// '/login': const LoginPage(),
// '/profile': const ProfilePage(),
// },
// notFoundPage: const NotFoundPage(),
// exitWindow: _confirmExit,
// );
//
// // 添加命名路由守卫
// router.addRouteGuard((from, to) async {
// final protectedRoutes = ['/profile'];
// if (protectedRoutes.contains(to.uri.toString()) && !_isLoggedIn) {
// router.pushNamed(name: '/login');
// return false;
// }
// return true;
// });
//
// // 添加页面类型守卫
// router.addPageTypeGuard((fromPageType, toPageType) async {
// final protectedPageTypes = [ProfilePage];
// if (protectedPageTypes.contains(toPageType) && !_isLoggedIn) {
// router.pushNamed(name: '/login');
// return false;
// }
// return true;
// });
// }
//
// MaterialApp.router(
// routerDelegate: router,
// routeInformationParser: router.defaultParser(),
// );
//
// ============ 抽屉路由栈使用示例 ============
//
// class MyHomePage extends StatefulWidget {
// @override
// _MyHomePageState createState() => _MyHomePageState();
// }
//
// class _MyHomePageState extends State<MyHomePage> {
// late final RouterProxy drawerRouter;
//
// @override
// void initState() {
// super.initState();
//
// // 创建抽屉路由实例
// drawerRouter = RouterProxy.getDrawerInstance(
// stackId: 'main-drawer',
// pageMap: {
// '/': DrawerHomePage(),
// '/settings': DrawerSettingsPage(),
// },
// drawerConfig: DrawerConfig(
// autoOpen: true, // 首次 push 时自动打开抽屉
// autoClose: true, // 栈为空时自动关闭抽屉
// isEndDrawer: true, // 右侧抽屉
// ),
// );
// }
//
// @override
// void dispose() {
// // 清理资源
// RouterProxy.removeDrawerInstance('main-drawer');
// super.dispose();
// }
//
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(title: Text('主页')),
// // 使用 SimpleDrawerWidget,自动处理 context 绑定和刷新
// endDrawer: SimpleDrawerWidget(
// router: drawerRouter,
// width: 300,
// ),
// body: ElevatedButton(
// onPressed: () {
// // 打开抽屉并跳转到设置页
// drawerRouter.pushNamed(name: '/settings');
// },
// child: Text('打开抽屉设置'),
// ),
// );
// }
// }
//
// // 抽屉内的页面
// class DrawerHomePage extends StatelessWidget {
// @override
// Widget build(BuildContext context) {
// final drawerRouter = RouterProxy.getDrawerInstance(stackId: 'main-drawer');
//
// return Column(
// children: [
// AppBar(
// title: Text('抽屉菜单'),
// actions: [
// IconButton(
// icon: Icon(Icons.close),
// onPressed: () => drawerRouter.closeDrawerStack(),
// ),
// ],
// ),
// ListTile(
// leading: Icon(Icons.settings),
// title: Text('设置'),
// onTap: () => drawerRouter.push(page: DrawerSettingsPage()),
// ),
// ],
// );
// }
// }
//
// ============ 抽屉路由栈特性 ============
//
// 1. 自动刷新:push/pop 时自动更新抽屉显示
// 2. 自动绑定:SimpleDrawerWidget 自动处理 context 绑定
// 3. 完整功能:支持路由守卫、启动模式、值回传等
// 4. 多实例:可创建多个独立的抽屉路由栈
//
// 三种封装 Widget:
// - SimpleDrawerWidget:最简单,推荐使用
// - StyledDrawerWidget:支持自定义样式
// - DrawerRouterWidget:完全自定义子组件
//
// 查看完整文档:DRAWER_ROUTER_USAGE.md
//
// ============ exitWindow 示例 ============
//
// Future<bool> _confirmExit(BuildContext context) async {
// final result = await showDialog<bool>(
// context: context,
// builder: (context) {
// return AlertDialog(
// content: const Text('确定要退出App吗?'),
// actions: [
// TextButton(
// child: const Text('取消'),
// onPressed: () => Navigator.pop(context, true),
// ),
// TextButton(
// child: const Text('确定'),
// onPressed: () => Navigator.pop(context, false),
// ),
// ],
// );
// });
// return result ?? true;
// }
typedef RoutePathCallBack = Widget? Function(RouteInformation routeInformation);