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

套件詳情v3.0.0

no_context_navigation

一個用於在螢幕(路由)之間導航而無需使用上下文(BuildContext)的 Flutter 套件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
845
喜歡數
20
Pub 分數
150

安裝套件

flutter pub add no_context_navigation

套件介紹

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

no_context_navigation 套件讓 Flutter 開發者能在不需要 BuildContext 的情況下於螢幕之間導航。這在上下文無法取得時特別實用,例如在非同步作業期間或使用全域方法時。透過提供簡單且靜態的導航 API,它簡化了應用程式中的路由管理。此套件支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 與 Linux,使其適用於跨平台應用。

適用情境

  • 從背景任務中導航
  • 全域錯誤處理程式
  • 異步回調
  • 狀態管理整合
  • 應用初始化流程

主要特色

  • 無需BuildContext
  • 靜態導航方法
  • 跨平台支援
  • 簡單 API
  • 適用於任何狀態管理

適合對象

  • 開發人員避免上下文依賴
  • 具有複雜狀態邏輯的應用程式
  • 全域導航工具
  • 大規模的 Flutter 項目
  • 非小部件上下文導航

使用建議

  • 需要正確初始化
  • 如果未妥善管理,可能會繞過路由守衛
  • 不適用於小部件特定的導航
  • 僅限基本路由轉換
  • 最好與路由配置一起使用

原始 README

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

No Context Navigation

No Context Navigation Package - pub.dev

Instead of using Navigator.of(context).pushNamed or some other method like pop() or pushReplacementNamed() USING context. With this package you don't need to anymore.

This service use a GlobalKey of type NavigatorState which can be used across the entire applicaton.

Now, you only need to this:

navService.pushNamed('/detail_screen', args: 'From Home Screen');

But first, add the navigationKey to your MaterialApp's navigationKey property:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //Over here
      navigatorKey: NavigationService.navigationKey,
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (_) => HomeScreen());
          case '/detail_screen':
            return MaterialPageRoute(builder: (_) => DetailScreen(message: settings.arguments));
          default:
            return null;
        }
      },
    );
  }
}

Use navService object to be able to acess those methods:

import 'package:flutter/material.dart';

final NavigationService navService = NavigationService();

class NavigationService<T, U> {
  static GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();

  Future<T> pushNamed(String routeName, {Object args}) async =>
      navigationKey.currentState.pushNamed<T>(
        routeName,
        arguments: args,
      );

  Future<T> push(Route<T> route) async =>
      navigationKey.currentState.push<T>(route);

  Future<T> pushReplacementNamed(String routeName, {Object args}) async =>
      navigationKey.currentState.pushReplacementNamed<T, U>(
        routeName,
        arguments: args,
      );

  Future<T> pushNamedAndRemoveUntil(
    String routeName, {
    Object args,
    bool keepPreviousPages = false,
  }) async =>
      navigationKey.currentState.pushNamedAndRemoveUntil<T>(
        routeName,
        (Route<dynamic> route) => keepPreviousPages,
        arguments: args,
      );

  Future<T> pushAndRemoveUntil(
    Route<T> route, {
    bool keepPreviousPages = false,
  }) async =>
      navigationKey.currentState.pushAndRemoveUntil<T>(
        route,
        (Route<dynamic> route) => keepPreviousPages,
      );

  Future<bool> maybePop([Object args]) async =>
      navigationKey.currentState.maybePop<bool>(args);

  bool canPop() => navigationKey.currentState.canPop();

  void goBack({T result}) => navigationKey.currentState.pop<T>(result);
}

Hope you enjoy!

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。