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

套件詳情v1.0.9

nested_navigators

用於實現多個嵌套導航器及其各自路由棧的 Flutter 小部件。

近 30 天下載
22
喜歡數
17
Pub 分數
40

安裝套件

flutter pub add nested_navigators

套件介紹

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

NestedNavigators 是一個 Flutter 套件,可實現多個嵌套導航器,每個導航器皆管理其獨立的路由堆疊。它簡化了具有層級介面的應用中的複雜導航情境,例如標籤介面或畫面內的對話方塊。透過允許每個導航器擁有隔離的導航行為,可防止意外的路由干擾並提升可維護性。此套件可無縫整合至現有的 Flutter 專案中,並支援嵌套結構內的動態路由。

適用情境

  • 複雜標籤介面
  • 螢幕內的模態對話方塊
  • 多層導航流程
  • 組件中的獨立路由棧
  • 嵌套螢幕轉換

主要特色

  • 每個導航器的路由棧隔離
  • 支援深度連結
  • 導航邏輯的清晰分離
  • 與 MaterialApp 的輕鬆整合
  • 動態路由管理

適合對象

  • 具有多個導航層級的應用程式
  • 開發人員管理複雜路由
  • 需要獨立導航上下文的 UI
  • 使用模組化螢幕架構的專案
  • 優先考慮可維護導航代碼的團隊

使用建議

  • 需要仔細的狀態管理
  • 如果過度使用,可能會增加應用的複雜性
  • 簡單導航不需要
  • 與核心 Flutter 相比,文件有限
  • 過度嵌套可能導致效能開銷

原始 README

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

NestedNavigators

Flutter widget for implementing multiple nested navigators with their own route stacks.

Usage

To use, you must at least specify two arguments items and generateRoute:

class RootPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  @override
  Widget build(BuildContext context) {
    return NestedNavigators(
      items: {
        NestedNavItemKey.blue: NestedNavigatorItem(
          initialRoute: Routes.blue,
          icon: Icons.access_time,
          text: "Blue",
        ),
        NestedNavItemKey.red: NestedNavigatorItem(
          initialRoute: Routes.red,
          icon: Icons.send,
          text: "Red",
        ),
        NestedNavItemKey.green: NestedNavigatorItem(
          initialRoute: Routes.green,
          icon: Icons.perm_identity,
          text: "Green",
        ),
      },
      generateRoute: Routes.generateRoute,
    );
  }
}

See Routes.generateRoute()

By default NestedNavigators displays BottomNavigationBar, but you can hide it if you want:

clearStackAfterTapOnCurrentTab: false

You can customize BottomNavigatosBar items in two ways:

  • configure BottomNavigationBarItem:
buildBottomNavigationItem: (key, item, selected) => BottomNavigationBarItem(
           icon: Icon(
             item.icon,
             color: Colors.blue,
           ),
           title: Text(
             item.text,
             style: TextStyle(fontSize: 20),
           ),
         )
  • Define your own widget:
buildCustomBottomNavigationItem: (key, item, selected) => Container(
            height: 60,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Icon(
                  item.icon,
                  size: 24,
                  color: selected ? Colors.blue : null,
                ),
                Text(
                  item.text,
                  style: TextStyle(fontSize: 20, color: selected ? Colors.blue : null),
                ),
              ],
            ),
          )

Initial selected navigator:

  • If number of navigators is even number, than it will be the first elemnt of items;
  • If number of navigators is odd number, than it will be the middle element of items;
  • You can specify your navigator key:
 initialNavigatorKey: NestedNavItemKey.green

You can specify the color of ripple effect in your app theme or in bottomNavigationBarTheme:

bottomNavigationBarTheme: Theme.of(context).copyWith(
       splashColor: Colors.blue[100],
   )

By default, if a user clicks on an already selected tab item, the route stack will be cleared and the initial route will be displayed. You can disable this option:

clearStackAfterTapOnCurrentTab: false

When you navigate to another route within nested navigator, you can hide BottomNavigatorsBar for specific route, just add hideNavTabBar: true to route arguments:

Navigator.of(context).pushNamed(
                    Routes.red,
                    arguments: {
                      hideNavTabBar: true,
                    },
                  );

You can select another navigator from your routes:

NestedNavigatorsBlocProvider.of(context).select(NestedNavItemKey.green)

You can also switch displayed navigator and at the same time make navigation in the new navigator:

NestedNavigatorsBlocProvider.of(context).selectAndNavigate(
                      NestedNavItemKey.green,
                      (navigator) => navigator.pushNamed(
                            Routes.green,
                            arguments: {
                              ArgumentKeys.value: 100,
                            },
                          ));

Add NestedNavigatorsBlocProvider to app widget if you want to use the root navigator and need access to nested navigators from there:

class App extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _AppState();
}

class _AppState extends State<App> {
  final NestedNavigatorsBloc _bloc = NestedNavigatorsBloc<NestedNavItemKey>();

  @override
  Widget build(BuildContext context) {
    return NestedNavigatorsBlocProvider(
      bloc: _bloc,
      child: MaterialApp(
        title: 'Flutter Demo',
        home: RootPage(),
        onGenerateRoute: (routeSettings) => Routes.generateRoute(routeSettings),
      ),
    );
  }
}

You can use Drawer instead of BottomNavigationBar, use drawer or endDrawer for this:

drawer: (items, selectedItemKey, selectNavigator) => Drawer(
            child: ListView(
              children: items.entries
                  .map((entry) => ListTile(
                        title: Text(
                          entry.value.text,
                          style: TextStyle(
                            color: entry.key == selectedItemKey
                                ? Colors.blue
                                : null,
                          ),
                        ),
                        trailing: Icon(
                          entry.value.icon,
                          color:
                              entry.key == selectedItemKey ? Colors.blue : null,
                        ),
                        onTap: () => selectNavigator(entry.key),
                      ))
                  .toList(),
            ),
          ),

Also you can open drawers from your pages:

NestedNavigatorsBlocProvider.of(context).actionWithScaffold(
                  (scaffoldState) => scaffoldState.openDrawer(),
                );

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。