Flutter 学习记录-Route

Tips: Native 页面目前以iOS(OC)代码为例进行讲解

一:Flutter Route学习

先解决处理native flutter route统一问题Flutter Route 先不做详细介绍

二:接入Flutter Route到应用中
1.解决什么问题?

Native/Flutter Route 统一处理问题。所有的页面跳转都基于原生应用(iOS/Android)进行处理,混编情况下统一处理页面管理。

2.存在什么问题?
  • 1.Native唤醒Flutter有且只存在唯一的一个入口(iOS FlutterViewController)
    1.1 Native 跳转到Flutter确切页面必须经过mian页面,再次进行Flutter Route跳转操作
    1.2 需要实例化多个Flutter App,Flutter 页面之间无法进行通信操作,属于不同的isLoate区间。
3.怎么解决
  • 1.保证APP启动后只有一个Flutter APP 启动
    1.1 Flutter使用单例模式启动,保证只有唯一一个Flutter APP在运行
+ (TuoFlutterViewController *)flutterVC{
    static dispatch_once_t onceToken;
    static TuoFlutterViewController *sxFlutterVC;
    if(sxFlutterVC)
        return sxFlutterVC;
    dispatch_once(&onceToken, ^{
        sxFlutterVC = [[TuoFlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
    });
    return sxFlutterVC;
}
  • 2.原生页面封装 容器Flutter 页面(wrapper),承载Flutter页面
    2.1 处理页面跳转时 flutter view remve add 等操作,在页面出现时进行页面的add操作,页面即将消失的时候进行页面的remove操作
    2.2 remove add 页面空白过渡场景处理,在页面进行转场时对flutter进行remove add 必然会出现闪动的问题,因此在 wrapper 在进行操作时保存flutter view的快照,截屏处理,贴在最上方做到页面不会有异样
    2.3 通知flutter route 进行页面跳转,容器页面确定某个flutter 页面后,通知flutter route 进行push操作,wrapper 销毁退出时,通知flutter route 进行pop操作,做到两边统一
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if(self.viewWillAppearBlock){
        self.viewWillAppearBlock();
        self.viewWillAppearBlock = nil;
    }
    if(!self.lastSnapshot){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self addChildFlutterVC];
        });
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self addChildFlutterVC];
    if(self.curFlutterRouteName.length){
        [[FltPluginRouterPlugin shareInstance].methodChannel invokeMethod:@"popToRouteNamed" arguments:self.curFlutterRouteName];
    }
    [[TuoFlutterViewWrapperController flutterVC].view setUserInteractionEnabled:TRUE];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSArray *curStackAry = [self flutterWapperVcs];
    NSInteger idx = [curStackAry indexOfObject:self];
    if(idx != NSNotFound && idx != curStackAry.count-1){
        [self saveSnapshot];
    }
    [[TuoFlutterViewWrapperController flutterVC].view setUserInteractionEnabled:FALSE];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSArray *ary = [self flutterWapperVcs];
    if(!ary.count){
        [[FltPluginRouterPlugin shareInstance].methodChannel invokeMethod:@"popToRoot" arguments:nil];
    }
    
    NSArray *curStackAry = [self flutterWapperVcs];
    NSInteger idx = [curStackAry indexOfObject:self];
    if(idx == NSNotFound){
        [[FltPluginRouterPlugin shareInstance].methodChannel invokeMethod:@"popRouteNamed" arguments:self.lastFlutterRouteName];
    }
}
  • 3.Native Flutter 通信处理两边Route同步
- (TuoFlutterViewWrapperController *)flutterVCWithParams:(NSDictionary *)params
{
    static BOOL sIsFirstPush = true;
    FlutterMethodChannel *channel = [FltPluginRouterPlugin shareInstance].methodChannel;
    if (sIsFirstPush) {
        [FltPluginRouterPlugin shareInstance].mainEntryParams = params;
        sIsFirstPush = false;
    }
    TuoFlutterViewWrapperController *controller = [[TuoFlutterViewWrapperController alloc] initWithParams:params];
    controller.viewWillAppearBlock = ^{
        if(sIsFirstPush){
            return ;
        }
        [channel invokeMethod:@"openURLFromFlutter" arguments:params result:^(id  _Nullable result) {
            
        }];
    };
    _controller = controller;
    [_wrappers addObject:controller];
    return controller;
}

结构图:

三:demo地址

你可能感兴趣的:(Flutter 学习记录-Route)