在pubspec.yaml中根据flutter sdk版本引入对应的flutter_boost版本。
官网
dependencies:
flutter_boost: ^1.12.13+2
1 初始化
FlutterBoost.init(postPush: _onRoutePushed)
2 注册页面
FlutterBoost.singleton.registerPageBuilders(<String, PageBuilder>{
MAIN_PAGE: (String pageName, Map<dynamic, dynamic> params, String _) =>
FlutterMainPage(),
FIRST_PAGE: (String pageName, Map<dynamic, dynamic> params, String _) =>
FlutterFirstPage(params: params),
});
3 监听页面状态
FlutterBoost.singleton.addBoostNavigatorObserver(BoostNavigatorObserver());
4 总体
main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'page/first_page.dart';
import 'page/main_page.dart';
import 'router.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
//注册页面
FlutterBoost.singleton.registerPageBuilders(<String, PageBuilder>{
MAIN_PAGE: (String pageName, Map<dynamic, dynamic> params, String _) =>
FlutterMainPage(),
FIRST_PAGE: (String pageName, Map<dynamic, dynamic> params, String _) =>
FlutterFirstPage(params: params),
});
//监听页面状态
FlutterBoost.singleton.addBoostNavigatorObserver(BoostNavigatorObserver());
}
//监听页面跳转情况
void _onRoutePushed(
String pageName,
String uniqueId,
Map<dynamic, dynamic> params,
Route<dynamic> route,
Future<dynamic> _,
) {}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "boost demo",
//初始化
builder: FlutterBoost.init(postPush: _onRoutePushed),
home: FlutterMainPage(),
);
}
}
class BoostNavigatorObserver extends NavigatorObserver {
@override
void didPush(Route route, Route previousRoute) {
super.didPush(route, previousRoute);
}
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
}
@override
void didRemove(Route route, Route previousRoute) {
super.didRemove(route, previousRoute);
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
}
}
router.dart
//flutter
const String MAIN_PAGE = "flutter://flutterMainPage";
const String FIRST_PAGE = "flutter://flutterFirstPage";
//native
const String NATIVE_PAGE = "native://main/firstPage";
1 初始化
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);
2 监听跳转
INativeRouter router = new INativeRouter() {
@Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
String assembleUrl = Utils.assembleUrl(url, urlParams);
PageRouter.openPageByUrl(context, assembleUrl, urlParams,requestCode);
}
};
3 监听状态
FlutterBoost.BoostLifecycleListener boostLifecycleListener = new FlutterBoost.BoostLifecycleListener() {
@Override
public void beforeCreateEngine() {
}
@Override
public void onEngineCreated() {
}
@Override
public void onPluginsRegistered() {
}
@Override
public void onEngineDestroy() {
}
};
4 总体
MyApplication.java
package com.example.flutterapp3;
import android.content.Context;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.flutterapp3.router.PageRouter;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.Platform;
import com.idlefish.flutterboost.Utils;
import com.idlefish.flutterboost.interfaces.INativeRouter;
import java.util.Map;
import io.flutter.app.FlutterApplication;
import io.flutter.embedding.android.FlutterView;
public class MyApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
//初始化
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);
ARouter.init(this);
}
//监听跳转
INativeRouter router = new INativeRouter() {
@Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
String assembleUrl = Utils.assembleUrl(url, urlParams);
PageRouter.openPageByUrl(context, assembleUrl, urlParams,requestCode);
}
};
//监听状态
FlutterBoost.BoostLifecycleListener boostLifecycleListener = new FlutterBoost.BoostLifecycleListener() {
@Override
public void beforeCreateEngine() {
}
@Override
public void onEngineCreated() {
}
@Override
public void onPluginsRegistered() {
}
@Override
public void onEngineDestroy() {
}
};
}
PageRouter.java
package com.example.flutterapp3.router;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.idlefish.flutterboost.containers.BoostFlutterActivity;
import com.idlefish.flutterboost.interfaces.IFlutterViewContainer;
import java.io.Serializable;
import java.util.Map;
public class PageRouter {
public static final String NATIVR_PARAM_KEY = "params";
//---protocol---
public static final String PROTOCOL_NATIVE = "native";
public static final String PROTOCOL_FLUTTER = "flutter";
//---- pages----
//flutter
public static final String FLUTTER_MAIN_PAGE = "flutter://flutterMainPage";
public static final String FLUTTER_FIRST_PAGE = "flutter://flutterFirstPage";
//native
public static final String NATIVE_MAIN_FIRST_PAGE = "/main/firstPage";
public static final String NATIVE_MAIN_SECOND_PAGE = "/main/secondPage";
public static boolean openPageByUrl(Context context, String url, Map<String, Object> params, int requestCode) {
//获取域名
String path = url.split("\\?")[0];
//获取协议
String protocol = url.split("://")[0];
if (TextUtils.equals(PROTOCOL_FLUTTER, protocol)) {
Intent intent = BoostFlutterActivity.withNewEngine().url(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);
}
return true;
} else if (TextUtils.equals(PROTOCOL_NATIVE, protocol)) {
String nativePath = getBoostToRouter(path);
if (TextUtils.isEmpty(nativePath)) {
return false;
}
Bundle bundle = new Bundle();
if (params != null) {
bundle.putSerializable(NATIVR_PARAM_KEY, (Serializable) params);
}
if (context instanceof Activity) {
Activity activity = (Activity) context;
getArouter(nativePath).with(bundle).navigation(activity, requestCode);
} else {
gotoActivity(nativePath, bundle);
}
return true;
}
return false;
}
/**
* Arouter跳转
*
* @param path
* @return
*/
public static Postcard getArouter(String path) {
Postcard postcard = ARouter.getInstance()
.build(path);
postcard.setName("");
return postcard;
}
/**
* Arouter跳转
*/
public static void gotoActivity(String path) {
getArouter(path).navigation();
}
/**
* Arouter跳转
*/
public static void gotoActivity(String path, Bundle bundle) {
Postcard postcard = getArouter(path)
.with(bundle);
postcard.navigation();
}
/**
* 设置native返回intent
*
* @param map
* @return
*/
public static Intent setNativeBackResult(Map<String, Object> map) {
Intent intent = new Intent();
intent.putExtra(IFlutterViewContainer.RESULT_KEY, (Serializable) map);
return intent;
}
/**
* 获取返回的数据
*
* @param intent
* @return
*/
public static Map<String, Object> getNativeBackResult(Intent intent) {
Map<String, Object> result = null;
if (intent != null) {
Serializable rlt = intent.getSerializableExtra(IFlutterViewContainer.RESULT_KEY);
if (rlt instanceof Map) {
result = (Map<String, Object>) rlt;
}
}
return result;
}
/**
* arouter地址转换成fluter-boost地址
*
* @param nativePath
* @return
*/
public static String getArouterToBoost(String nativePath) {
return PageRouter.PROTOCOL_NATIVE + ":/" + nativePath;
}
/**
* fluter-boost地址转换成Arouter地址
* @param path
* @return
*/
public static String getBoostToRouter(String path) {
String nativePath = null;
if (!TextUtils.isEmpty(path)) {
String[] aar = path.split("://");
if (aar.length > 1) {
nativePath = aar[1];
nativePath = "/" + nativePath;
}
}
return nativePath;
}
}
注册flutter页面查看初始化部分。
页面1:
跳转+获取回参
FlutterBoost.singleton.open(FIRST_PAGE,
//传参
urlParams: <String, dynamic>{
"param1": "111"
}).then((Map<dynamic, dynamic> value) {
//获取回参
setValue(value);
});
页面2:
获取参数
final Map<dynamic, dynamic> params;
FlutterFirstPage({this.params});
返回参数
final BoostContainerSettings settings =
BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,
result: <String, dynamic>{'result': 'data from fist page'});
总体
main_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import '../router.dart';
class FlutterMainPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => FlutterMainPageState();
}
class FlutterMainPageState extends State<FlutterMainPage> {
Map<dynamic, dynamic> value;
void setValue(Map<dynamic, dynamic> newValue) {
setState(() {
this.value = newValue;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MainPage"),
),
body: Column(
children: <Widget>[
Text("返回内容" + (value != null ? value.toString() : "")),
RaisedButton(
child: Text("跳转flutter;有传参;接收返回数据"),
onPressed: () {
FlutterBoost.singleton.open(FIRST_PAGE,
//传参
urlParams: <String, dynamic>{
"param1": "111"
}).then((Map<dynamic, dynamic> value) {
//获取回参
setValue(value);
});
},
),
],
),
);
}
}
first_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_boost/container/boost_container.dart';
import 'package:flutter_boost/flutter_boost.dart';
class FlutterFirstPage extends StatelessWidget {
final Map<dynamic, dynamic> params;
FlutterFirstPage({this.params});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("FirstPage"),
),
body: Column(
children: <Widget>[
Text("入参:" + params.toString()),
RaisedButton(
child: Text("返回参数"),
onPressed: () {
//关闭并返回参数
final BoostContainerSettings settings =
BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,
result: <String, dynamic>{'result': 'data from fist page'});
},
),
],
),
);
}
}
页面1:
跳转+参数返回
FlutterBoost.singleton.open(NATIVE_PAGE,
urlParams: <String, dynamic>{
"param1": "333"
}).then((Map<dynamic, dynamic> value) {
setValue(value);
});
页面2:
获取参数
private void parseIntent() {
Intent intent = getIntent();
if (intent == null) {
return;
}
params = (Map<String, Object>) intent.getSerializableExtra(PageRouter.NATIVR_PARAM_KEY);
}
返回参数
Map<String, Object> map = new HashMap<>();
map.put("result", "FirstActivity result");
setResult(RESULT_OK, PageRouter.setNativeBackResult(map));
finish();
总体:
main_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import '../router.dart';
class FlutterMainPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => FlutterMainPageState();
}
class FlutterMainPageState extends State<FlutterMainPage> {
Map<dynamic, dynamic> value;
void setValue(Map<dynamic, dynamic> newValue) {
setState(() {
this.value = newValue;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MainPage"),
),
body: Column(
children: <Widget>[
Text("返回内容" + (value != null ? value.toString() : "")),
RaisedButton(
child: Text("跳转native;有传参;接收返回数据"),
onPressed: () {
FlutterBoost.singleton.open(NATIVE_PAGE,
urlParams: <String, dynamic>{
"param1": "333"
}).then((Map<dynamic, dynamic> value) {
setValue(value);
});
},
)
],
),
);
}
}
FirstActivity.java
package com.example.flutterapp3.page;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.flutterapp3.R;
import com.example.flutterapp3.router.PageRouter;
import java.util.HashMap;
import java.util.Map;
@Route(path = PageRouter.NATIVE_MAIN_FIRST_PAGE)
public class FirstActivity extends Activity {
private TextView tvParams;
private Button buttonBack;
private Map<String, Object> params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
parseIntent();
tvParams = findViewById(R.id.tvParams);
buttonBack = findViewById(R.id.buttonBack);
if (params != null) {
tvParams.setText("入参:" + params.toString());
}
buttonBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> map = new HashMap<>();
map.put("result", "FirstActivity result");
setResult(RESULT_OK, PageRouter.setNativeBackResult(map));
finish();
}
});
}
private void parseIntent() {
Intent intent = getIntent();
if (intent == null) {
return;
}
params = (Map<String, Object>) intent.getSerializableExtra(PageRouter.NATIVR_PARAM_KEY);
}
}
页面1:
带参数跳转
Map<String, Object> params = new HashMap<>();
params.put("param1", "FirstActivity parames");
PageRouter.openPageByUrl(FirstActivity.this, PageRouter.FLUTTER_FIRST_PAGE,
params, 3);
获取回参
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
params = PageRouter.getNativeBackResult(data);
if (params != null) {
tvParams.setText("返回参数:" + params.toString());
}
}
页面2:
获取参数
final Map<dynamic, dynamic> params;
FlutterFirstPage({this.params});
返回参数:
//关闭并返回参数
final BoostContainerSettings settings =
BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,
result: <String, dynamic>{'result': 'data from fist page'});
总体:
FistActivity.java
package com.example.flutterapp3.page;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.flutterapp3.R;
import com.example.flutterapp3.router.PageRouter;
import java.util.HashMap;
import java.util.Map;
@Route(path = PageRouter.NATIVE_MAIN_FIRST_PAGE)
public class FirstActivity extends Activity {
private TextView tvParams;
private Button buttonBack2;
private Map<String, Object> params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tvParams = findViewById(R.id.tvParams);
buttonBack2 = findViewById(R.id.buttonBack2);
buttonBack2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> params = new HashMap<>();
params.put("param1", "FirstActivity parames");
PageRouter.openPageByUrl(FirstActivity.this, PageRouter.FLUTTER_FIRST_PAGE,
params, 3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
params = PageRouter.getNativeBackResult(data);
if (params != null) {
tvParams.setText("返回参数:" + params.toString());
}
}
}
fist_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_boost/container/boost_container.dart';
import 'package:flutter_boost/flutter_boost.dart';
class FlutterFirstPage extends StatelessWidget {
final Map<dynamic, dynamic> params;
FlutterFirstPage({this.params});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("FirstPage"),
),
body: Column(
children: <Widget>[
Text("入参:" + params.toString()),
RaisedButton(
child: Text("返回参数"),
onPressed: () {
//关闭并返回参数
final BoostContainerSettings settings =
BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,
result: <String, dynamic>{'result': 'data from fist page'});
},
),
],
),
);
}
}
页面1:
跳转
Map<String, Object> params = new HashMap<>();
params.put("param1", "FirstActivity parames");
PageRouter.openPageByUrl(FirstActivity.this,
PageRouter.getArouterToBoost(PageRouter.NATIVE_MAIN_SECOND_PAGE),
params, 3);
返回参数:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
params = PageRouter.getNativeBackResult(data);
if (params != null) {
tvParams.setText("返回参数:" + params.toString());
}
}
页面2:
接收参数
private void parseIntent() {
Intent intent = getIntent();
if (intent == null) {
return;
}
params = (Map<String, Object>) intent.getSerializableExtra(PageRouter.NATIVR_PARAM_KEY);
}
返回参数
Map<String, Object> map = new HashMap<>();
map.put("result", "SecondActivity result");
setResult(RESULT_OK, PageRouter.setNativeBackResult(map));
finish();
总体:
FistActivity.java
package com.example.flutterapp3.page;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.flutterapp3.R;
import com.example.flutterapp3.router.PageRouter;
import java.util.HashMap;
import java.util.Map;
@Route(path = PageRouter.NATIVE_MAIN_FIRST_PAGE)
public class FirstActivity extends Activity {
private TextView tvParams;
private Button buttonBack3;
private Map<String, Object> params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tvParams = findViewById(R.id.tvParams);
buttonBack3 = findViewById(R.id.buttonBack3);
buttonBack3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> params = new HashMap<>();
params.put("param1", "FirstActivity parames");
PageRouter.openPageByUrl(FirstActivity.this,
PageRouter.getArouterToBoost(PageRouter.NATIVE_MAIN_SECOND_PAGE),
params, 3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
params = PageRouter.getNativeBackResult(data);
if (params != null) {
tvParams.setText("返回参数:" + params.toString());
}
}
}
SecondActivity.java
package com.example.flutterapp3.page;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.flutterapp3.R;
import com.example.flutterapp3.router.PageRouter;
import java.util.HashMap;
import java.util.Map;
@Route(path = PageRouter.NATIVE_MAIN_SECOND_PAGE)
public class SecondActivity extends Activity {
TextView textView;
Button button;
private Map<String, Object> params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
parseIntent();
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
if (params != null) {
textView.setText("入参:" + params.toString());
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> map = new HashMap<>();
map.put("result", "SecondActivity result");
setResult(RESULT_OK, PageRouter.setNativeBackResult(map));
finish();
}
});
}
private void parseIntent() {
Intent intent = getIntent();
if (intent == null) {
return;
}
params = (Map<String, Object>) intent.getSerializableExtra(PageRouter.NATIVR_PARAM_KEY);
}
}
flutter_boost_demo