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

cross_connectivity 封面
套件詳情v3.3.0

cross_connectivity

一個用於在行動、Web 和桌面平台處理連接性與真實連接狀態的 Flutter 插件。支援 iOS、Android、Web、Windows、Linux 和 macOS。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1490
喜歡數
128
Pub 分數
140

安裝套件

flutter pub add cross_connectivity

套件介紹

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

CrossConnectivity 是一個 Flutter 插件,可在行動、Web 和桌面平台提供一致的連線狀態監控。它區分一般網路可用性與實際現實世界的連線狀態,確保可靠檢測到活躍的網際網路存取。相容於 Android、iOS、Web、macOS、Windows 和 Linux,為開發者提供統一的 API 來處理多樣環境中的網路狀況。此外,該插件支援 Wi-Fi 與蜂巢網路,並提供連線變更的即時更新。

適用情境

  • 基於連接狀態的即時應用程式回應能力
  • 跨平台應用中的離線模式處理
  • 資料同步操作的網路健康監控
  • 連接狀態變更時動態更新使用者介面
  • 僅透過活躍連接確保安全的資料傳輸

主要特色

  • 支援所有主要平台,包括網頁和桌面
  • 區分連接狀態和實際連接狀態
  • 網路變更的即時事件發射
  • 輕量且易於整合
  • 提供準確的連接類型檢測

適合對象

  • 跨平台 Flutter 應用
  • 需要強大離線/線上行為的應用程式
  • 針對網頁、桌面以及行動裝置的開發者
  • 需要精確網絡狀態感知的應用程式

使用建議

  • 需要移動平台特定的權限
  • Web 支援取決於瀏覽器功能
  • 桌面實現可能因作業系統略有不同
  • 不適用於低階網路診斷
  • 可能需要額外的邏輯來處理複雜的網路場景

原始 README

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

cross_connectivity

Build Pub GitHub GitHub stars

A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.

Getting Started

In order to use this plugin, add dependency in the pubspec.yaml:

cross_connectivity: any

or

cross_connectivity:
    git:
      url: https://github.com/marchdev-tk/cross_connectivity

Add an import to dart file:

import 'package:cross_connectivity/cross_connectivity.dart';
Samples

Web sample:

Web Sample

Desktop sample:

Desktop Sample

Mobile sample:

Mobile Sample

Usage
Functional approach

This plugin provides two streams:

  • isConnected that shows whether the device is REALLY connected to the network or not.
  • onConnectivityChanged that it will not let you know about state of the REAL network connection. It only shows connectivity state.

Also for one time check could be used following methods:

  • checkConnection() that is working like isConnected, but returns Future<bool> instread of Stream<bool>.
  • checkConnectivity() that is working like onConnectivityChanged, but returns Future<ConnectivityStatus> instread of Stream<ConnectivityStatus>.

There are no more methods (they are working only on Android/iOS/macOS):

  • getWifiName() - Obtains the wifi name (SSID) of the connected network.
  • getWifiBSSID() - Obtains the wifi BSSID of the connected network.
  • getWifiIP() - Obtains the IP address of the connected wifi network.

They are removed to wifi_info_flutter.

Migration guide:

If you don't use any of the above APIs, your code should work as is. In addition, you can also remove NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription in ios/Runner/Info.plist

If you use any of the above APIs, you can find the same APIs in the wifi_info_flutter plugin. For example, to migrate getWifiName, use the new plugin:

final WifiInfo _wifiInfo = WifiInfo();
final String wifiName = await _wifiInfo.getWifiName();
Widget approach

As an alteration to funcitonal approach could be used ConnectivityBuilder widget as follows:

ConnectivityBuilder(
  builder: (context, isConnected, status) => Row(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Icon(
        isConnected == true
            ? Icons.signal_wifi_4_bar
            : Icons.signal_wifi_off,
        color: isConnected == true ? Colors.green : Colors.red,
      ),
      const SizedBox(width: 8),
      Text(
        '$status',
        style: TextStyle(
          color: status != ConnectivityStatus.none
              ? Colors.green
              : Colors.red,
        ),
      ),
    ],
  ),
)

Feature requests and Bug reports

Feel free to post a feature requests or report a bug here.

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。