connectivity_widget 包提供了一个简单且响应式的控件,用于显示设备的当前互联网连接状态。它可在 Android、iOS、Web、macOS、Windows 和 Linux 平台上监控网络可用性,并在连接状态发生变化时实时更新 UI。该包设计易于集成,允许开发者通过图标或文本等视觉指示器,向用户提示当前是否在线或离线。适用于需要即时反馈网络状态的应用程序,例如消息传递、媒体流传输或数据同步服务。
适合场景
实时网络状态显示
离线模式指示器
连接变化期间的用户体验增强
根据网络状态调整应用功能
在连接状况不佳时加载状态管理
主要特点
跨平台支持
响应式 UI 更新
简单集成,设置最少
可自定义外观
自动检测连接变化
适合对象
需要实时网络感知的应用程序
寻求轻量级连接监控的开发者
需要动态网络反馈的UI组件
针对多个平台的项目
想要即时在线/离线状态指示器的用户
使用建议
在移动平台上需要适当的权限
可能需要额外的逻辑来处理临时网络状态
不处理特定网络类型(例如,Wi-Fi 与蜂窝网络)
仅限连接状态,不包括带宽或质量
依赖底层平台 API 以确保准确性
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
connectivity_widget
A widget that shows the user if the phone is connected to the internet or not
This is accomplished not only by verifying the status of the mobile network and/or wifi, but also by pinging a remote server and verifying its response.
Using the ConnectivityWidget
The ConnectivityWidget uses a builder function that provides you a isOnline flag to build different screens for offline or online mode.
ConnectivityWidget(
builder: (context, isOnline) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("${isOnline ? 'Online' : 'Offline'}", style: TextStyle(fontSize: 30, color: isOnline ? Colors.green : Colors.red),),
SizedBox(height: 20,),
Text(
'Number of times we connected to the internet:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
)
It also provides both a onlineCallback and a offlineCallback that are called when the phone changes the connection state to online and offline, respectively.
If there is a need to change the default offline banner, a Widget can be provided to the offlineBanner parameter. Additionally, its visibility can be enabled or disabled by using the showOfflineBanner parameter.
Configurable Parameters:
serverToPing;
timeoutDuration;
debounceDuration;
verifyResponseCallback;
Changing the server to ping and the response verification
By default, the Connectivity Widget checks if there is a connection to http://www.google.com. If you want to check the availability of a custom endpoint, you can set a new endpoint to ping and a callback to verify the response.
ConnectivityUtils.instance.setCallback((response) => response.contains("This is a test!"));
ConnectivityUtils.instance.setServerToPing("https://gist.githubusercontent.com/Vanethos/dccc4b4605fc5c5aa4b9153dacc7391c/raw/355ccc0e06d0f84fdbdc83f5b8106065539d9781/gistfile1.txt");
Using ConnectivityUtils to Listen to Network Changes
This library also provides access to the ConnectivityUtils class in which you can verify the status of the network.
Stream<bool> ConnectivityUtils.instance.isPhoneConnectedStream // gets the current status of the networkFuture<bool> ConnectivityUtils.instance.isPhoneConnected() // future that determines network status
Note on Web:
Although we can verify the status of the connection in web, what happens is that browsers will cache a request to a specific endpoint, so
we cannot verify for certain if the connection is down or if we are using cache, using the methods presented in this package.