navigation_channel.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:flutter/services.dart';
  2. import 'package:go_router/go_router.dart';
  3. /// 平台(iOS/Android)主动下发路由时,通过此 Channel 传递给 Flutter。
  4. ///
  5. /// iOS 侧调用方式:
  6. /// ```objc
  7. /// [engine.navigationChannel invokeMethod:@"pushRouteInformation"
  8. /// arguments:@{@"location": @"/expense/apply"}];
  9. /// ```
  10. ///
  11. /// 或者通过自定义 channel:
  12. /// ```objc
  13. /// FlutterMethodChannel *ch = [FlutterMethodChannel
  14. /// methodChannelWithName:@"com.tboss.oa/navigation"
  15. /// binaryMessenger:engine.binaryMessenger];
  16. /// [ch invokeMethod:@"navigateTo" arguments:@"/expense/apply"];
  17. /// ```
  18. class NavigationChannel {
  19. static const _channel = MethodChannel('com.tboss.oa/navigation');
  20. /// 开始监听平台下发的导航指令。
  21. ///
  22. /// [router] 必须是已初始化的 GoRouter 实例。
  23. static void startListening(GoRouter router) {
  24. _channel.setMethodCallHandler((call) async {
  25. switch (call.method) {
  26. case 'navigateTo':
  27. final route = call.arguments as String?;
  28. if (route != null && route.isNotEmpty) {
  29. router.go(route);
  30. }
  31. break;
  32. case 'pushRoute':
  33. final route = call.arguments as String?;
  34. if (route != null && route.isNotEmpty) {
  35. router.push(route);
  36. }
  37. break;
  38. default:
  39. break;
  40. }
  41. });
  42. }
  43. }