Flutter-flutter_boost與iOS頁面跳轉和channel參數傳遞

一.Flutter 項目配置

  • 建立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,),
    });
  }
複製代碼

二.iOS項目配置

項目爲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) {
    }];
}
複製代碼
  • 新建文件PlatformRouterbash

    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];
        }
    }
}
複製代碼

三.Flutter 與 Native 頁面間跳轉

1.Native 打開 Flutter 頁面

  • 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),
});
複製代碼

2.Native 關閉 Flutter 頁面

  • iOS代碼
[FlutterBoostPlugin close:@"flutterFragment" result:nil exts:nil completion:^(BOOL finish) {
}];
複製代碼
  • Flutter代碼
FlutterBoost.singleton.registerPageBuilders({
  'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params: params,)
});
複製代碼

3.Flutter 打開 Native 頁面

  • 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];
    }
}
複製代碼

4.Flutter 關閉 Native 頁面

  • 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 與 Native 方法調用和參數傳遞

1.Flutter 調用 Native 方法

  • 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"];
}
複製代碼

2.Native 調用 Flutter

  • 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");
}
複製代碼

3.除了經過channel參數傳遞外,也能夠經過openclose,制訂本身的規則,進行非頁面跳轉的方法調用和參數傳遞

相關文章
相關標籤/搜索