my_navigator_observer.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. import 'package:shared/utils/log_utils.dart';
  3. class MyNavigatorObserver extends NavigatorObserver {
  4. @override
  5. void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
  6. super.didPush(route, previousRoute);
  7. Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
  8. ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
  9. 'parameters: ${getParameters(route)}}');
  10. }
  11. @override
  12. void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
  13. super.didPop(route, previousRoute);
  14. Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
  15. ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
  16. 'parameters: ${getParameters(route)}}');
  17. }
  18. @override
  19. void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
  20. super.didRemove(route, previousRoute);
  21. Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
  22. ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
  23. 'parameters: ${getParameters(route)}}');
  24. }
  25. @override
  26. void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
  27. super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
  28. Log.d('NavigationRecord{route: ${getRouteName(newRoute)}, action: didPush ,'
  29. ' previousRoute: ${getPreviousRouteName(oldRoute)}, '
  30. 'parameters: ${getParameters(newRoute)}}');
  31. }
  32. /// 获取路由参数
  33. static Map<String, dynamic> getParameters(Route<dynamic>? route) {
  34. final params = <String, dynamic>{};
  35. if (route?.settings.arguments != null) {
  36. params['arguments'] = route!.settings.arguments;
  37. }
  38. if (route?.settings.name != null) {
  39. params['name'] = route!.settings.name;
  40. }
  41. return params;
  42. }
  43. /// 获取路由名称
  44. static String getRouteName(Route<dynamic>? route) {
  45. return route?.settings.name ?? 'Unknown route';
  46. }
  47. /// 获取前一个路由名称
  48. static String getPreviousRouteName(Route<dynamic>? previousRoute) {
  49. return previousRoute?.settings.name ?? 'Unknown previous route';
  50. }
  51. }