パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.8

connectivity_monitor

ダイナミックなUI更新とトースト通知を備えたインターネット接続状態をモニタリングするFlutterパッケージ。

AndroidiOSWeb
30日間ダウンロード
119
いいね数
27
Pub ポイント
140

パッケージをインストール

flutter pub add connectivity_monitor

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Connectivity Monitor は、Android、iOS、Web プラットフォームでインターネット接続のリアルタイムモニタリングを可能にする Flutter パッケージです。ネットワーク状態が変化したときに、ダイナミックな UI の更新とオプションのトースト通知を提供し、ユーザーが接続の可用性について把握できるようにします。データ同期やオンラインサービスを含むアプリに必要な信頼性の高いネットワーク状態認識を実現するため、シームレスに統合できます。シンプルな API を通じて最小限のセットアップで Wi-Fi およびモバイルデータの検出をサポートしています。

用途

  • リアルタイムネットワーク状態の追跡
  • アプリケーションにおけるオフラインモードの処理
  • 失敗したリクエストに対する自動再試行ロジック
  • 接続喪失に関するユーザーのフィードバック
  • オンライン時にデータを同期する
  • ネットワークの変動時のUX向上

主な機能

  • 接続状態の変更時のダイナミックUI更新
  • ネットワークイベント用のトースト通知
  • クロスプラットフォーム対応(Android、iOS、Web)
  • 最小限の構成でのシンプルな統合
  • Wi-Fi およびモバイルデータの自動検出
  • 軽量で依存関係なし

適している対象

  • 信頼性のあるネットワーク状態の認識を必要とするアプリ
  • オフライン対応のアプリケーションを開発する開発者
  • 接続性に関するユーザーのフィードバックが必要なプロジェクト
  • ネットワークに依存する機能を備えたユーザー体験を重視したアプリケーション
  • 軽量なネットワークモニタリングソリューションを求めるチーム

注意事項

  • Androidでインターネット権限が必要です
  • Webのサポートはブラウザの機能に依存します
  • トースト通知は、異なるテーマに合わせてカスタマイズが必要になる場合があります
  • 基本的な接続チェックに限定(速度や品質のメトリクスなし)
  • 複雑なフローでは、再接続ロジックの手動処理が必要になる場合があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Connectivity Monitor

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.


Features

  • Global Connectivity Monitoring: Automatically tracks network changes and validates active internet access.
  • Customizable Connectivity Indicator: Display dialogs, custom widgets, or toast notifications to inform users of connectivity changes.
  • Debouncing Events: Reduces excessive updates by applying debounce logic to connectivity events.
  • Callbacks for Connectivity Events: Easily handle onConnected and onDisconnected events with custom logic.
  • Cross-Platform Support: Works flawlessly on Android, iOS.
  • Customizable Options: Tailor toast messages, colors, and widgets to match your app's theme and design.

Installation

Add the package to your pubspec.yaml:

dependencies:
  connectivity_monitor: ^1.0.5

Then run:

flutter pub get

Important Note

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.

Usage

1. Start Global Monitoring with ConnectivityService

Start 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')),
    );
  }
}
2. Use ConnectivityMonitor Widget

Wrap 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')),
    );
  }
}
Connectivity Monitor Widget
  • 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)

How to Work with ConnectivityService

Connectivity Service Options
  • 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).

ConnectivityService Methods Explained
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:

  1. Monitors network connectivity changes using connectivity_plus.
  2. Validates actual internet access by pinging a reliable URL.
  3. Triggers callbacks for onConnected and onDisconnected events.
  4. Displays customizable toast notifications for connection status.
  5. Debounces rapid network change events to prevent excessive updates.

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:

  1. Cancels the current subscription to the connectivity listener, preventing further updates.
  2. Closes the StreamController used for broadcasting connectivity changes, releasing all related resources.
  3. Marks the connectivity notifier as inactive.

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.

Parameters:
  • 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).
How It Works:
  1. Sends an HTTP GET request to the specified testUrl.
  2. If the request succeeds and returns a 200 status code, it confirms internet access.
  3. If the request fails, it retries up to the specified number of times before returning 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

Contributions are welcome! Please feel free to submit issues or pull requests to improve this package.

カテゴリー

依存関係

依存関係一覧 4 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。