搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。