老孟导读:很多时候我们需要监听路由堆栈的变化,这样可以自定义路由堆栈、方便分析异常日志等。
监听路由堆栈的变化使用 RouteObserver ,首先在 MaterialApp 组件中添加 navigatorObservers
:
voID main() { runApp(MyApp());}RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();class MyApp extends StatelessWidget { @overrIDe Widget build(BuildContext context) { return MaterialApp( ... navigatorObservers: [routeObserver],home: HomePage(),); }}
监听页面设置如下:
class ARouteObserverDemo extends StatefulWidget { @overrIDe _RouteObserverDemoState createState() => _RouteObserverDemoState();}class _RouteObserverDemoState extends State<ARouteObserverDemo> with RouteAware { @overrIDe voID dIDChangeDependencIEs() { super.dIDChangeDependencIEs(); routeObserver.subscribe(this,ModalRoute.of(context)); } @overrIDe Widget build(BuildContext context) { return Scaffold( body: Container( alignment: Alignment.center,child: Raisedbutton( child: Text('A RouteObserver'),onpressed: () { Navigator.of(context).pushnamed('/brouteObserver'); },),); } @overrIDe voID dispose() { super.dispose(); routeObserver.unsubscribe(this); } @overrIDe voID dIDPush() { final route = ModalRoute.of(context).settings.name; print('A-dIDPush route: $route'); } @overrIDe voID dIDPopNext() { final route = ModalRoute.of(context).settings.name; print('A-dIDPopNext route: $route'); } @overrIDe voID dIDPushNext() { final route = ModalRoute.of(context).settings.name; print('A-dIDPushNext route: $route'); } @overrIDe voID dIDPop() { final route = ModalRoute.of(context).settings.name; print('A-dIDPop route: $route'); }}
其中 dIDPush、dIDPushNext、dIDPopNext、dIDPop 为路由堆栈变化的回调。
从 A 页面跳转到 ARouteObserverDemo 页面,日志输出如下:
Flutter: A-dIDPush route: /ARouteObserver
进入此页面只调用了 dIDPush。
从 ARouteObserverDemo 页面跳转到 brouteObserverDemo 页面(同 ARouteObserverDemo 页面,设置了监听),日志输出如下:
Flutter: A-dIDPushNext route: /ARouteObserverFlutter: B-dIDPush route: /brouteObserver
先调用了 ARouteObserverDemo 页面的 dIDPushNext,然后调用了 brouteObserverDemo 页面的 dIDPush。
从 brouteObserverDemo 页面执行 pop 返回 ARouteObserverDemo 页面,日志输出如下:
Flutter: A-dIDPopNext route: /ARouteObserverFlutter: B-dIDPop route: /brouteObserver
先调用了 ARouteObserverDemo 页面的 dIDPopNext,然后调用了 brouteObserverDemo 页面的 dIDPop。
上面的案例仅仅是页面级别的路由堆栈变化,如果想知道整个应用程序路由堆栈变化如何处理?
一种方法是写一个监听路由堆栈的基类,所有页面继承此基类。此方法对源代码的侵入性非常高。
还有一种方法是自定义 RouteObserver,继承RouteObserver并重写其中的方法:
class MyRouteObserver<R extends Route<dynamic>> extends RouteObserver<R> { @overrIDe voID dIDPush(Route route,Route prevIoUsRoute) { super.dIDPush(route,prevIoUsRoute); print('dIDPush route: $route,prevIoUsRoute:$prevIoUsRoute'); } @overrIDe voID dIDPop(Route route,Route prevIoUsRoute) { super.dIDPop(route,prevIoUsRoute); print('dIDPop route: $route,prevIoUsRoute:$prevIoUsRoute'); } @overrIDe voID dIDReplace({Route newRoute,Route oldRoute}) { super.dIDReplace(newRoute: newRoute,oldRoute: oldRoute); print('dIDReplace newRoute: $newRoute,oldRoute:$oldRoute'); } @overrIDe voID dIDRemove(Route route,Route prevIoUsRoute) { super.dIDRemove(route,prevIoUsRoute); print('dIDRemove route: $route,prevIoUsRoute:$prevIoUsRoute'); } @overrIDe voID dIDStartUserGesture(Route route,Route prevIoUsRoute) { super.dIDStartUserGesture(route,prevIoUsRoute); print('dIDStartUserGesture route: $route,prevIoUsRoute:$prevIoUsRoute'); } @overrIDe voID dIDStopUserGesture() { super.dIDStopUserGesture(); print('dIDStopUserGesture'); }}
使用:
voID main() { runApp(MyApp());}MyRouteObserver<PageRoute> myRouteObserver = MyRouteObserver<PageRoute>();class MyApp extends StatelessWidget { @overrIDe Widget build(BuildContext context) { return MaterialApp( Title: 'Flutter Demo',navigatorObservers: [myRouteObserver],initialRoute: '/A',home: APage(),); }}
此时从 A 页面 跳转到 B 页面,日志输出如下:
Flutter: dIDPush route: MaterialPageRoute<dynamic>(RouteSettings("/B",来自A),animation: AnimationController#6d429(▶ 0.000; for MaterialPageRoute<dynamic>(/B))),prevIoUsRoute:MaterialPageRoute<dynamic>(RouteSettings("/A",null),animation: AnimationController#e60f7(⏭ 1.000; paused; for MaterialPageRoute<dynamic>(/A)))
交流交流老孟Flutter博客(330个控件用法+实战入门系列文章):http://laomengit.com
欢迎加入Flutter交流群(微信:laomengit)、关注公众号【老孟Flutter】:
以上是内存溢出为你收集整理的全局监听路由堆栈变化全部内容,希望文章能够帮你解决全局监听路由堆栈变化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)