搜索插件、代码仓与 APP 分类

插件详情v1.1.1

navigation_history_observer

一个 Flutter NavigatorObserver,用于存储已推送路由的历史记录以及已弹出的路由历史记录。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.1万
喜欢数
70
Pub 分数
160

安装插件

flutter pub add navigation_history_observer

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

navigation_history_observer 是一个 Flutter NavigatorObserver,用于跟踪和存储导航过程中的推送和弹出路由历史。它为开发者提供了完整的导航流程访问权限,支持自定义返回按钮行为、分析或调试导航模式等功能。该观察器可在所有支持的平台上实时捕获路由转换,包括移动设备、网页和桌面端。它与 Flutter 的路由系统无缝集成,仅需通过 pub add 命令即可完成最小化配置。

适合场景

  • 调试导航流程
  • 实现自定义返回导航逻辑
  • 用于分析的用户旅程追踪
  • 在应用重启后重新创建导航状态
  • 实时监控路由转换

主要特点

  • 同时跟踪已推送和已弹出的路由
  • 提供完整的导航历史记录
  • 跨平台支持(Android、iOS、Web、macOS、Windows、Linux)
  • 与导航器的简单集成
  • 实时路线变更监控

适合对象

  • 开发复杂导航工作流的开发者
  • 需要详细导航分析的应用程序
  • 实施自定义导航行为的 Teams
  • 调试路由相关问题
  • 需要状态恢复的应用程序

使用建议

  • 深度导航栈可能会增加内存使用
  • 需要手动管理历史数据
  • 不适用于轻量级导航需求
  • 仅限与 MaterialApp 或 CupertinoApp 一起使用
  • 应正确注册到导航观察者

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter Navigation History Observer

A flutter navigation observer that adds access to lists that track the navigation stack both forwards and backwards.

Usage

This is a singleton, meaning you can access its single instance from anywhere by calling the default constructor.

To initialize the first instance you must add it to your app's navigator observers list like so:

void main() {
  runApp(MaterialApp(
    home: Container(),
    navigatorObservers: [NavigationHistoryObserver()],
  ));
}
History:

To access the history, use NavigationHistoryObserver().history from anywhere in your code.

Use NavigationHistoryObserver().top to peek on the navigation stack's top element - which would be the current route.

Popped routes (forwards history):

To access the popped routes, use NavigationHistoryObserver().poppedRoutes from anywhere in your code.

UseNavigationHistoryObserver().next to get the most recent popped route.

This feature is intended to track the pages visited by the user, rather than navigating forwards. Please keep in mind that popped routes have already been disposed, meaning that you can't simply navigate to them as is via Navigator.push(context, route).

If you'd still like to navigate forwards, you may try Navigator.pushNamed(context, name), whereas the name should be extracted from route.settings.name provided that you're using named routes in your app.
However, there's no guarantee that it'll work.

Nevertheless, I discourage having forwards-navigation in your app.

History Change Stream:

The NavigationHistoryObserver class holds a StreamController that broadcasts each time the navigation stack changes.

Use NavigationHistoryObserver().historyChangeStream.listen((change) => action) to listen for changes.

The change object is of type HistoryChange. It holds the following properties:

  1. action - a NavigationStackAction enum reporting the action that caused this change. Values: push, pop, remove, replace.
  2. newRoute - the new route that was added/removed from the stack.
  3. oldRoute - the old route that was in place before this action happened.

This provides you with an easy-to-use interface that allows updating states across your app whenever the navigation stack changes. See the example for basic usage.

Developer's Note:

The history and poppedRoutes getters return clones of the actual private collections as BuiltLists, meaning they're immutable. To change these collections, use the default Navigator as you normally would, and they'll update as you navigate throughout your app.

Special Thanks

  • Shoutout to the Flutter Israel Developers community for being supportive and encouraging me to create this package.
  • Special thanks to Sahar Vanunu for giving me the original idea for this package.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。