From 8a95ed441758bf210d359cb206ff98a10f9672da Mon Sep 17 00:00:00 2001 From: toly <1981462002@qq.com> Date: Mon, 23 Oct 2023 20:01:59 +0800 Subject: [PATCH] v5 --- lib/main.dart | 2 +- .../router/app_router_delegate.dart | 28 +------ .../navigation/views/app_navigation_rail.dart | 6 +- lib/v4/pages/color/color_add_page.dart | 3 - lib/v4/pages/color/color_detail_page.dart | 9 --- lib/v4/pages/color/color_page.dart | 7 +- .../router/app_router_delegate.dart | 78 +++++++++---------- .../navigation/views/app_navigation_rail.dart | 3 +- .../navigation/views/app_router_editor.dart | 64 +++++++++++++++ lib/v5/app/navigation/views/app_top_bar.dart | 34 +++++--- lib/v5/app/unit_app.dart | 3 +- lib/v5/pages/color/color_page.dart | 8 +- lib/v5/pages/empty/empty_page.dart | 6 +- 13 files changed, 139 insertions(+), 112 deletions(-) create mode 100644 lib/v5/app/navigation/views/app_router_editor.dart diff --git a/lib/main.dart b/lib/main.dart index 5dfc9c5..95ccbe5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:window_manager/window_manager.dart'; -import 'v4/app.dart'; +import 'v5/app.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); diff --git a/lib/v4/app/navigation/router/app_router_delegate.dart b/lib/v4/app/navigation/router/app_router_delegate.dart index d40009f..71f3820 100644 --- a/lib/v4/app/navigation/router/app_router_delegate.dart +++ b/lib/v4/app/navigation/router/app_router_delegate.dart @@ -38,16 +38,7 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { Completer? completer; - final List _alivePaths = []; - final Map> _alivePageMap = {}; - - void setPathKeepLive(String value){ - _alivePageMap[value] = _buildPageByPath(value); - path = value; - } - final Map _pathExtraMap = {}; - final List _livePages = []; void setPathForData(String value,dynamic data){ _pathExtraMap[value] = data; @@ -69,29 +60,15 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { @override Widget build(BuildContext context) { - List pages = []; - if(_alivePageMap.containsKey(path)){ - pages = _alivePageMap[path]!; - }else{ - for (var element in _alivePageMap.values) { - pages.addAll(element); - } - pages.addAll(_buildPageByPath(path)); - } - return Navigator( onPopPage: _onPopPage, - pages: pages.toSet().toList(), + pages: _buildPageByPath(path), ); } List _buildPageByPath(String path) { Widget? child; if(path.startsWith('/color')){ - // child = Navigator( - // pages: , - // onPopPage: _onPopPage, - // ); return buildColorPages(path); } @@ -125,10 +102,9 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { if(segment =='detail'){ final Map queryParams = uri.queryParameters; String? selectedColor = queryParams['color']; - if (selectedColor != null) { Color color = Color(int.parse(selectedColor, radix: 16)); - result.add( FadeTransitionPage( + result.add(FadeTransitionPage( key: const ValueKey('/color/detail'), child:ColorDetailPage(color: color), )); diff --git a/lib/v4/app/navigation/views/app_navigation_rail.dart b/lib/v4/app/navigation/views/app_navigation_rail.dart index 1ab9714..f1aaa61 100644 --- a/lib/v4/app/navigation/views/app_navigation_rail.dart +++ b/lib/v4/app/navigation/views/app_navigation_rail.dart @@ -52,11 +52,7 @@ class _AppNavigationRailState extends State { } void _onDestinationSelected(int index) { - if(index==1){ - router.setPathKeepLive(kDestinationsPaths[index]); - }else{ - router.path = kDestinationsPaths[index]; - } + router.path = kDestinationsPaths[index]; } void _onRouterChange() { diff --git a/lib/v4/pages/color/color_add_page.dart b/lib/v4/pages/color/color_add_page.dart index c6ca0cd..48e6dc6 100644 --- a/lib/v4/pages/color/color_add_page.dart +++ b/lib/v4/pages/color/color_add_page.dart @@ -89,9 +89,6 @@ class _ColorAddPageState extends State { ); } - void _selectColor() { - Navigator.of(context).pop(_color); - } void changeColor(Color value) { _color = value; diff --git a/lib/v4/pages/color/color_detail_page.dart b/lib/v4/pages/color/color_detail_page.dart index 17fcd17..7dfed86 100644 --- a/lib/v4/pages/color/color_detail_page.dart +++ b/lib/v4/pages/color/color_detail_page.dart @@ -15,15 +15,6 @@ class ColorDetailPage extends StatelessWidget { ); String text = '# ${color.value.toRadixString(16)}'; return Scaffold( - // appBar: AppBar( - // systemOverlayStyle: SystemUiOverlayStyle( - // statusBarColor: Colors.transparent, - // statusBarIconBrightness: Brightness.light - // ), - // iconTheme: IconThemeData(color: Colors.white), - // titleTextStyle:TextStyle(color: Colors.white,fontSize: 18) , - // backgroundColor: color, - // title: Text('颜色界面',),), body: Container( alignment: Alignment.center, color: color, diff --git a/lib/v4/pages/color/color_page.dart b/lib/v4/pages/color/color_page.dart index a15d581..e7b88da 100644 --- a/lib/v4/pages/color/color_page.dart +++ b/lib/v4/pages/color/color_page.dart @@ -36,10 +36,9 @@ class _ColorPageState extends State { } void _selectColor(Color color){ - String value = color.value.toRadixString(16); - router.path = '/color/detail?color=$value'; - // router.setPathForData('/color/detail',color); - // router.setPathKeepLive('/color/detail?color=$value'); + // String value = color.value.toRadixString(16); + // router.path = '/color/detail?color=$value'; + router.setPathForData('/color/detail',color); } diff --git a/lib/v5/app/navigation/router/app_router_delegate.dart b/lib/v5/app/navigation/router/app_router_delegate.dart index 12b8b88..3e51873 100644 --- a/lib/v5/app/navigation/router/app_router_delegate.dart +++ b/lib/v5/app/navigation/router/app_router_delegate.dart @@ -38,30 +38,28 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { Completer? completer; - final List _alivePaths = []; - final Map _alivePageMap = {}; + final Map> _alivePageMap = {}; void setPathKeepLive(String value){ - if(!_alivePaths.contains(value)){ - _alivePaths.add(value); - } + _alivePageMap[value] = _buildPageByPath(value); path = value; } final Map _pathExtraMap = {}; - final List _livePages = []; - void setPathForData(String value,dynamic data){ - _pathExtraMap[value] = data; - path = value; + FutureOr changePath(String value,{bool forResult=false,Object? extra}){ + if(forResult){ + _completerMap[value] = Completer(); + } + if(extra!=null){ + _pathExtraMap[value] = extra; + } + + if(forResult){ + return _completerMap[value]!.future; + } } - Future changePathForResult(String value) async{ - Completer completer = Completer(); - _completerMap[value] = completer; - path = value; - return completer.future; - } set path(String value) { if (_path == value) return; @@ -71,22 +69,16 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { @override Widget build(BuildContext context) { - List pages = _buildPageByPath(path); - - if(_alivePaths.contains(path)){ - _alivePageMap[path] = pages.last; + List pages = []; + if(_alivePageMap.containsKey(path)){ + pages = _alivePageMap[path]!; + }else{ + for (var element in _alivePageMap.values) { + pages.addAll(element); + } + pages.addAll(_buildPageByPath(path)); } - pages.insertAll(0,_alivePageMap.values); - - // if(!_livePaths.contains(path)){ - // if(_livePaths.isNotEmpty){ - // pages.addAll(_buildPageByPath(_livePaths.first)); - // } - // } - - // pages.addAll(); - return Navigator( onPopPage: _onPopPage, pages: pages.toSet().toList(), @@ -94,11 +86,11 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { } List _buildPageByPath(String path) { + Widget? child; if(path.startsWith('/color')){ return buildColorPages(path); } - Widget? child; if (path == kDestinationsPaths[1]) { child = const CounterPage(); } @@ -127,24 +119,24 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { )); } if(segment =='detail'){ - // final Map queryParams = uri.queryParameters; - // String? selectedColor = queryParams['color']; - // - // if (selectedColor != null) { - // Color color = Color(int.parse(selectedColor, radix: 16)); - // result.add( FadeTransitionPage( - // key: const ValueKey('/color/detail'), - // child:ColorDetailPage(color: color), - // )); - // } + final Map queryParams = uri.queryParameters; + String? selectedColor = queryParams['color']; - Color? selectedColor = _pathExtraMap[path]; if (selectedColor != null) { + Color color = Color(int.parse(selectedColor, radix: 16)); result.add( FadeTransitionPage( key: const ValueKey('/color/detail'), - child:ColorDetailPage(color: selectedColor), + child:ColorDetailPage(color: color), )); - _pathExtraMap.remove(path); + }else{ + Color? selectedColor = _pathExtraMap[path]; + if (selectedColor != null) { + result.add( FadeTransitionPage( + key: const ValueKey('/color/detail'), + child:ColorDetailPage(color: selectedColor), + )); + _pathExtraMap.remove(path); + } } } if(segment == 'add'){ diff --git a/lib/v5/app/navigation/views/app_navigation_rail.dart b/lib/v5/app/navigation/views/app_navigation_rail.dart index c75fc00..1ab9714 100644 --- a/lib/v5/app/navigation/views/app_navigation_rail.dart +++ b/lib/v5/app/navigation/views/app_navigation_rail.dart @@ -54,8 +54,9 @@ class _AppNavigationRailState extends State { void _onDestinationSelected(int index) { if(index==1){ router.setPathKeepLive(kDestinationsPaths[index]); + }else{ + router.path = kDestinationsPaths[index]; } - router.path = kDestinationsPaths[index]; } void _onRouterChange() { diff --git a/lib/v5/app/navigation/views/app_router_editor.dart b/lib/v5/app/navigation/views/app_router_editor.dart new file mode 100644 index 0000000..10d5701 --- /dev/null +++ b/lib/v5/app/navigation/views/app_router_editor.dart @@ -0,0 +1,64 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:iroute/components/toly_ui/button/hover_icon_button.dart'; +import '../router/app_router_delegate.dart'; + +class AppRouterEditor extends StatefulWidget { + final ValueChanged? onSubmit; + const AppRouterEditor({super.key, this.onSubmit}); + + @override + State createState() => _AppRouterEditorState(); +} + +class _AppRouterEditorState extends State { + + final TextEditingController _controller = TextEditingController(); + + + @override + void initState() { + super.initState(); + _onRouteChange(); + router.addListener(_onRouteChange); + } + + void _onRouteChange() { + _controller.text=router.path; + } + + @override + void dispose() { + _controller.dispose(); + router.removeListener(_onRouteChange); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Stack( + alignment: Alignment.centerRight, + children: [ + SizedBox( + child: CupertinoTextField( + controller: _controller, + style: TextStyle(fontSize: 14), + padding: EdgeInsets.only(left:12,top: 6,bottom: 6,right: 32), + placeholder: '输入路由地址导航', + onSubmitted: widget.onSubmit, + decoration: BoxDecoration(color: Color(0xffF1F2F3),borderRadius: BorderRadius.circular(6)), + ), + ), + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: HoverIconButton( + icon: Icons.directions_outlined, + defaultColor: Color(0xff68696B), + onPressed:()=>widget.onSubmit?.call(_controller.text), + size: 20 + ), + ) + ], + ); + } +} diff --git a/lib/v5/app/navigation/views/app_top_bar.dart b/lib/v5/app/navigation/views/app_top_bar.dart index 86fc670..1b95110 100644 --- a/lib/v5/app/navigation/views/app_top_bar.dart +++ b/lib/v5/app/navigation/views/app_top_bar.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:iroute/components/components.dart'; import '../router/app_router_delegate.dart'; +import 'app_router_editor.dart'; class AppTopBar extends StatelessWidget { const AppTopBar({super.key}); @@ -11,13 +12,26 @@ class AppTopBar extends StatelessWidget { child: Container( alignment: Alignment.center, height: 46, - child: const Row( + child: Row( children: [ - SizedBox(width: 16), - Expanded(child: Align( - alignment: Alignment.centerLeft, - child: RouterIndicator())), - WindowButtons() + const SizedBox(width: 16), + const RouterIndicator(), + Expanded( + child: Row(children: [ + const Spacer(), + SizedBox( + width: 250, + child: AppRouterEditor( + onSubmit: (path) => router.path = path, + )), + const Padding( + padding: EdgeInsets.symmetric(vertical: 12.0), + child: VerticalDivider( + width: 32, + ), + ) + ])), + const WindowButtons() ], ), ), @@ -41,7 +55,6 @@ Map kRouteLabelMap = { '/settings': '系统设置', }; - class _RouterIndicatorState extends State { @override void initState() { @@ -59,8 +72,8 @@ class _RouterIndicatorState extends State { Widget build(BuildContext context) { return TolyBreadcrumb( items: pathToBreadcrumbItems(router.path), - onTapItem: (item){ - if(item.to!=null){ + onTapItem: (item) { + if (item.to != null) { router.path = item.to!; } }, @@ -84,9 +97,8 @@ class _RouterIndicatorState extends State { for (String segment in uri.pathSegments) { to += '/$segment'; String label = kRouteLabelMap[to] ?? '未知路由'; - result.add(BreadcrumbItem(to: to, label: label,active: to==distPath)); + result.add(BreadcrumbItem(to: to, label: label, active: to == distPath)); } return result; } } - diff --git a/lib/v5/app/unit_app.dart b/lib/v5/app/unit_app.dart index 4dfe1ba..60d28f8 100644 --- a/lib/v5/app/unit_app.dart +++ b/lib/v5/app/unit_app.dart @@ -8,11 +8,10 @@ class UnitApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( - // routerDelegate: router, theme: ThemeData( fontFamily: "宋体", + scaffoldBackgroundColor: Colors.white, appBarTheme: const AppBarTheme( elevation: 0, iconTheme: IconThemeData(color: Colors.black), diff --git a/lib/v5/pages/color/color_page.dart b/lib/v5/pages/color/color_page.dart index 15fc6fa..6764129 100644 --- a/lib/v5/pages/color/color_page.dart +++ b/lib/v5/pages/color/color_page.dart @@ -36,15 +36,15 @@ class _ColorPageState extends State { } void _selectColor(Color color){ - // String value = color.value.toRadixString(16); - // router.path = '/color/detail?color=$value'; - router.setPathForData('/color/detail',color); + String value = color.value.toRadixString(16); + router.path = '/color/detail?color=$value'; + // router.setPathForData('/color/detail',color); // router.setPathKeepLive('/color/detail?color=$value'); } void _toAddPage() async { - Color? color = await router.changePathForResult('/color/add'); + Color? color = await router.changePath('/color/add',forResult: true); if (color != null) { setState(() { _colors.add(color); diff --git a/lib/v5/pages/empty/empty_page.dart b/lib/v5/pages/empty/empty_page.dart index e9064a8..b05f56f 100644 --- a/lib/v5/pages/empty/empty_page.dart +++ b/lib/v5/pages/empty/empty_page.dart @@ -6,9 +6,9 @@ class EmptyPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: Text('界面走丢了'), - ), + // appBar: AppBar( + // title: Text('界面走丢了'), + // ), body: Scaffold( body: Center( child: Wrap(