import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:go_router/go_router.dart'; /// 平台(iOS/Android)主动下发路由时,通过此 Channel 传递给 Flutter。 /// /// navigateTo:用 [GoRouter.go] 清空整个导航栈后跳转到目标路由。 /// 配合 Android 端追加的 _ts 时间戳,确保缓存引擎下每次打开 Flutter 页面都重建 State。 /// /// iOS 侧调用方式: /// ```objc /// [engine.navigationChannel invokeMethod:@"pushRouteInformation" /// arguments:@{@"location": @"/expense/apply"}]; /// ``` /// /// 或者通过自定义 channel: /// ```objc /// FlutterMethodChannel *ch = [FlutterMethodChannel /// methodChannelWithName:@"com.tboss.oa/navigation" /// binaryMessenger:engine.binaryMessenger]; /// [ch invokeMethod:@"navigateTo" arguments:@"/expense/apply"]; /// ``` class NavigationChannel { static const _channel = MethodChannel('com.tboss.oa/navigation'); /// 开始监听平台下发的导航指令。 /// /// [router] 必须是已初始化的 GoRouter 实例。 static void startListening(GoRouter router) { _channel.setMethodCallHandler((call) async { switch (call.method) { case 'navigateTo': final route = call.arguments as String?; if (route != null && route.isNotEmpty) { // 在 widget 树重建前先设置状态栏,避免缓存引擎二次打开时 // Android Activity 主题覆盖 Flutter 的状态栏设置 SystemChrome.setSystemUIOverlayStyle( const SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.dark, ), ); router.go(route); } break; case 'pushRoute': final route = call.arguments as String?; if (route != null && route.isNotEmpty) { router.push(route); } break; default: break; } }); } }