原生應(yīng)用集成flutter

一創(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;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容