flutter组件化调研

news/2024/7/20 20:02:48 标签: flutter, ios, android

# 一、组件化接入方式
跟原生组件化类似,共有两种方式接入:pod和framework
怎么配置flutter环境,flutter官网上讲得很详细了,不在累赘了

## 1.以pod的方式接入
  1.创建一个flutter_module
```
 flutter create -t module flutter_module 
```


额。。。瞬间翻车。。。尴尬得一批。。。囧么办呢
现在用的是zsh命名,需要切换到bash命令
```
source ~/.bash_profile
```


可以看到文件下面的flutter_module文件


然后是创建一个iOS项目。并创建pod文件


修改podfile,添加
```
  flutter_application_path = '../flutter_module'
  load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  install_all_flutter_pods(flutter_application_path)
```


pod 成功了


在ViewController中修改代码
```
#import "ViewController.h"
#import <Flutter/FlutterViewController.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(100, 100, 200, 50)];
    [button setBackgroundColor:[UIColor greenColor]];
    [button setTitle:@"ClickMePushToFlutterVC" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btn_click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)btn_click {
    
//    FlutterViewController *flutterViewController = [[FlutterViewController alloc] init];
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
    if (self.navigationController) {
        [self.navigationController pushViewController:flutterViewController animated:YES];
    } else {
        [self presentViewController:flutterViewController animated:YES completion:nil];
    }
}

@end

```


成了
 

## 2.以framewor的方式接入
同上
创建一个MyFlutterPod仓库
cd 到flutter_module目录下,然后build一下就有对应的文件了
```
 flutter build ios --debug      //编译debug产物
 flutter build ios --release --no-codesign //编译release产物(选择不需要证书)
 flutter build ios 默认是使用证书打release产物
```


把需要的产物copy到MyFlutterPod仓库下,为了方便我新建了一个ios_frameworks


然后在MyFlutterPod仓库,增加一下framework打包代码
```
  s.static_framework = true
  p = Dir::open("ios_frameworks")
  arr = Array.new
  arr.push('ios_frameworks/*.framework')
  s.ios.vendored_frameworks = arr
```
在到iOSProject项目中把MyFlutterPod仓库pod进来
```
pod 'MyFlutterPod', :path => '../MyFlutterPod'
```


跑一下完成
 

# 二、组件化接入
每个语言都只有一个main入口函数,dart也不例外,所以你接入的时候也只能绑定一个flutter_module(即app.framework)
然后通过flutter_module来进行模块拆分,通过路由跳转到对应的模块
初始化flutter有两种方式
## 1.直接用FlutterViewController
```
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
```
不能先加载引擎,初始化的时候,需要一段时间

## 2.使用engine去初始化
```
FlutterEngine *flutterEngine =
    [[FlutterEngine alloc] initWithName:@"my flutter engine"];
[[flutterEngine navigationChannel] invokeMethod:@"setInitialRoute"
                                      arguments:@"/onboarding"];
[flutterEngine run];
 ```

设置路由跳转的时候不起作用 flutterViewController.setInitialRoute("test1")

## 3.初始化两个引擎
这样可以大大提高UI初始化速度,减少用户等待时间,大大提高用户体验
但是随之而来就是,大量的内存消耗,复杂的页面跳转逻辑

# 三、flutter与原生的通信
## 1.BasicMessageChannel:
用于传递字符串和半结构化的信息,持续通信,收到消息后可以回复此次消息,如:Native将遍历到的文件信息陆续传递到Dart,在比如:Flutter将从服务端陆陆续获取到信息交个Native加工,Native处理完返回等;

## 2.MethodChannel:
用于传递方法调用(method invocation)一次性通信:如Flutter调用Native拍照;

## 3.EventChannel: 
用于数据流(event streams)的通信,持续通信,收到消息后无法回复此次消息,通常用于Native向Dart的通信,如:手机电量变化,网络连接变化,陀螺仪,传感器等;

# 四、组件化方案
## 1.接入方式
flutter_module的方式去接入,减少打包成framework的成本

## 2.原生捆绑
 flutter_module是调用flutter组件的中间键,用于与原生进行捆绑

## 3.跳转方案
1.用FlutterViewController去初始化,再进行组建的跳转
2.维护一个路由用于每个组建的跳转

## 4.原生交互
维护一个交互的路由


http://www.niftyadmin.cn/n/994029.html

相关文章

个体工商户核名查询_公司刚注销完成可以注册个体户吗

大家都知道&#xff0c;营业执照是每一个公司老总的“身份证”&#xff0c;但要想获得这一凭证并没那么容易&#xff0c;不单单要准备许多申报材料&#xff0c;还需要为公司选取一个最合适的名称。昨天有个朋友向鑫鑫财务咨询&#xff1a;“我打算在衡水注册个体户&#xff0c;…

提供一个Red Hat Enterprise Linux 6 下载地址

红帽在本周三&#xff08;11月10日&#xff09;发布了其企业级Linux&#xff0c;RHEL 6的正式版红帽官方已经不用RHEL这个简称了&#xff0c;其全称叫做Red Hat Enterprise Linux&#xff09;。“红帽RHEL 6是10年研发和合作的结晶”&#xff0c;下面我给大家提供一个RHEL6 32/…

《Greenplum5.0 最佳实践》 系统参数 (二)

《Greenplum 数据库最佳实践 》 系统参数配置 系统配置 本章主要描述在Greenplum部署之前&#xff0c;系统参数的配置 文件系统 (File System) 推荐使用XFS作为Greenplum默认文件系统, 目前redhat,Centos 7.0 都开始使用XFS作为默认文件系统如果系统不支持 需要使用下面的挂载命…

【小技巧】自定义asp.net mvc的WebFormViewEngine修改默认的目录结构

先看一下我的解决方案的目录结构吧&#xff5e;&#xff5e;&#xff5e; 一&#xff1a;先把Controller程序提取出来 默认的情况是所有的****Controller.cs文件都会放在Web程序集下的一个叫Controllers的文件夹下 这样感觉有点不爽&#xff08;你懂的...&#xff09; 我们决定…

如何使用VSS

这段时间在公司做一个.net项目&#xff0c; 一个项目开发小组&#xff0c;开发环境是vs.net&#xff0c;语言是c#&#xff0c;三个人协同开发。既然是协同开发&#xff0c;就要找一个源代码控制软件来进行开发中的源代码管理和版本控 制。我们首先想到的是clearcase&#xf…

统计学怎么求加权指数_实用扑克统计学常规桌部分

引言你无法消除波动,但是&#xff0c;你可以利用统计数据洞悉波动&#xff0c;或是避免被随机性欺骗。而且你可以做好波动的准备&#xff0c;以使坏运气不会成为一种灾难。——《The Math Of Holdem》Q1&#xff1a;如何确定稳定的赢率区间&#xff1f;Q2&#xff1a;应该打多少…

GetType和typeof的区别

typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符&#xff0c;获得某一类型的 System.Type 对象。 Type t typeof(int); GetType: Gets the Type of the current instance. 方法&#xff0c;获取当前实例的类型。 int i 10;Conso…

setTimeout() 與 clearTimeout() 方法

window是指一個畫面視窗。setTimeout是指script再過某一特定時間後去執行某一敘述&#xff1a; 變數window.setTimeout("Script",千分之一秒)。clearTimeout是指script取消某setTimeout的設定&#xff1a; window.setTimeout(變數)。運用範例如下&#xff1a;<bod…