This commit is contained in:
toly
2023-11-13 16:15:49 +08:00
parent 5396712cf9
commit 8de38299e2
57 changed files with 3039 additions and 156 deletions

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

@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
class NotFindPage extends StatelessWidget {
const NotFindPage({super.key});
@override
Widget build(BuildContext context) {
return const Material(
child: Center(
child: Wrap(
spacing: 16,
crossAxisAlignment: WrapCrossAlignment.center,
direction: Axis.vertical,
children: [
Icon(Icons.nearby_error,size: 64, color: Colors.redAccent),
Text(
'404 Page Not Find',
style: TextStyle(fontSize: 24, color: Colors.grey),
),
],
),
),
);
}
}

View File

@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
import '../app_router_delegate.dart';
class RouteBackIndicator extends StatefulWidget {
const RouteBackIndicator({super.key});
@override
State<RouteBackIndicator> createState() => _RouteBackIndicatorState();
}
class _RouteBackIndicatorState extends State<RouteBackIndicator> {
@override
void initState() {
super.initState();
router.addListener(_onChange);
}
@override
void dispose() {
router.removeListener(_onChange);
super.dispose();
}
@override
Widget build(BuildContext context) {
if(router.canPop){
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: router.backStack,
child: Container(
width: 26,
height: 26,
margin: EdgeInsets.only(right: 8),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xffE3E5E7),
borderRadius: BorderRadius.circular(6)
),
child: Icon(Icons.arrow_back_ios_new,size: 14,)),
),
);
}
return SizedBox();
}
void _onChange() {
setState(() {
});
}
}