搜索插件、代码仓与 APP 分类

插件详情v1.0.0+18-dev.1

flutter_no_internet_widget

一个全新的 Flutter 小部件,无需额外代码或依赖即可显示在线或离线小部件。

AndroidiOSmacOSWindowsLinux
近 30 天下载
4
喜欢数
51
Pub 分数
150

安装插件

flutter pub add flutter_no_internet_widget

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的 Flutter 小部件,当设备离线时自动显示自定义 UI,并在连接恢复时无缝切换到在线视图。它无需额外依赖或样板代码,可轻松集成到任何 Flutter 应用中。该小部件实时监控网络状态,并提供一种简洁、声明式的方式来处理离线状态,而无需复杂的逻辑。非常适合需要即时反馈连接变化的应用。

适合场景

  • 在应用中显示离线占位符
  • 在连接丢失期间提供用户反馈
  • 通过动态在线/离线视图提升用户体验
  • 简化网络状态管理
  • 为不可靠网络构建有弹性的用户界面

主要特点

  • 自动网络状态检测
  • 最小设置,无额外依赖
  • 可自定义的在线和离线小部件
  • 实时连接监控
  • 无需样板代码

适合对象

  • 需要基本离线支持的简单应用
  • 寻求快速网络状态解决方案的开发者
  • 需要响应式UI以应对连接变化的应用程序
  • 开发周期紧张的项目
  • 避免复杂网络处理的团队

使用建议

  • 仅限基本的在线/离线检测
  • 不处理特定网络类型(例如,Wi-Fi 与蜂窝网络)
  • 没有内置的重试机制来处理失败的请求
  • 不适合高级网络诊断
  • 依赖平台级别的连接性API

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_no_internet_widget

flutter_no_internet_widget codecov

Overview

A new Flutter widget shows online or offline screens without extra code or dependencies.

We need a lot of boilerplate code to check every project's network connectivity. Why can't there be a single package that takes care of everything and make the developer's life easy?

I also had the same question and have created this simple widget to help myself and others.

Use case

The package's primary purpose is to show a default offline screen and a mandatory online screen provided while initializing.

Declare this widget at the top of the widget tree once. Whenever there is no internet, the default offline screen or the provided screen will be displayed.

Example

InternetWidget(
 online: Text('Online'),
 offline: Text('offline),
 );

Full Example

import 'package:flutter/material.dart';
import 'package:flutter_no_internet_widget/flutter_no_internet_widget.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return InternetWidget(
      offline: const FullScreenWidget(),
      // ignore: avoid_print
      whenOffline: () => print('No Internet'),
      // ignore: avoid_print
      whenOnline: () => print('Connected to internet'),
      loadingWidget: const Center(child: Text('Loading')),
      online: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headline4,
              ),
              ElevatedButton(
                onPressed: () {
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => const SecondScreen(),
                    ),
                  );
                },
                child: const Text('Navigate'),
              )
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

Video

ezgif-3-b21b127bea

Use this package as a library

Run this command:

With Dart:

 dart pub add flutter_no_internet_widget

With Flutter:

 flutter pub add flutter_no_internet_widget

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  flutter_no_internet_widget: [use_latest]

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it Now in your Dart code, you can use:

import 'package:flutter_no_internet_widget/flutter_no_internet_widget.dart';

Maintainers

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目

暂无相关数据。