A robust Flutter package for monitoring internet connectivity in real-time.
Unlike traditional connectivity checkers that only verify network interface status,
online_offline
performs actual socket connections to ensure true internet availability.
This package:
- Uses socket connections to verify real internet connectivity (not just network interface status)
- Efficiently manages resources by pausing checks when the app is in background
- Provides easy-to-use widgets for handling online/offline UI states
- Offers customizable check intervals, timeouts, and hosts
Add this to your package's pubspec.yaml
file:
dependencies:
online_offline: ^latest_version
Then run:
flutter pub get
Or using command line:
flutter pub add online_offline
import 'package:flutter/material.dart';
import 'package:online_offline/online_offline.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// Create a connection service instance
final connectionService = ConnectionService();
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Online Offline Demo')),
body: Center(
child: OnlineOffline(
connectionService: connectionService,
onlineWidget: const Text('You are online!', style: TextStyle(color: Colors.green, fontSize: 24)),
offlineWidget: const Text('You are offline!', style: TextStyle(color: Colors.red, fontSize: 24)),
onConnectionChange: (isOnline) {
// Handle connection status changes
print('Connection status changed: ${isOnline ? 'Online' : 'Offline'}');
},
),
),
),
);
}
}
// Create a custom connection service with specific configuration
final connectionService = ConnectionService(
host: 'google.com', // Google Domain (default: 8.8.8.8)
port: 80, // TCP port (default: 53)
timeoutDuration: const Duration(seconds: 3),
checkInterval: const Duration(seconds: 10),
enableDebugLogging: true,
);
// Check current status
bool currentStatus = connectionService.isOnline.value;
// Listen for changes
connectionService.isOnline.addListener(() {
print('Connection changed: ${connectionService.isOnline.value}');
});
// Remember to dispose the service when no longer needed
@override
void dispose() {
connectionService.dispose();
super.dispose();
}
For a comprehensive demonstration of all features, check out the example project
Contributions are welcome! If you'd like to contribute, please:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.