Flutter和Native交互:Fluter Boost(Da
2020-12-09 本文已影响0人
扫地僧练级
1. 准备
*集成Flutter(请查看前面的篇章)
2. 集成
2.1 pubspec.yaml中
flutter_boost:
git:
url: 'https://github.com/alibaba/flutter_boost.git'
ref: '1.17.1'
2.1. main.dart中 根widget中:
@override
void initState() {
// TODO: implement initState
super.initState();
// 第三方FlutterBoost路由管理:register page widget builders,the key is pageName
FlutterBoost.singleton.registerPageBuilders({
'test': (String pageName, Map<String, dynamic> params, String _) => WyTest(),
//可以在native层通过 getContainerParams 来传递参数
'setting': (String pageName, Map<String, dynamic> params, String _) {
print("setting params:$params");
var _title = params['title'];
return SettingPage(title: _title);
},
});
FlutterBoost.singleton.addBoostNavigatorObserver(TestBoostNavigatorObserver());
}
class TestBoostNavigatorObserver extends NavigatorObserver {
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
print('flutterboost#didPush');
}
@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
print('flutterboost#didPop');
}
@override
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
print('flutterboost#didRemove');
}
@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
print('flutterboost#didReplace');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
),
// 第三方FlutterBoost路由主要代码
builder: FlutterBoost.init(postPush: _onRoutePushed),
home: .......
);
在点击的地方:
onPressed: (){
/*返回和打开新页面,统一用第三方FlutterBoost*/
// 返回到native界面,给native传值
Map<String, dynamic> result = {
"routeName": "setting",
};
try {
// 这里的 flutterCallBack 是我们在页面上 类似 key 值的东西
FlutterBoost.singleton.channel.sendEvent("flutterCallBack", result);
} catch (e) {
print(e);
}
// 这里的 dismiss 是我们返回的时候需要监控的地方在 PlatformRouterImp 里面
FlutterBoost.singleton.closeByContext(context);
//FlutterBoost.singleton.close('setting',result: result);
/* 以下是没有和Native交互的返回代码 有系统的也有第三方FlutterBoost;需要保留 防止内存泄漏*/
// 系统Navigator返回--保留 '':这个是要返回给上一个页面的数据
Navigator.of(context).pop('');
},