搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。