Skip to content

fuadreza/online_offline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Online Offline Flutter Package

pub.dev GitHub stars

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:

dependencies:
  online_offline: ^latest_version

Then run:

flutter pub get

Or using command line:

flutter pub add online_offline

Usage

Basic Usage

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

// 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

// 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

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 file for details.

About

Flutter package for monitoring internet connectivity in real-time using socket connection

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published