搜尋套件、儲存庫與 APP 分類

套件詳情v0.2.1-prerelease

riverpod_navigation

使用 riverpod 管理 Flutter 導航

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
14
喜歡數
27
Pub 分數
120

安裝套件

flutter pub add riverpod_navigation

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

RiverpodNavigation 是一個 Flutter 套件,透過與 Riverpod 狀態管理系統的無縫整合,簡化導航管理。它提供了一種反應式、類型安全的方式來處理路由和導航事件,讓開發者能夠以宣告式且高效的方式管理應用導航。透過利用 Riverpod 的依賴注入和狀態管理功能,它實現了路由邏輯與 UI 元件之間的清晰職責分離。該套件支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常適合跨平台應用。

適用情境

  • 宣告式路由管理
  • 跨平台導航
  • 反應式導航流程
  • 狀態驅動路由
  • 與 Riverpod 狀態整合
  • 動態路由處理

主要特色

  • Riverpod 驅動的導航
  • 類型安全的路由定義
  • 響應式導航更新
  • 平台無關設計
  • 路由邏輯的清晰分離
  • 對嵌套導航器的支援

適合對象

  • 使用 Riverpod 的開發者
  • 跨平台 Flutter 應用
  • 複雜導航場景
  • 需要狀態感知路由的應用
  • 注重可維護代碼的團隊
  • 具有動態路由需求的專案

使用建議

  • 需要熟悉 Riverpod
  • 可能會增加簡單應用的樣板程式碼
  • 新使用者的學習曲線
  • 與其他替代方案相比,文件有限
  • 不適用於非 Riverpod 專案
  • 大型導航器中潛在的性能開銷

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Navigation for Riverpod

Managing Flutter navigation with riverpod.

Usage

Bootstrap

Replace your root ProviderScope with a RiverpodNavigation widget with your routing hierarchy and give the provided delegate and parser to your MaterialApp.router factory.

  final routes = RouteDefinition(
      template: UriTemplate.parse('/'),
      builder: (context, entry) => MaterialPage(
        child: HomeLayout(),
      ),
      next: [
        RouteDefinition(
          template: UriTemplate.parse('/articles/:id'),
          builder: (context, entry) => MaterialPage(
            child: ArticleLayout(
              id: entry.parameters['id']!,
            ),
          ),
        ),
      ],
    );
    return RiverpodNavigation( // Replaces your root ProviderScope
      routes: routes,
      builder: (context, delegate, parser) => MaterialApp.router(
        title: 'Flutter Demo',
        routerDelegate: delegate,
        routeInformationParser: parser,
      ),
    );
Navigate
From a provider

A navigationProvider is exposed and can be used to read the current navigation state.

To access the underlying notifier that allows various actions, use the navigationProvider.notifier provider.

final myProvider = Provider<MyState>((ref) {
        final navigation = ref.watch(navigationProvider.notifier);
        return MyState(
            navigateToArticles: () {
                navigation.navigate(Uri.parse('/articles'))
            },
            pop: () {
                navigation.pop();
            }
        );
    },
);
From a BuildContext

The notifier can be accessed with the navigation extension method from the BuildContext.

@override
Widget build(BuildContext context) {
    return TextButton(
        child: Text('Articles'),
        onPressed: () {
            context.navigation.navigate(Uri.parse('/articles'))
        }
    );
}
Pop behaviour

To customize the behaviour of pops when navigating back, a PopBehavior callback can be provided to the RiverpodNavigation instance. The result indicates whether the current pop action should be updated, cancelled or auto (default behavior which simply replace the route with the parent one).

 RiverpodNavigation(
    popBehaviour: (notifier, stack) {
        if (stack.lastRoute?.key == Key('share-article')) {
          return PopResult.update(Uri.parse('/'));
        }
        return PopResult.auto();
      },
    // ...
 )
URI rewriting

The uri can be modified before they are processed by the router with the uriRewriter property. This can be useful for redirecting or normalizing uris.

 RiverpodNavigation(
    uriRewriter: (notifier, uri) {
        if (uri == Uri.parse('/home')) {
            return Uri.parse('/');
        }
        const publicPrefix = 'https://example.com';
        final stringUri = uri.toString();
        if (stringUri.startsWith(publicPrefix)) {
            return Uri.parse(stringUri.substring(publicPrefix.length);
        }
        return uri;
    },
    // ...
);

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。