1.解决方法 监听路由的push和pop 通知原生控制器flutterVC
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [MyNavigator()],
debugShowCheckedModeBanner:false,
theme:ThemeData(
// platform: TargetPlatform.iOS,
),
routes: {
'addBank': (context) =>AddBankCard(),
"myBankCard": (context) =>MyBankCard(),
'MessageDetail': (context) =>MessageDetail(),
},
home: _rootPage(pageName),
);
}
///导航栈的变化监听
class MyNavigatorextends NavigatorObserver {
final MethodChannel_routeChannel =const MethodChannel('routeChannel');
@override
void didPop(Route route, Route? previousRoute) {
super.didPop(route, previousRoute);
String? previousName ='';
if (previousRoute ==null) {
previousName ='null';
}else {
previousName = previousRoute.settings.name;
}
print('pop------' + previousName!);
///如果 previousName = '/' 说明是根控制器
if(previousName! =='/'){
//告诉原生当前是根控制器
Map map = {"code":"OK"};
_routeChannel.invokeMapMethod('exit',map);
}else{
//告诉原生当前是非根控制器
Map map = {"code":"NO"};
_routeChannel.invokeMapMethod('exit',map);
}
}
@override
void didPush(Route route, Route? previousRoute) {
super.didPush(route, previousRoute);
String? previousName ='';
if (previousRoute ==null) {
previousName ='null';
}else {
previousName = previousRoute.settings.name;
}
print('push------' + previousName!);
Map map = {"code":"NO"};
_routeChannel.invokeMapMethod('exit',map);
}
@override
void didStopUserGesture() {
super.didStopUserGesture();
}
@override
void didStartUserGesture(Route route, Route? previousRoute) {
// TODO: implement didStartUserGesture
super.didStartUserGesture(route, previousRoute);
}
@override
void didReplace({Route? newRoute, Route? oldRoute}) {
// TODO: implement didReplace
super.didReplace(newRoute: newRoute,oldRoute: oldRoute);
}
@override
void didRemove(Route route, Route? previousRoute) {
// TODO: implement didRemove
super.didRemove(route, previousRoute);
}
}
网友评论