这里写自定义目录标题
- 1、新建routeMonitor.dart文件:
- 2、修改main.dart:
- 3.监听页面显隐的页面
1、新建routeMonitor.dart文件:
import 'package:flutter/material.dart';class AppRouteObserver {//这是实际上的路由监听器static final RouteObserver<ModalRoute<void>> _routeObserver =RouteObserver<ModalRoute<void>>();//这是个单例static final AppRouteObserver _appRouteObserver =AppRouteObserver._internal();AppRouteObserver._internal();//通过单例的get方法轻松获取路由监听器RouteObserver<ModalRoute<void>> get routeObserver {return _routeObserver;}factory AppRouteObserver() {return _appRouteObserver;}}
2、修改main.dart:
MaterialApp(navigatorObservers: [AppRouteObserver().routeObserver],);
3.监听页面显隐的页面
class _XXX extends State<XXX> with RouteAware {...void didChangeDependencies() {// TODO: implement didChangeDependenciessuper.didChangeDependencies();/// 路由订阅AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);}void dispose() {// TODO: implement dispose/// 取消路由订阅AppRouteObserver().routeObserver.unsubscribe(this);super.dispose();}void didPush() {print('跳转该页面而显示');}void didPop() {print('当前页面被pop而隐藏');}void didPopNext() {print('上一个页面关闭而显示');}void didPushNext() {print('跳转下一个页面而隐藏');}...}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END