1.当我们使用flutter开发的时候 页面里面只有 initState 与dispose方法.
initState 只有进入页面的时候调用,并且在二级页面返回的时候 是不会有调用的
dispose 当页面销毁的时候 才会调用,你进入二级页面是不会调用的
综上分析者两个方法没有办法友盟页面统计分享上报
2.我们可以使用 NavigatorObserver来解决
首先新建一个 继承与NavigatorObserver的类 实现NavigatorObserver 的方法就可以了
import 'package:flutter/cupertino.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';
import 'package:zhengda_health/app/support/user_info_manager.dart';
class CustomNavigatorObserver extends NavigatorObserver{
factory CustomNavigatorObserver() => _getInstance();
static CustomNavigatorObserver get instance => _getInstance();
static CustomNavigatorObserver _instance;
BuildContext curContext;
//tabbar 里面的几个主页面,返回的是 根据索引进行上报
List <String> routesNames = ['home','community','health','classroom','mine'];
CustomNavigatorObserver._internal() {}
static CustomNavigatorObserver _getInstance() {
if (_instance == null) {
_instance = new CustomNavigatorObserver._internal();
}
return _instance;
}
String topRouteName = "";
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
notifyHide(previousRoute);
notifyShow(route);
}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
notifyHide(route);
notifyShow(previousRoute);
}
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
notifyHide(route);
notifyShow(previousRoute);
}
/// The [Navigator] replaced `oldRoute` with `newRoute`.
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
notifyHide(oldRoute);
notifyShow(newRoute);
}
void notifyShow(Route<dynamic> route) {
if(route !=null){
if(route.settings !=null){
if(route.settings.name == '/'){
//这里是当前tabbar 的索引,因为从一级页面进入二级页面返回主页面的时候 route.settings.name = '/'
String name = routesNames[ UserInfoManager.instance.tabbarSelIndex];
UmengCommonSdk.onPageStart(name);
}else{
UmengCommonSdk.onPageStart(route.settings.name);
}
}
}
}
void notifyHide(Route<dynamic> route) {
if(route !=null){
if(route.settings !=null){
if(route.settings.name == '/'){
String name = routesNames[ UserInfoManager.instance.tabbarSelIndex];
UmengCommonSdk.onPageEnd(name);
}else{
UmengCommonSdk.onPageEnd(route.settings.name);
}
}
}
}
}
上面代码我这里通过route.settings. name 拿到当前路由的名字进行上报
最后我们需要再 MaterialApp里面的navigatorObservers添加监听就可以了
return MaterialApp(
navigatorObservers: [
CustomNavigatorObserver.instance
],
)
这时候 会有一个问题点击tabbar几个主页是没有统计的 ,我们也需要再tabbar的点击方法来进行处理
void _umClick(index){
List <String> routesNames = ['home','community','health','classroom','mine'];
UmengCommonSdk.onPageEnd(routesNames[_odlIndex]);
UmengCommonSdk.onPageStart(routesNames[index]);
}