v7
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'iroute.dart';
|
||||
import 'route_history.dart';
|
||||
|
||||
const List<String> kDestinationsPaths = [
|
||||
'/color',
|
||||
'/counter',
|
||||
'/user',
|
||||
'/settings',
|
||||
];
|
||||
import 'iroute_config.dart';
|
||||
import 'route_history_manager.dart';
|
||||
import 'routes.dart';
|
||||
import 'views/not_find_view.dart';
|
||||
|
||||
AppRouterDelegate router = AppRouterDelegate();
|
||||
|
||||
@@ -18,120 +13,82 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
|
||||
|
||||
String get path => _path;
|
||||
|
||||
AppRouterDelegate() {
|
||||
// keepAlivePath.add('/color');
|
||||
_histories.add(RouteHistory(path));
|
||||
final IRoutePageBuilder? notFindPageBuilder;
|
||||
|
||||
AppRouterDelegate({this.notFindPageBuilder}) {
|
||||
_historyManager.recode(IRouteConfig(uri: Uri.parse(path)));
|
||||
}
|
||||
|
||||
final List<RouteHistory> _histories = [];
|
||||
final List<RouteHistory> _backHistories = [];
|
||||
Page _defaultNotFindPageBuilder(_, __) => const MaterialPage(
|
||||
child: Material(child: NotFindPage()),
|
||||
);
|
||||
|
||||
List<RouteHistory> get histories => _histories.reversed.toList();
|
||||
final RouteHistoryManager _historyManager = RouteHistoryManager();
|
||||
|
||||
bool get hasHistory => _histories.length > 1;
|
||||
|
||||
bool get hasBackHistory => _backHistories.isNotEmpty;
|
||||
RouteHistoryManager get historyManager => _historyManager;
|
||||
|
||||
/// 历史回退操作
|
||||
/// 将当前顶层移除,并加入 _backHistories 撤销列表
|
||||
/// 并转到前一路径
|
||||
void back() {
|
||||
if (!hasHistory) return;
|
||||
RouteHistory top = _histories.removeLast();
|
||||
_backHistories.add(top);
|
||||
if (_histories.isNotEmpty) {
|
||||
_path = _histories.last.path;
|
||||
if (_histories.last.extra != null) {
|
||||
_pathExtraMap[_path] = _histories.last.extra;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
/// 详见: [RouteHistoryManager.back]
|
||||
void back() => _historyManager.back(changeRoute);
|
||||
|
||||
void toHistory(RouteHistory history) {
|
||||
_path = history.path;
|
||||
if (history.extra != null) {
|
||||
_pathExtraMap[_path] = history.extra;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
/// 撤销回退操作
|
||||
/// 详见: [RouteHistoryManager.revocation]
|
||||
void revocation() => _historyManager.revocation(changeRoute);
|
||||
|
||||
void closeHistory(int index) {
|
||||
_histories.removeAt(index);
|
||||
_historyManager.close(index);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearHistory() {
|
||||
_histories.clear();
|
||||
_historyManager.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 撤销回退操作
|
||||
/// 取出回退列表的最后元素,跳转到该路径
|
||||
void revocation() {
|
||||
RouteHistory target = _backHistories.removeLast();
|
||||
_path = target.path;
|
||||
if (target.extra != null) {
|
||||
_pathExtraMap[_path] = target.extra;
|
||||
}
|
||||
_histories.add(target);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
final Map<String, dynamic> _pathExtraMap = {};
|
||||
|
||||
final List<String> keepAlivePath = [];
|
||||
|
||||
FutureOr<dynamic> changePath(
|
||||
String value, {
|
||||
bool forResult = false,
|
||||
Object? extra,
|
||||
bool keepAlive = false,
|
||||
bool recordHistory = true,
|
||||
}) {
|
||||
FutureOr<dynamic> changeRoute(IRouteConfig config) {
|
||||
String value = config.uri.path;
|
||||
if (_path == value) null;
|
||||
if (forResult) {
|
||||
if (config.forResult) {
|
||||
_completerMap[value] = Completer();
|
||||
}
|
||||
if (keepAlive) {
|
||||
if (config.keepAlive) {
|
||||
if (keepAlivePath.contains(value)) {
|
||||
keepAlivePath.remove(value);
|
||||
}
|
||||
keepAlivePath.add(value);
|
||||
}
|
||||
if (extra != null) {
|
||||
_pathExtraMap[value] = extra;
|
||||
if (config.extra != null) {
|
||||
_pathExtraMap[value] = config.extra;
|
||||
}
|
||||
|
||||
if (recordHistory) {
|
||||
_addPathToHistory(value,extra);
|
||||
if (config.recordHistory) {
|
||||
_historyManager.recode(config);
|
||||
}
|
||||
|
||||
_path = value;
|
||||
notifyListeners();
|
||||
if (forResult) {
|
||||
if (config.forResult) {
|
||||
return _completerMap[value]!.future;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void _addPathToHistory(String value, Object? extra) {
|
||||
if (_histories.isNotEmpty && value == _histories.last.path) return;
|
||||
_histories.add(RouteHistory(
|
||||
value,
|
||||
extra: _pathExtraMap[path],
|
||||
FutureOr<dynamic> changePath(
|
||||
String value, {
|
||||
bool forResult = false,
|
||||
Object? extra,
|
||||
bool keepAlive = false,
|
||||
bool recordHistory = true,
|
||||
}) {
|
||||
return changeRoute(IRouteConfig(
|
||||
uri: Uri.parse(value),
|
||||
forResult: forResult,
|
||||
extra: extra,
|
||||
keepAlive: keepAlive,
|
||||
recordHistory: recordHistory,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -156,7 +113,7 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
|
||||
|
||||
/// 去除和 topPages 中重复的界面
|
||||
pages.removeWhere(
|
||||
(element) => topPages.map((e) => e.key).contains(element.key));
|
||||
(element) => topPages.map((e) => e.key).contains(element.key));
|
||||
}
|
||||
|
||||
pages.addAll(topPages);
|
||||
@@ -165,23 +122,26 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
|
||||
|
||||
List<Page> _buildPageByPathFromTree(BuildContext context, String path) {
|
||||
List<Page> result = [];
|
||||
List<IRoute> iRoutes = root.find(path);
|
||||
List<IRouteNode> iRoutes = rootRoute.find(path);
|
||||
if (iRoutes.isNotEmpty) {
|
||||
for (int i = 0; i < iRoutes.length; i++) {
|
||||
IRoute iroute = iRoutes[i];
|
||||
IRouteNode iroute = iRoutes[i];
|
||||
String path = iroute.path;
|
||||
Object? extra = _pathExtraMap[path];
|
||||
bool keepAlive = keepAlivePath.contains(path);
|
||||
bool forResult = _completerMap.containsKey(path);
|
||||
Page? page = iroute.builder?.call(
|
||||
context,
|
||||
IRouteData(
|
||||
uri: Uri.parse(path),
|
||||
extra: extra,
|
||||
keepAlive: keepAlive,
|
||||
forResult: forResult,
|
||||
),
|
||||
IRouteConfig config = IRouteConfig(
|
||||
uri: Uri.parse(path),
|
||||
extra: extra,
|
||||
keepAlive: keepAlive,
|
||||
forResult: forResult,
|
||||
);
|
||||
Page? page;
|
||||
if (iroute is NotFindNode) {
|
||||
page = (notFindPageBuilder ?? _defaultNotFindPageBuilder)(context, config);
|
||||
} else {
|
||||
page = iroute.createPage(context, config);
|
||||
}
|
||||
if (page != null) {
|
||||
result.add(page);
|
||||
}
|
||||
@@ -201,8 +161,7 @@ class AppRouterDelegate extends RouterDelegate<Object> with ChangeNotifier {
|
||||
_completerMap[path]?.complete(result);
|
||||
_completerMap.remove(path);
|
||||
}
|
||||
|
||||
changePath(backPath(path),recordHistory: false);
|
||||
changePath(backPath(path), recordHistory: false);
|
||||
return route.didPop(result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user