import 'package:flutter/material.dart'; import 'package:shared/utils/log_utils.dart'; class MyNavigatorObserver extends NavigatorObserver { @override void didPush(Route route, Route? previousRoute) { super.didPush(route, previousRoute); Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,' ' previousRoute: ${getPreviousRouteName(previousRoute)}, ' 'parameters: ${getParameters(route)}}'); } @override void didPop(Route route, Route? previousRoute) { super.didPop(route, previousRoute); Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,' ' previousRoute: ${getPreviousRouteName(previousRoute)}, ' 'parameters: ${getParameters(route)}}'); } @override void didRemove(Route route, Route? previousRoute) { super.didRemove(route, previousRoute); Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,' ' previousRoute: ${getPreviousRouteName(previousRoute)}, ' 'parameters: ${getParameters(route)}}'); } @override void didReplace({Route? newRoute, Route? oldRoute}) { super.didReplace(newRoute: newRoute, oldRoute: oldRoute); Log.d('NavigationRecord{route: ${getRouteName(newRoute)}, action: didPush ,' ' previousRoute: ${getPreviousRouteName(oldRoute)}, ' 'parameters: ${getParameters(newRoute)}}'); } /// 获取路由参数 static Map getParameters(Route? route) { final params = {}; if (route?.settings.arguments != null) { params['arguments'] = route!.settings.arguments; } if (route?.settings.name != null) { params['name'] = route!.settings.name; } return params; } /// 获取路由名称 static String getRouteName(Route? route) { return route?.settings.name ?? 'Unknown route'; } /// 获取前一个路由名称 static String getPreviousRouteName(Route? previousRoute) { return previousRoute?.settings.name ?? 'Unknown previous route'; } }