搜尋套件、儲存庫與 APP 分類

套件詳情v1.0.2

internet_connectivity_checker

幫助您根據裝置的網路連接狀態輕鬆動態管理組件的 Flutter 套件。

AndroidiOSmacOSWindowsLinux
近 30 天下載
458
喜歡數
38
Pub 分數
160

安裝套件

flutter pub add internet_connectivity_checker

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可根據裝置目前的網路連線狀態實現動態元件管理。它提供跨多個平台的即時網路可用性監控,讓開發者能根據網路狀況條件性地顯示內容、顯示載入指示器,或在離線時觸發備用 UI。此套件可無縫整合至需要在不同網路條件下具備回應式行為的應用中。透過簡單的 API 與跨平台支援,確保無論連線狀態為何,皆能提供一致的使用者體驗。

適用情境

  • 顯示離線橫幅
  • 條件性地渲染網路依賴的組件
  • 離線時顯示重試按鈕
  • 連接丟失時的加載佔位符
  • 具有網路感知能力的 API 請求處理
  • 根據即時連接狀態變更更新UI

主要特色

  • 即時連接監控
  • 跨平台相容性
  • 用於小部件控制的簡單 API
  • 自動狀態更新
  • 支援所有主要的桌面和行動平台
  • 輕量且易於整合

適合對象

  • 需要離線模式支援的應用
  • 網路敏感應用程式
  • 需要動態反饋的使用者介面
  • 開發響應式網路感知功能的開發者
  • 針對 Android、iOS、macOS、Windows 和 Linux 的專案

使用建議

  • 在某些平台需要適當的權限
  • 可能需要額外的錯誤處理來應對特殊情況
  • 不適用於高頻率輪詢場景
  • 僅限連接狀態(不包括頻寬)
  • 依賴系統級別的網路檢測API

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

internet_connectivity_checker

The connectivity package and other similar packages only provides information if there is a network connection, but not if the network is connected to the Internet. That's why the internet_connectivity_checker package helps you easily manage widgets dynamically based on the device's internet access.


Demo


🧑‍💻 Getting started

flutter pub add internet_connectivity_checker

🛠️ Usage

Simply import package:internet_connectivity_checker/internet_connectivity_checker.dart' and use the ConnectivityBuilder widget. It takes a builder and an optional parameter, interval(Duration object) which is the time interval to wait between each internet connectivity verification; default is 5 seconds.




Exemple
class Hello extends StatelessWidget {
  const Hello({super.key});

  @override
  Widget build(BuildContext context) {
    return ConnectivityBuilder(
      builder: (status) {
        bool connected = status == ConnectivityStatus.online;
        return Text(connected ? "Online" : "Offline");
      },
    );
  }
}

A more complete example.

ConnectivityBuilder(
  builder: (ConnectivityStatus status) {
    if (status == ConnectivityStatus.online) {
      return Container(
        decoration: BoxDecoration(
          color: Colors.green,
          borderRadius: BorderRadius.circular(15),
        ),
        child: Center(child: Text("Online", style: kTextStyle)),
      );
    } else if (status == ConnectivityStatus.offline) {
      return Container(
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(15),
        ),
        child: Center(child: Text("Offline", style: kTextStyle)),
      );
    } else { // status == ConnectivityStatus.checking
      return const Center(
        child: SizedBox(
          width: 25,
          height: 25,
          child: CircularProgressIndicator(strokeWidth: 2),
        ),
      );
    }
  },
),

You can also use the ConnectivityChecker().stream stream, provided be the package, to handle the logic yourself.

class Foo extends StatelessWidget {
  const Foo({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: ConnectivityChecker(interval: const Duration(seconds: 10)).stream,
      builder: (context, snapshot) {
        if (snapshot.hasData && (snapshot.data as bool) == true) {
          return const Text("Online");
        } else if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else {
          return const Text("Offline");
        }
      },
    );
  }
}

ℹ️ Additional Information

Contribute to this project by making a PR ⬆️ or creating a new issue 🐞 on GitHub.
Do not hesitate to let a 🌟 on the repo if you find it useful.

👷 Maintainers

Created & maintained with 💖 by moustapha.dev.

📄 License

MIT License

Copyright (c) Moustapha Ndoye (@csof3cen), 2023.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。