一創(chuàng)建Flutter model
原生項目目錄: xxxx/flutter_hybrid/Native項目:
?? cd xxxx/flutter_hybrid/
? flutter create -t module flutter_module
生成flutter_module的宿主工程
.android. — flutter_module的android宿主工程
.ios —flutter_module的iOS宿主工程
lib —flutter_module的Dart部分代碼
二ios 工程配置
1.pod文件添加代碼
flutter_application_path = ‘../flutter_module/’
Eval(File.read(File.join(flutter_aoolication_path,’.ios’,’flutter’,’pod helper.rb’)),binding)
2.pod install
3.關(guān)閉 bitcode
4.添加build phase 構(gòu)建Dart代碼
?? 選中項目,點擊 New Run Script Phase
? “$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh” buiObject-cld
“$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh” embed
5.OC中調(diào)用Flutter module
A:直接使用FlutterViewController的方式
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
FlutterViewzcontroller * flutter = [FlutterViewzcontroller new];
GeneratedPluginRegistrant.register(with:flutter);
[flutter setInitialRoute:@‘route1’];
[self presentViewController:flutter animated:true completion:nil];
B:AppDelegate繼承FlutterAppDelegate
self.flutterEngine = [[FlutterEngine alloc] initWithName:’io.flutter’ project:nil];
[self.flutterEngine runWithEntrypoint:nil];
[generatedPluginRegistrant registerWithRegistry:self.flutterEngine];
Return [super application:application didFinishLaunchingWithOptions:launchOptions];
三安卓工程配置
1.settings.gradle添加如下代碼
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile,’flutter_module/.android/include_flutter.groovy’))
2.app中添加flutter的依賴
dependencies{implementation project(‘:flutter’)}
3.java中調(diào)用Flutter module
方式有兩種:a:使用Flutter.createView API的方式
? ? ? ? ? ? ? ? ? ? ? ? b:使用FlutterFragment的方式
eg: public void onclick(View view){
?? View flutterView = Flutter.createView(
? ? ? MainActivity.this,
? ? ? getLifecycle(),
? ? ? ‘route1’
? ? ? );
?? FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600,600);
?? Layout.leftMargin = 100;
?? Layout.topMargin = 200;
?? addContentView(flutterView,layout);
}
}
eg: public void onclick(View view){
? ? FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
? ? tx.replace(R.id.somecontainer,Flutter.createFragment(‘route1’));
? ? tx.commit();
? }
}
四傳遞參數(shù)
? tx.replace(R.id.somecontainer,Flutter.createFragment(‘{name:’devio’,dataList:[‘a(chǎn)a’,’bb’,’cc’]}’));
Dart中:String initParams = window.defaultRouteName;