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

源码仓库

使用此插件的项目

暂无相关数据。