getx_router_observer.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:get/get_navigation/src/router_report.dart';
  4. import '../my_router_history_manager.dart';
  5. final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
  6. /// 手动让getx感知原生路由 (固定代码无需修改)
  7. class GetXRouterObserver extends NavigatorObserver {
  8. @override
  9. void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
  10. RouterReportManager.reportCurrentRoute(route);
  11. MyRouterHistoryManager().putRouterByName(route.settings.name);
  12. if (!kReleaseMode){
  13. print('添加之后-当前的My路由表:${MyRouterHistoryManager().routeNames.toString()}') ;
  14. }
  15. }
  16. @override
  17. void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) async {
  18. RouterReportManager.reportRouteDispose(route);
  19. MyRouterHistoryManager().removeRouterByName(route.settings.name);
  20. if (!kReleaseMode){
  21. print('Pop之后-当前的My路由表:${MyRouterHistoryManager().routeNames.toString()}');
  22. }
  23. }
  24. @override
  25. void didRemove(Route route, Route? previousRoute) {
  26. MyRouterHistoryManager().removeRouterByName(route.settings.name);
  27. if (!kReleaseMode){
  28. print('Remove之后-当前的My路由表:${MyRouterHistoryManager().routeNames.toString()}');
  29. }
  30. }
  31. @override
  32. void didReplace({Route? newRoute, Route? oldRoute}) {
  33. MyRouterHistoryManager().putRouterByName(newRoute?.settings.name);
  34. MyRouterHistoryManager().removeRouterByName(oldRoute?.settings.name);
  35. if (!kReleaseMode){
  36. print('Replace之后-当前的My路由表:${MyRouterHistoryManager().routeNames.toString()}');
  37. }
  38. }
  39. }