v3
This commit is contained in:
185
lib/v2/app/navigation/app_router_delegate.dart
Normal file
185
lib/v2/app/navigation/app_router_delegate.dart
Normal file
@@ -0,0 +1,185 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../pages/color/color_detail_page.dart';
|
||||
import '../../pages/color/color_page.dart';
|
||||
import '../../pages/empty/empty_page.dart';
|
||||
import '../../pages/settings/settings_page.dart';
|
||||
import '../../pages/counter/counter_page.dart';
|
||||
import '../../pages/user/user_page.dart';
|
||||
import '../../transition/fade_transition_page.dart';
|
||||
import '../../pages/color/color_add_page.dart';
|
||||
|
||||
const List<String> kDestinationsPaths = [
|
||||
'/color',
|
||||
'/counter',
|
||||
'/user',
|
||||
'/settings',
|
||||
];
|
||||
|
||||
AppRouterDelegate router = AppRouterDelegate();
|
||||
|
||||
class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
|
||||
String _path = '/color';
|
||||
|
||||
String get path => _path;
|
||||
|
||||
int? get activeIndex {
|
||||
if(path.startsWith('/color')) return 0;
|
||||
if(path.startsWith('/counter')) return 1;
|
||||
if(path.startsWith('/user')) return 2;
|
||||
if(path.startsWith('/settings')) return 3;
|
||||
return null;
|
||||
}
|
||||
|
||||
final Map<String,Completer<dynamic>> _completerMap = {};
|
||||
|
||||
Completer<dynamic>? completer;
|
||||
|
||||
Future<dynamic> changePathForResult(String value) async{
|
||||
Completer<dynamic> completer = Completer();
|
||||
_completerMap[value] = completer;
|
||||
path = value;
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
set path(String value) {
|
||||
if (_path == value) return;
|
||||
_path = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
onPopPage: _onPopPage,
|
||||
pages: _buildPageByPath(path),
|
||||
);
|
||||
}
|
||||
|
||||
List<Page> _buildPageByPath(String path) {
|
||||
if(path.startsWith('/color')){
|
||||
return buildColorPages(path);
|
||||
}
|
||||
|
||||
Widget? child;
|
||||
if (path == kDestinationsPaths[1]) {
|
||||
child = const CounterPage();
|
||||
}
|
||||
if (path == kDestinationsPaths[2]) {
|
||||
child = const UserPage();
|
||||
}
|
||||
if (path == kDestinationsPaths[3]) {
|
||||
child = const SettingPage();
|
||||
}
|
||||
return [
|
||||
FadeTransitionPage(
|
||||
// key: ValueKey(path),
|
||||
child: child ?? const EmptyPage(),
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
List<Page> buildColorPages(String path){
|
||||
List<Page> result = [];
|
||||
Uri uri = Uri.parse(path);
|
||||
for (String segment in uri.pathSegments) {
|
||||
if(segment == 'color'){
|
||||
result.add( const FadeTransitionPage(
|
||||
key: ValueKey('/color'),
|
||||
child:ColorPage(),
|
||||
));
|
||||
}
|
||||
if(segment =='detail'){
|
||||
final Map<String, String> 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),
|
||||
));
|
||||
}
|
||||
}
|
||||
if(segment == 'add'){
|
||||
result.add( const FadeTransitionPage(
|
||||
key: ValueKey('/color/add'),
|
||||
child:ColorAddPage(),
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> popRoute() async {
|
||||
print('=======popRoute=========');
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _onPopPage(Route route, result) {
|
||||
if(_completerMap.containsKey(path)){
|
||||
_completerMap[path]?.complete(result);
|
||||
_completerMap.remove(path);
|
||||
}
|
||||
|
||||
path = backPath(path);
|
||||
return route.didPop(result);
|
||||
}
|
||||
|
||||
String backPath(String path){
|
||||
Uri uri = Uri.parse(path);
|
||||
if(uri.pathSegments.length==1) return path;
|
||||
List<String> parts = List.of(uri.pathSegments)..removeLast();
|
||||
return '/${parts.join('/')}';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(configuration) async {}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// class AppRouterDelegate extends RouterDelegate<String> with ChangeNotifier, PopNavigatorRouterDelegateMixin {
|
||||
//
|
||||
// List<String> _value = ['/'];
|
||||
//
|
||||
//
|
||||
// List<String> get value => _value;
|
||||
//
|
||||
// set value(List<String> value){
|
||||
// _value = value;
|
||||
// notifyListeners();
|
||||
// }
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return Navigator(
|
||||
// onPopPage: _onPopPage,
|
||||
// pages: _value.map((e) => _pageMap[e]!).toList(),
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// final Map<String, Page> _pageMap = const {
|
||||
// '/': MaterialPage(child: HomePage()),
|
||||
// 'a': MaterialPage(child: PageA()),
|
||||
// 'b': MaterialPage(child: PageB()),
|
||||
// 'c': MaterialPage(child: PageC()),
|
||||
// };
|
||||
//
|
||||
// bool _onPopPage(Route route, result) {
|
||||
// _value = List.of(_value)..removeLast();
|
||||
// notifyListeners();
|
||||
// return route.didPop(result);
|
||||
// }
|
||||
//
|
||||
// @override
|
||||
// GlobalKey<NavigatorState>? navigatorKey = GlobalKey<NavigatorState>();
|
||||
//
|
||||
// @override
|
||||
// Future<void> setNewRoutePath(String configuration) async{
|
||||
// }
|
||||
// }
|
||||
37
lib/v2/app/navigation/router/iroute.dart
Normal file
37
lib/v2/app/navigation/router/iroute.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
class IRoute {
|
||||
final String path;
|
||||
final List<IRoute> children;
|
||||
|
||||
const IRoute({required this.path, this.children = const []});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'IRoute{path: $path, children: $children}';
|
||||
}
|
||||
|
||||
List<String> list(){
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
const List<IRoute> kDestinationsIRoutes = [
|
||||
IRoute(
|
||||
path: '/color',
|
||||
children: [
|
||||
IRoute(path: '/color/add'),
|
||||
IRoute(path: '/color/detail'),
|
||||
],
|
||||
),
|
||||
IRoute(
|
||||
path: '/counter',
|
||||
),
|
||||
IRoute(
|
||||
path: '/user',
|
||||
),
|
||||
IRoute(
|
||||
path: '/settings',
|
||||
),
|
||||
];
|
||||
50
lib/v2/app/navigation/views/app_navigation_rail.dart
Normal file
50
lib/v2/app/navigation/views/app_navigation_rail.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../app_router_delegate.dart';
|
||||
|
||||
class AppNavigationRail extends StatefulWidget {
|
||||
const AppNavigationRail({super.key});
|
||||
|
||||
@override
|
||||
State<AppNavigationRail> createState() => _AppNavigationRailState();
|
||||
}
|
||||
|
||||
class _AppNavigationRailState extends State<AppNavigationRail> {
|
||||
final List<NavigationRailDestination> destinations = const [
|
||||
NavigationRailDestination(icon: Icon(Icons.color_lens_outlined), label: Text("颜色板")),
|
||||
NavigationRailDestination(icon: Icon(Icons.add_chart), label: Text("计数器")),
|
||||
NavigationRailDestination(icon: Icon(Icons.person), label: Text("我的")),
|
||||
NavigationRailDestination(icon: Icon(Icons.settings), label: Text("设置")),
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
router.addListener(_onRouterChange);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
router.removeListener(_onRouterChange);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return NavigationRail(
|
||||
labelType: NavigationRailLabelType.all,
|
||||
onDestinationSelected: _onDestinationSelected,
|
||||
destinations: destinations,
|
||||
selectedIndex: router.activeIndex,
|
||||
);
|
||||
}
|
||||
|
||||
void _onDestinationSelected(int index) {
|
||||
router.path = kDestinationsPaths[index];
|
||||
}
|
||||
|
||||
void _onRouterChange() {
|
||||
setState(() {
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
38
lib/v2/app/unit_app.dart
Normal file
38
lib/v2/app/unit_app.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'navigation/app_router_delegate.dart';
|
||||
import 'navigation/views/app_navigation_rail.dart';
|
||||
|
||||
class UnitApp extends StatelessWidget {
|
||||
const UnitApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
appBarTheme: const AppBarTheme(
|
||||
elevation: 0,
|
||||
iconTheme: IconThemeData(color: Colors.black),
|
||||
titleTextStyle: TextStyle(
|
||||
color: Colors.black,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
))),
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Row(
|
||||
children: [
|
||||
const AppNavigationRail(),
|
||||
Expanded(
|
||||
child: Router(
|
||||
routerDelegate: router,
|
||||
backButtonDispatcher: RootBackButtonDispatcher(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user