https://github.com/fuadreza/online_offline
⚙️Flutter package for monitoring internet connectivity in real-time using socket connection
https://github.com/fuadreza/online_offline
android connection-checker flutter flutter-package flutter-socket ios macos windows
Last synced: 3 months ago
JSON representation
⚙️Flutter package for monitoring internet connectivity in real-time using socket connection
- Host: GitHub
- URL: https://github.com/fuadreza/online_offline
- Owner: fuadreza
- License: bsd-3-clause
- Created: 2025-08-07T09:57:59.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-19T17:28:47.000Z (9 months ago)
- Last Synced: 2025-11-03T09:10:16.006Z (7 months ago)
- Topics: android, connection-checker, flutter, flutter-package, flutter-socket, ios, macos, windows
- Language: Dart
- Homepage: https://pub.dev/packages/online_offline
- Size: 95.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Online Offline Flutter Package
[](https://pub.dev/packages/online_offline)
[](https://github.com/fuadreza/online_offline)
## Description
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
## Installation
Add this to your package's `pubspec.yaml` file:
```yaml
dependencies:
online_offline: ^latest_version
```
Then run:
```bash
flutter pub get
```
Or using command line:
```bash
flutter pub add online_offline
```
## Usage
### Basic Usage
```dart
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'}');
},
),
),
),
);
}
}
```
### Customizing Connection Service
```dart
// 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,
);
```
### Accessing Connection Status Directly
```dart
// 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();
}
```
## Complete example link
For a comprehensive demonstration of all features, check out the [example project](https://pub.dev/packages/online_offline/example)
## Contributing
Contributions are welcome! If you'd like to contribute, please:
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.