This commit is contained in:
toly
2023-11-11 11:37:41 +08:00
parent 8fb4bf57d6
commit 5396712cf9
17 changed files with 368 additions and 101 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:iroute/v9/app/navigation/router/views/navigator_scope.dart';
import 'iroute.dart';
import 'iroute_config.dart';
import 'route_history_manager.dart';
@@ -7,11 +8,12 @@ import 'routes.dart';
import 'views/not_find_view.dart';
AppRouterDelegate router = AppRouterDelegate(
initial: IRouteConfig(uri: Uri.parse('/color')),
);
initial: IRouteConfig(
uri: Uri.parse('/app/color'),
),
node: appRoute);
class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
/// 核心数据,路由配置数据列表
final List<IRouteConfig> _configs = [];
@@ -21,8 +23,11 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
final IRoutePageBuilder? notFindPageBuilder;
final IRouteNode node;
AppRouterDelegate({
this.notFindPageBuilder,
required this.node,
required IRouteConfig initial,
}) {
_configs.add(initial);
@@ -57,7 +62,8 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
// final List<IRouteConfig> _pathStack = [];
bool get canPop => _configs.where((e) => e.routeStyle==RouteStyle.push).isNotEmpty;
bool get canPop =>
_configs.where((e) => e.routeStyle == RouteStyle.push).isNotEmpty;
final Map<String, Completer<dynamic>> _completerMap = {};
@@ -80,7 +86,7 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
}
}
void _handleChangeStyle(IRouteConfig config){
void _handleChangeStyle(IRouteConfig config) {
switch (config.routeStyle) {
case RouteStyle.push:
if (_configs.contains(config)) {
@@ -89,9 +95,10 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
_configs.add(config);
break;
case RouteStyle.replace:
List<IRouteConfig> liveRoutes = _configs.where((e) => e.keepAlive&&e!=config).toList();
List<IRouteConfig> liveRoutes =
_configs.where((e) => e.keepAlive && e != config).toList();
_configs.clear();
_configs.addAll([...liveRoutes,config]);
_configs.addAll([...liveRoutes, config]);
break;
}
}
@@ -101,7 +108,7 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
bool forResult = false,
Object? extra,
bool keepAlive = false,
bool recordHistory = true,
bool recordHistory = false,
RouteStyle style = RouteStyle.replace,
}) {
return changeRoute(IRouteConfig(
@@ -116,15 +123,29 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
@override
Widget build(BuildContext context) {
return Navigator(
return NavigatorScope(
node: node,
onPopPage: _onPopPage,
pages: _buildPages(context, _configs),
configs: _configs,
notFindPageBuilder: (notFindPageBuilder ?? _defaultNotFindPageBuilder),
);
// return Navigator(
// onPopPage: _onPopPage,
// pages: [
// MaterialPage(child: NavigatorScope(
// cellNode: cellNode,
// onPopPage: _onPopPage,
// configs: _configs,
// notFindPageBuilder: (notFindPageBuilder ?? _defaultNotFindPageBuilder),
// ))
// ],
// );
}
List<Page> _buildPages(BuildContext context, List<IRouteConfig> configs) {
IRouteConfig top = configs.last;
List<IRouteConfig> bottoms = _configs.sublist(0,_configs.length-1).toList();
List<IRouteConfig> bottoms =
_configs.sublist(0, _configs.length - 1).toList();
List<Page> pages = [];
List<Page> topPages = _buildPageByPathFromTree(context, top);
pages = _buildLivePageByPathList(context, bottoms, top, topPages);
@@ -145,8 +166,10 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
pages.addAll(_buildPageByPathFromTree(context, path));
}
}
/// 去除和 curPages 中重复的界面
pages.removeWhere((page) => curPages.map((e) => e.key).contains(page.key));
pages
.removeWhere((page) => curPages.map((e) => e.key).contains(page.key));
}
return pages;
}
@@ -161,14 +184,15 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
config = config.copyWith(path: iroute.path);
Page? page;
if (iroute is NotFindNode) {
page = (notFindPageBuilder ?? _defaultNotFindPageBuilder)(context, config);
page = (notFindPageBuilder ?? _defaultNotFindPageBuilder)(
context, config);
} else {
page = iroute.createPage(context, config);
}
if (page != null) {
result.add(page);
}
if(iroute is CellIRoute){
if (iroute is CellIRoute) {
break;
}
}
@@ -199,7 +223,7 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
_completerMap.remove(path);
}
if (_configs.isNotEmpty) {
if (_configs.length > 1) {
_configs.removeLast();
notifyListeners();
} else {

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:iroute/v9/app/navigation/router/views/navigator_scope.dart';
import 'iroute_config.dart';
@@ -7,6 +8,8 @@ typedef IRoutePageBuilder = Page? Function(
IRouteConfig data,
);
typedef IRouteWidgetBuilder = Widget? Function(
BuildContext context,
IRouteConfig data,
@@ -26,7 +29,15 @@ abstract class IRouteNode {
List<IRouteNode> find(
String input,
) {
return findNodes(this, Uri.parse(input), 0, '/', []);
String prefix = '/';
if (this is CellIRoute) {
input = input.replaceFirst(path, '');
if (path != '/') {
prefix = path + "/";
}
}
return findNodes(this, Uri.parse(input), 0, prefix, []);
}
List<IRouteNode> findNodes(
@@ -43,6 +54,7 @@ abstract class IRouteNode {
String target = parts[deep];
if (node.children.isNotEmpty) {
target = prefix + target;
List<IRouteNode> nodes =
node.children.where((e) => e.path == target).toList();
bool match = nodes.isNotEmpty;
@@ -103,14 +115,43 @@ class NotFindNode extends IRouteNode {
}
}
typedef CellBuilder = Widget Function(
BuildContext context,
IRouteConfig config,
Widget navigator,
);
typedef CellIRoutePageBuilder = Page? Function(
BuildContext context,
IRouteConfig data,
Widget child,
);
class CellIRoute extends IRouteNode {
final CellBuilder cellBuilder;
final CellIRoutePageBuilder? pageBuilder;
class CellIRoute extends IRoute {
const CellIRoute({
required this.cellBuilder,
this.pageBuilder,
required super.path,
super.pageBuilder,
super.children,
super.widget,
required super.children,
});
}
typedef CellBuilder = Widget Function(BuildContext context,IRouteConfig config, CellIRoute cell);
@override
Page? createPage(BuildContext context, IRouteConfig config) {
return null;
}
Page? createCellPage(BuildContext context, IRouteConfig config,
Widget child) {
if (pageBuilder != null) {
return pageBuilder!(context, config, child);
}
print("======CellIRoute#createCellPage${config.pageKey}=================");
return MaterialPage(
child: child,
key: config.pageKey,
);
}
}

View File

@@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:iroute/components/components.dart';
import '../../../pages/login/login.dart';
import '../transition/no_transition_page.dart';
import '../../../pages/sort/views/player/sort_player.dart';
import 'iroute_config.dart';
import '../views/app_navigation.dart';
import 'iroute.dart';
import '../../../pages/color/color_add_page.dart';
import '../../../pages/color/color_detail_page.dart';
@@ -11,37 +15,52 @@ import '../../../pages/settings/settings_page.dart';
import '../../../pages/sort/views/sort_page/sort_page.dart';
import '../../../pages/sort/views/settings/sort_setting.dart';
IRoute rootRoute = const IRoute(
path: 'root',
CellIRoute appRoute = CellIRoute(
cellBuilder: (_, __, navigator) => AppNavigation(
navigator: navigator,
),
path: '/app',
children: [
IRoute(
path: '/color',
IRoute(
path: '/app/color',
widget: ColorPage(),
children: [
IRoute(path: '/color/detail', widgetBuilder: _buildColorDetail),
IRoute(path: '/color/add', widget: ColorAddPage()),
IRoute(path: '/app/color/detail', widgetBuilder: _buildColorDetail),
IRoute(path: '/app/color/add', widget: ColorAddPage()),
],
),
IRoute(path: '/counter', widget: CounterPage()),
const IRoute(path: '/app/counter', widget: CounterPage()),
CellIRoute(
path: '/sort',
widget: SortPage(),
cellBuilder: (_, __, navigator) => SortNavigation(navigator: navigator),
// pageBuilder: (_,config,child)=> NoTransitionPage(
// child: child,
// key: config.pageKey
// ),
path: '/app/sort',
children: [
IRoute(
path: '/sort/settings',
const IRoute(
path: '/app/sort/settings',
widget: SortSettings(),
),
IRoute(
path: '/sort/player',
const IRoute(
path: '/app/sort/player',
widget: SortPlayer(),
),
],
),
IRoute(path: '/user', widget: UserPage()),
IRoute(path: '/settings', widget: SettingPage()),
const IRoute(path: '/app/user', widget: UserPage()),
const IRoute(path: '/app/settings', widget: SettingPage()),
],
);
IRoute rootRoute = IRoute(path: '/', children: [
appRoute,
const IRoute(
path: '/login',
widget: LoginPage()
)
]);
Widget? _buildColorDetail(BuildContext context, IRouteConfig data) {
final Map<String, String> queryParams = data.uri.queryParameters;
String? selectedColor = queryParams['color'];
@@ -53,3 +72,24 @@ Widget? _buildColorDetail(BuildContext context, IRouteConfig data) {
}
return ColorDetailPage(color: color);
}
Map<String, String> kRouteLabelMap = {
'/app': '',
'/app/color': '颜色板',
'/app/color/add': '添加颜色',
'/app/color/detail': '颜色详情',
'/app/counter': '计数器',
'/app/sort': '可视化排序算法',
'/app/sort/player': '演示',
'/app/sort/settings': '排序配置',
'/app/user': '我的',
'/app/settings': '系统设置',
};
const List<MenuMeta> deskNavBarMenus = [
MenuMeta(label: '颜色板', icon: Icons.color_lens_outlined, path: '/app/color'),
MenuMeta(label: '计数器', icon: Icons.add_chart, path: '/app/counter'),
MenuMeta(label: '排序', icon: Icons.sort, path: '/app/sort/player'),
MenuMeta(label: '我的', icon: Icons.person, path: '/app/user'),
MenuMeta(label: '设置', icon: Icons.settings, path: '/app/settings'),
];

View File

@@ -0,0 +1,109 @@
import 'package:flutter/material.dart';
import '../iroute.dart';
import '../iroute_config.dart';
class NavigatorScope extends StatefulWidget {
final IRouteNode node;
final PopPageCallback onPopPage;
final List<IRouteConfig> configs;
final IRoutePageBuilder notFindPageBuilder;
const NavigatorScope({
super.key,
required this.node,
required this.onPopPage,
required this.configs,
required this.notFindPageBuilder,
});
@override
State<NavigatorScope> createState() => _NavigatorScopeState();
}
class _NavigatorScopeState extends State<NavigatorScope> {
@override
Widget build(BuildContext context) {
Widget content = Navigator(
onPopPage: widget.onPopPage,
pages: _buildPages(context, widget.configs),
);
if(widget.node is CellIRoute){
content = (widget.node as CellIRoute).cellBuilder(context,widget.configs.last,content);
}
return HeroControllerScope(
controller: MaterialApp.createMaterialHeroController(),
child: content,
);
}
List<Page> _buildPages(BuildContext context, List<IRouteConfig> configs) {
IRouteConfig top = configs.last;
List<IRouteConfig> bottoms =
configs.sublist(0, configs.length - 1).toList();
List<Page> pages = [];
List<Page> topPages = _buildPageByPathFromTree(context, top);
pages = _buildLivePageByPathList(context, bottoms, top, topPages);
pages.addAll(topPages);
return pages;
}
List<Page> _buildLivePageByPathList(
BuildContext context,
List<IRouteConfig> paths,
IRouteConfig curConfig,
List<Page> curPages,
) {
List<Page> pages = [];
if (paths.isNotEmpty) {
for (IRouteConfig path in paths) {
if (path != curConfig) {
pages.addAll(_buildPageByPathFromTree(context, path));
}
}
/// 去除和 curPages 中重复的界面
pages.removeWhere((page) => curPages.map((e) => e.key).contains(page.key));
}
return pages;
}
List<Page> _buildPageByPathFromTree(
BuildContext context, IRouteConfig config) {
List<Page> result = [];
List<IRouteNode> iRoutes = widget.node.find(config.path);
if (iRoutes.isNotEmpty) {
for (int i = 0; i < iRoutes.length; i++) {
IRouteNode iroute = iRoutes[i];
IRouteConfig fixConfig = config;
if(iroute.path!=config.uri.path){
fixConfig = IRouteConfig(uri: Uri.parse(iroute.path));
}
Page? page;
if (iroute is NotFindNode) {
page = widget.notFindPageBuilder(context, config);
} else if (iroute is CellIRoute) {
Widget scope = NavigatorScope(
node: iroute,
onPopPage: widget.onPopPage,
configs: widget.configs,
notFindPageBuilder: widget.notFindPageBuilder,
);
page = iroute.createCellPage(context, fixConfig, scope);
} else {
page = iroute.createPage(context, fixConfig);
}
if (page != null) {
result.add(page);
}
if (iroute is CellIRoute) {
break;
}
}
}
return result;
}
}

View File

@@ -3,11 +3,32 @@ import '../router/app_router_delegate.dart';
import 'app_navigation_rail.dart';
import 'app_top_bar/app_top_bar.dart';
class AppNavigation extends StatelessWidget {
const AppNavigation({super.key});
class AppNavigation extends StatefulWidget {
final Widget navigator;
const AppNavigation({super.key,required this.navigator});
@override
State<AppNavigation> createState() => _AppNavigationState();
}
class _AppNavigationState extends State<AppNavigation> {
@override
void initState() {
print('======_AppNavigationState#initState==============');
super.initState();
}
@override
void dispose() {
print('======_AppNavigationState#dispose==============');
super.dispose();
}
@override
Widget build(BuildContext context) {
print('=======AppNavigation#build===============');
double px1 = 1/View.of(context).devicePixelRatio;
return Scaffold(
body: Row(
@@ -19,10 +40,7 @@ class AppNavigation extends StatelessWidget {
const AppTopBar(),
Divider(height: px1,),
Expanded(
child: Router(
routerDelegate: router,
backButtonDispatcher: RootBackButtonDispatcher(),
),
child: widget.navigator,
),
],
),

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:iroute/components/components.dart';
import '../router/app_router_delegate.dart';
import '../router/iroute_config.dart';
import '../router/routes.dart';
class AppNavigationRail extends StatefulWidget {
const AppNavigationRail({super.key});
@@ -11,13 +12,6 @@ class AppNavigationRail extends StatefulWidget {
}
class _AppNavigationRailState extends State<AppNavigationRail> {
final List<MenuMeta> deskNavBarMenus = const [
MenuMeta(label: '颜色板', icon: Icons.color_lens_outlined, path: '/color'),
MenuMeta(label: '计数器', icon: Icons.add_chart, path: '/counter'),
MenuMeta(label: '排序', icon: Icons.sort, path: '/sort'),
MenuMeta(label: '我的', icon: Icons.person, path: '/user'),
MenuMeta(label: '设置', icon: Icons.settings, path: '/settings'),
];
@override
void initState() {
@@ -54,14 +48,14 @@ class _AppNavigationRailState extends State<AppNavigationRail> {
);
}
RegExp _segReg = RegExp(r'/\w+');
RegExp _segReg = RegExp(r'/app/\w+');
int? get activeIndex {
String path = router.path;
RegExpMatch? match = _segReg.firstMatch(path);
if (match == null) return null;
String? target = match.group(0);
int index = deskNavBarMenus.indexWhere((menu) => menu.path == target);
int index = deskNavBarMenus.indexWhere((menu) => menu.path!.contains(target??''));
if (index == -1) return null;
return index;
}

View File

@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:iroute/components/components.dart';
import '../../router/app_router_delegate.dart';
import '../../router/routes.dart';
import '../../router/views/route_back_indicator.dart';
import 'app_router_editor.dart';
import 'history_view_icon.dart';
@@ -54,16 +55,6 @@ class RouterIndicator extends StatefulWidget {
State<RouterIndicator> createState() => _RouterIndicatorState();
}
Map<String, String> kRouteLabelMap = {
'/color': '颜色板',
'/color/add': '添加颜色',
'/color/detail': '颜色详情',
'/counter': '计数器',
'/sort': '可视化排序算法',
'/sort/settings': '排序配置',
'/user': '我的',
'/settings': '系统设置',
};
class _RouterIndicatorState extends State<RouterIndicator> {
@override
@@ -107,7 +98,9 @@ class _RouterIndicatorState extends State<RouterIndicator> {
for (String segment in uri.pathSegments) {
to += '/$segment';
String label = kRouteLabelMap[to] ?? '未知路由';
result.add(BreadcrumbItem(to: to, label: label, active: to == distPath));
if(label.isNotEmpty){
result.add(BreadcrumbItem(to: to, label: label, active: to == distPath));
}
}
return result;
}

View File

@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:iroute/components/components.dart';
import '../../router/app_router_delegate.dart';
import '../../router/iroute.dart';
import '../../router/routes.dart';
import '../../router/iroute_config.dart';
import 'app_top_bar.dart';