flutter_module
項目pubspec.yaml
中集成flutter_boost
flutter_boost: ^0.1.54
複製代碼
main.dart
中註冊void initState() { super.initState(); FlutterBoost.singleton.registerPageBuilders({ 'first': (pageName, params, _) => FirstRouteWidget(params), 'second': (pageName, params, _) => SecondRouteWidget(), 'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params: params,), }); } 複製代碼
項目爲
Xcode 11
建立,自定義window
,可看Xcode11ios
flutter
,配置podfile
文件flutter_application_path = '../flutter_module/' load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb') target 'Native-FF' do install_all_flutter_pods(flutter_application_path) 複製代碼
pod install
,成功後以下圖:Appdelgate
配置- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { PlatformRouter * router = [[PlatformRouter alloc] init]; router.navigationController = nav; [FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router onStart:^(FlutterEngine *engine) { }]; } 複製代碼
新建文件PlatformRouter
bash
PlatformRouter.h
代碼:markdown
/** navigationController */
@property (nonatomic,strong) UINavigationController * navigationController;
複製代碼
`PlatformRouter.m`代碼:
複製代碼
-(void)open:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion { if ([url containsString:@"sample://nativePage"]) { SecondViewController * secondVC = [[SecondViewController alloc] init]; secondVC.params = urlParams; [self.navigationController pushViewController:secondVC animated:YES]; }else{ BOOL animated = [exts[@"animated"] boolValue]; FLBFlutterViewContainer * containerVC = [[FLBFlutterViewContainer alloc] init]; [containerVC setName:url params:urlParams]; [self.navigationController pushViewController:containerVC animated:animated]; if (completion) { completion(YES); } } } -(void)present:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion { BOOL animated = [exts[@"animated"] boolValue]; FLBFlutterViewContainer * containerVC = [[FLBFlutterViewContainer alloc] init]; [containerVC setName:url params:urlParams]; [self.navigationController presentViewController:containerVC animated:animated completion:^{ if (completion) { completion(YES); } }]; } -(void)close:(NSString *)uid result:(NSDictionary *)result exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion { if ([uid containsString:@"sample://nativePage"]) { BOOL animated = [exts[@"animated"] boolValue]; NSLog(@"關閉頁面時,傳遞的信息%@",result); [self.navigationController popViewControllerAnimated:animated]; }else{ BOOL animated = [exts[@"animated"] boolValue]; FLBFlutterViewContainer *vc = (id)self.navigationController.presentedViewController; if([vc isKindOfClass:FLBFlutterViewContainer.class] && [vc.uniqueIDString isEqual: uid]){ [vc dismissViewControllerAnimated:animated completion:^{}]; }else{ [self.navigationController popViewControllerAnimated:animated]; } } } 複製代碼
iOS
代碼[FlutterBoostPlugin open:@"first" urlParams:@{@"firstMessage":@"Native open Flutter first page with message"} exts:@{@"animated":@(YES)} onPageFinished:^(NSDictionary *result) { NSLog(@"Opened first page%@",result); } completion:^(BOOL finish) { }]; 複製代碼
Flutter
代碼FlutterBoost.singleton.registerPageBuilders({ 'first': (pageName, params, _) => FirstRouteWidget(params), }); 複製代碼
iOS
代碼[FlutterBoostPlugin close:@"flutterFragment" result:nil exts:nil completion:^(BOOL finish) { }]; 複製代碼
Flutter
代碼FlutterBoost.singleton.registerPageBuilders({ 'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params: params,) }); 複製代碼
Flutter
代碼FlutterBoost.singleton.open("sample://nativePage",urlParams: {"message":"Flutter open Native page!!!"}), 複製代碼
iOS
端在PlatformRouter
中根據路由判斷,執行跳轉-(void)open:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion { if ([url containsString:@"sample://nativePage"]) { SecondViewController * secondVC = [[SecondViewController alloc] init]; secondVC.params = urlParams; [self.navigationController pushViewController:secondVC animated:YES]; } } 複製代碼
flutter
代碼:return FlutterBoost.singleton.close("sample://nativePage",result: params); 複製代碼
iOS
端在PlatformRouter
中根據路由判斷,執行關閉:-(void)close:(NSString *)uid result:(NSDictionary *)result exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion { if ([uid containsString:@"sample://nativePage"]) { BOOL animated = [exts[@"animated"] boolValue]; NSLog(@"關閉頁面時,傳遞的信息%@",result); [self.navigationController popViewControllerAnimated:animated]; } } 複製代碼
flutter
代碼:FlutterBoost.singleton.channel.sendEvent("flutter-native", {"message":"flutter send message to Native!!!"}) 複製代碼
iOS
端監聽方法:#pragma mark - Flutter---調用---native -(void)flutterCallNative { /** 監聽 Flutter 調用原生 */ [FlutterBoostPlugin.sharedInstance addEventListener:^(NSString *name, NSDictionary *arguments) { NSLog(@"\n=====flutter發來消息========\n%@----\n%@",name,arguments); } forName:@"flutter-native"]; } 複製代碼
iOS
發送事件- (IBAction)sendMessageToFlutter:(id)sender { [FlutterBoostPlugin.sharedInstance sendEvent:@"native-flutter" arguments:@{@"flutter":@"native向flutter發送了參數\n"}]; } 複製代碼
Flutter
監聽事件FlutterBoost.singleton.channel.addEventListener("native-flutter",(name,params){ return handleMsg(name,params); }); handleMsg(String name,Map params){ print("$name--原生調用flutter-$params"); } 複製代碼
channel
參數傳遞外,也能夠經過open
和close
,制訂本身的規則,進行非頁面跳轉的方法調用和參數傳遞