什么是FlutterBoost
? 新一代Flutter-Native混合解決方案状植。 FlutterBoost是一個(gè)Flutter插件稿辙,它可以輕松地為現(xiàn)有原生應(yīng)用程序提供Flutter混合集成方案贩虾。FlutterBoost的理念是將Flutter像Webview那樣來(lái)使用平道。在現(xiàn)有應(yīng)用程序中同時(shí)管理Native頁(yè)面和Flutter頁(yè)面并非易事募强。 FlutterBoost幫你處理頁(yè)面的映射和跳轉(zhuǎn),你只需關(guān)心頁(yè)面的名字和參數(shù)即可(通诚舾#可以是URL)拉鹃。
開(kāi)發(fā)環(huán)境
flutter和flutter boost版本有很多,技術(shù)不斷發(fā)展统锤,以官方最新版本為準(zhǔn)
Flutter 1.12.13+hotfix.5 ? channel stable ? https://github.com/flutter/flutter.git
Framework ? revision 27321ebbad (3 months ago) ? 2019-12-10 18:15:01 -0800
Engine ? revision 2994f7e1e6
Tools ? Dart 2.7.0
AndroidStudio 3.6
flutter-boost版本 task/task_v1.12.13_support_hotfixes毛俏,不支持androidx
引入依賴(lài)和配置flutterboost
FlutterBoost是flutter混合開(kāi)發(fā)時(shí)使用的庫(kù),引入依賴(lài)時(shí)饲窿,你需要新建一個(gè)安卓工程,并把flutter作為一個(gè)安卓工程的子模塊引入焕蹄,安卓flutter混合工程配置請(qǐng)看Flutter系列筆記-9:Android原生Flutter混合編程
flutter_module配置
打開(kāi)flutter_module子工程逾雄,先在 pubspec.yaml 里加入
flutter_boost:
git:
url: 'https://github.com/alibaba/flutter_boost.git'
ref: 'task/task_v1.12.13_support_hotfixes'
完整如下
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flutter_boost:
git:
url: 'https://github.com/alibaba/flutter_boost.git'
ref: 'task/task_v1.12.13_support_hotfixes'
添加完成后,執(zhí)行 Packages get
安卓工程配置
執(zhí)行完后,切換到安卓工程鸦泳,在app/build.gradle里添加依賴(lài)
implementation project(':flutter_boost')
完整如下
dependencies {
implementation project(':flutter')
implementation project(':flutter_boost')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
同步一下工程银锻。依賴(lài)配置成功。
安卓工程初始化FlutterBoost配置
在安卓工程的Application的onCreate方法初始化 flutter boost
public class MyApplication extends Application {
public final static String TAG = "flutterBoostApplication";
@Override
public void onCreate() {
super.onCreate();
INativeRouter router = new INativeRouter() {
@Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
Log.d(TAG,"openContainer url:"+url+"\turlParams:"+urlParams+"\trequestCode:"+requestCode+"\texts:"+exts);
String assembleUrl = Utils.assembleUrl(url, urlParams);
PageRouter.openPageByUrl(context, assembleUrl, urlParams);
}
};
FlutterBoost.BoostLifecycleListener boostLifecycleListener = new FlutterBoost.BoostLifecycleListener() {
@Override
public void beforeCreateEngine() {
Log.d(TAG,"beforeCreateEngine");
}
@Override
public void onEngineCreated() {
Log.d(TAG,"onEngineCreated");
}
@Override
public void onPluginsRegistered() {
Log.d(TAG,"onPluginsRegistered");
}
@Override
public void onEngineDestroy() {
Log.d(TAG,"onEngineDestroy");
}
};
//
// AndroidManifest.xml 中必須要添加 flutterEmbedding 版本設(shè)置
//
// <meta-data android:name="flutterEmbedding"
// android:value="2">
// </meta-data>
// GeneratedPluginRegistrant 會(huì)自動(dòng)生成 新的插件方式
//
//
Platform platform = new FlutterBoost
.ConfigBuilder(this, router)
.isDebug(true)
.whenEngineStart(FlutterBoost.ConfigBuilder.ANY_ACTIVITY_CREATED)
.renderMode(FlutterView.RenderMode.texture)
.lifecycleListener(boostLifecycleListener)
.build();
FlutterBoost.instance().init(platform);
}
}
app/src/main/AndroidManifest.xml里配置com.idlefish.flutterboost.containers.BoostFlutterActivity
<activity
android:name="com.idlefish.flutterboost.containers.BoostFlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:theme="@style/Theme.AppCompat"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/page_loading" />
</activity>
flutter_module里初始化flutterboost配置
主要是
FlutterBoost.singleton.registerPageBuilders方法 注冊(cè)頁(yè)面路由用
FlutterBoost.singleton.addBoostNavigatorObserver(TestBoostNavigatorObserver());路由生命周期回調(diào)
MaterialApp里的 builder: FlutterBoost.init(postPush: _onRoutePushed),
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
FlutterBoost.singleton.registerPageBuilders({
'embeded': (pageName, params, _)=>EmbededFirstRouteWidget(),
'first': (pageName, params, _) => FirstRouteWidget(),
'second': (pageName, params, _) => SecondRouteWidget(),
'tab': (pageName, params, _) => TabRouteWidget(),
'platformView': (pageName, params, _) => PlatformRouteWidget(),
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native層通過(guò) getContainerParams 來(lái)傳遞參數(shù)
'flutterPage': (pageName, params, _) {
print("flutterPage params:$params");
return FlutterRouteWidget(params:params);
},
});
FlutterBoost.singleton.addBoostNavigatorObserver(TestBoostNavigatorObserver());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Boost example',
builder: FlutterBoost.init(postPush: _onRoutePushed),
home: Container(
color:Colors.white
));
}
void _onRoutePushed(
String pageName, String uniqueId, Map params, Route route, Future _) {
}
}
安卓頁(yè)面里打開(kāi)安卓頁(yè)面
使用普通的startActivity即可
context.startActivity(new Intent(context, NativePageActivity.class));
安卓原生頁(yè)面里打開(kāi)flutter頁(yè)面
url為 flutter_module里FlutterBoost.singleton.registerPageBuilders里配置的頁(yè)面路由做鹰,
params 是原生要傳給flutter頁(yè)面的參數(shù)集合
backgroundMode 有兩種opaque(不透明)和transparent(透明)兩種
//打開(kāi)flutter頁(yè)面
Intent intent = BoostFlutterActivity.withNewEngine().url(pageName.get(path)).params(params)
.backgroundMode(BoostFlutterActivity.BackgroundMode.opaque).build(context);
if(context instanceof Activity){
Activity activity=(Activity)context;
activity.startActivityForResult(intent,requestCode);
}else{
context.startActivity(intent);
}
flutter頁(yè)面打開(kāi)安卓原生頁(yè)面
FlutterBoost.singleton
.open("sample://nativePage", urlParams: {
"query": {"aaa": "bbb"}
})
需要指定url和urlParams击纬,
FlutterBoost.singleton.open方法實(shí)際上是對(duì)應(yīng)執(zhí)行安卓代碼的 INativeRouter的openContainer方法,原生根據(jù)參數(shù)按需要使用startActivity打開(kāi)原生頁(yè)面
INativeRouter router = new INativeRouter() {
@Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
}
};
flutter頁(yè)面使用FlutterBoost打開(kāi)flutter頁(yè)面
flutter打開(kāi)flutter頁(yè)面(widget)可以使用flutter里普通的路由管理Navigator.push或Navigator.pushName,也可以用FlutterBoost.singleton.open打開(kāi)一個(gè)flutter頁(yè)面
FlutterBoost.singleton.open("second").then((Map value) {
print(
"call me when page is finished. did recieve second route result $value");
})
flutter頁(yè)面關(guān)閉FlutterBoost.singleton.open打開(kāi)的flutter頁(yè)面,并返回值給前一個(gè)頁(yè)面
BoostContainerSettings settings =BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,result: {"result": "data from second"});
安卓原生頁(yè)面里使用flutterboost的FlutterFragment的使用
flutterboost的FlutterFragment和安卓的Fragment差不多
com.idlefish.flutterboost.containers.FlutterFragment這個(gè)FlutterFragment和flutter官方的io.flutter.embedding.android.FlutterFragment不是同一個(gè)
FlutterFragment mFragment = new FlutterFragment.NewEngineFragmentBuilder().url("flutterFragment").build()
使用NewEngineFragmentBuilder創(chuàng)建了FlutterFragment之后钾麸,按普通的Fragment使用即可更振。一般還配合SplashScreenProvider使用,以避免白屏饭尝。
flutter頁(yè)面顯示原生控件
暫未整理
項(xiàng)目地址體驗(yàn)地址:
FlutterBoostExample