connectivity_monitor
一個 Flutter 套件,用於監控互聯網連接狀態,並支援動態 UI 更新和 Toast 通知。
一個用於無縫網路連線監控的Flutter套件。包含用於檢測網際網路存取的全域ConnectivityService,以及可自訂的ConnectivityMonitor元件,可根據連線狀態動態調整應用程式UI。支援Android與iOS。
{"sdk":"flutter"}^1.3.0^8.2.12^6.1.4^0.28.0{"sdk":"flutter"}以下為英文專案原文快照,最新內容請造訪 GitHub。
connectivity_monitor is a Flutter package that simplifies network connectivity handling in your apps. It provides a robust service (ConnectivityService) and a flexible widget (ConnectivityMonitor) to help you manage connectivity changes seamlessly and enhance user experience.
onConnected and onDisconnected events with custom logic.Add the package to your pubspec.yaml:
dependencies:
connectivity_monitor: ^1.0.5
Then run:
flutter pub get
To use the ConnectivityMonitor widget effectively, the ConnectivityService.startConnectionNotifier must be called first to initialize the global connectivity listener. This ensures that the connectivity events are properly monitored and can be utilized by the ConnectivityMonitor widget or any custom logic you implement.
You can use ConnectivityMonitor to adapt your UI based on connectivity changes or choose not to use it and rely solely on the ConnectivityService for monitoring and callbacks.
ConnectivityServiceStart monitoring network connectivity in your app's main function:
Note: Ensure that WidgetsFlutterBinding.ensureInitialized(); is called before starting the connectivity service.
import 'package:flutter/material.dart';
import 'package:connectivity_monitor/connectivity_monitor.dart';
void main() {
// Ensure bindings are initialized before starting the connectivity service
WidgetsFlutterBinding.ensureInitialized();
// Start monitoring network connectivity globally
ConnectivityService.startConnectionNotifier(
connectedToastMessage: "Connected!",
disconnectedToastMessage: "No Internet Connection.",
showToasts: true, // Show toast notifications for connectivity changes
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Connectivity Monitor Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Connectivity Monitor Example')),
body: Center(child: Text('Home Screen')),
);
}
}
ConnectivityMonitor WidgetWrap your app's widgets or pages with the ConnectivityMonitor widget to display custom UI or messages when the user is offline:
import 'package:flutter/material.dart';
import 'package:connectivity_monitor/connectivity_monitor.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Connectivity Monitor Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ConnectivityMonitor(
child: HomeScreen(),
customDisconnectedWidget: Center(
child: Text(
'No Internet Connection. Please reconnect.',
style: TextStyle(color: Colors.red, fontSize: 18),
),
),
customDialog: AlertDialog(
title: Text('No Internet'),
content: Text('Please check your connection and try again.'),
),
),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Connectivity Monitor Example')),
body: Center(child: Text('Home Screen')),
);
}
}
child (required): The main widget to display when connected.customDisconnectedWidget: Custom widget to display when the device is disconnected. (default: null)requiresConnection: If true, ensures that the child widget is displayed only when the device is connected to the internet. (default: true)useDialogAsConnectivityIndicator: Whether to show a dialog as the connectivity indicator. (default: true)useWidgetAsConnectivityIndicator: Whether to use a custom widget for the connectivity indicator. (default: false)customDialog: Custom dialog widget for the disconnected state. (default: null)startConnectionNotifier: Initializes global connectivity monitoring, validating internet access and broadcasting changes.stopNotifier: Stops global connectivity monitoring and cleans up resources.validateInternetConnection: Checks if the device has access to the internet by pinging a specified URL (default: https://google.com).startConnectionNotifier()The startConnectionNotifier method initializes global connectivity monitoring and listens for internet connection changes. It also provides options for displaying toast notifications, handling connection events, and debouncing network change events. This method must be called before using the ConnectivityMonitor widget to ensure connectivity updates are available.
Features:
connectivity_plus.onConnected and onDisconnected events.Parameters:
connectedToastMessage (String, default: "You are now connected to the Internet."):
The message displayed when the device is connected to the internet.
disconnectedToastMessage (String, default: "No internet connection. Please check your network."):
The message displayed when the device is disconnected from the internet.
testUrl (String, default: "https://google.com"):
The URL used to validate actual internet access.
connectedToastBackgroundColor (Color, default: Colors.green):
The background color of the toast message when connected.
disconnectedToastBackgroundColor (Color, default: Colors.red):
The background color of the toast message when disconnected.
connectedWebToastBackgroundHexColor (String, default: "#4CAF50"):
The background color (in hex) for web toasts when connected.
disconnectedWebToastBackgroundHexColor (String, default: "#F44336"):
The background color (in hex) for web toasts when disconnected.
toastGravity (ToastGravity, default: ToastGravity.BOTTOM):
The position of the toast on the screen.
toastFontSize (double, default: 16.0):
The font size of the toast message.
showToasts (bool, default: true):
Whether to show toast notifications for connectivity changes.
debounceDuration (Duration, default: Duration(milliseconds: 500)):
The debounce duration for connectivity change events.
onConnected (Function, optional):
A callback function triggered when the device connects to the internet.
onDisconnected (Function, optional):
A callback function triggered when the device disconnects from the internet.
Example Usage:
Make sure to call WidgetsFlutterBinding.ensureInitialized(); in your app's main function before starting the notifier.
import 'package:flutter/material.dart';
import 'package:connectivity_monitor/connectivity_service.dart';
void main() {
// Ensure proper initialization
WidgetsFlutterBinding.ensureInitialized();
// Start global connectivity monitoring
ConnectivityService.startConnectionNotifier(
connectedToastMessage: "Connected to the Internet!",
disconnectedToastMessage: "No Internet Connection.",
onConnected: () => debugPrint("Connected!"),
onDisconnected: () => debugPrint("Disconnected!"),
showToasts: true,
);
runApp(MyApp());
}
stopNotifier()The stopNotifier method stops the global connectivity monitoring process and cleans up resources. It performs the following actions:
StreamController used for broadcasting connectivity changes, releasing all related resources.This method is particularly useful in scenarios where you no longer need to monitor connectivity, such as when the app is being disposed or during testing.
// Example usage of stopNotifier
void dispose() {
ConnectivityService.stopNotifier();
super.dispose();
}
Note: It is generally not necessary to call stopNotifier explicitly in most applications because the listener stops automatically when the app is closed. Use it only when specific cleanup is required.
validateInternetConnection()The validateInternetConnection method checks whether the device has active internet access. Unlike basic network connectivity checks (e.g., Wi-Fi or mobile data), this method ensures that the device can actually access the internet by sending an HTTP request to a reliable URL.
testUrl: The URL used to verify internet access (default is https://google.com).retries: The number of times to retry the request if it fails (default is 3).retryDelay: The delay between retry attempts (default is 2 seconds).testUrl.200 status code, it confirms internet access.false.// Example usage of validateInternetConnection
Future<void> checkConnection() async {
bool isConnected = await ConnectivityService.validateInternetConnection(
testUrl: 'https://example.com',
retries: 2,
retryDelay: Duration(seconds: 1),
);
if (isConnected) {
print('Internet is accessible');
} else {
print('No active internet connection');
}
}
Contributions are welcome! Please feel free to submit issues or pull requests to improve this package.