Skip to content

sbaskoy/s_network_connection_checker

Repository files navigation

SNetwork Connection Checker

Screenshots

poc poc
poc

Configs

[failedPingCount] -> a failed ping up to this parameter is considered a lost connection

[reTryDuration] -> throws a ping during this duration

[pingTimeout] -> If there is no response to the ping in this time, it is considered unsuccessful

[urls] -> links to ping, As default, the first element of the list is taken

SNetworkConnectionChecker.instance.selectedIp.setState(SIpModel("Google 1", "8.8.8.8", 53));

  SNetworkConnectionChecker.instance.setConfig(
    failedPingCount: 2,
    pingTimeout: const Duration(seconds: 2),
    reTryDuration: const Duration(seconds: 3),
    urls: [
      SIpModel("Google 1", "8.8.8.8", 53),
      SIpModel("Google 2", "8.8.4.4", 53),
    ],
  );

Usage

Check if you have internet connection

  var hasConnection = await SNetworkConnectionChecker.instance.hasConnection();

  print("Has connection $hasConnection");
  runApp(const FullScreenWarningWithOpacity());

Listen to the status of your internet connection

void main() async {
    // IMPORTANT
    // You can call anywhere you want
    SNetworkConnectionChecker.instance.startListen();
    runApp(const FullScreenWarningWithOpacity());
}
// ..........

SNetworkConnectionChecker.instance.isConnected.listen(
      (value) {
        // true connected, false not connected, null loading
        print("Connection status changed $value");
      },
    );

/// .........

Use in UI

  • Basic usage
MaterialApp(
      title: 'SNetwork connection checker example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      builder: (context, child) {
        return Scaffold(
          body: SNetworkConnectionCheckerWidget(
            loadingBuilder: () {
              return const Center(child: Text("Loading"));
            },
            disConnectedBuilder: () {
              return const Center(child: Text("Custom disconnected page"));
            },
            child: child!,
          ),
        );
      },
      home: const Scaffold(
        body: Center(
          child: Text("Connected"),
        ),
      ),
    );
  }
  • other usage
MaterialApp(
      title: 'SNetwork connection checker example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      builder: (context, child) {
        return Scaffold(
          body: SNetworkConnectionChecker.instance.isConnected.builder((loading, isConnected, error, context) {
            if (isConnected == null) {
              return const Center(
                child: Text("Loading"),
              );
            }
            return Stack(
              children: [
                AnimatedOpacity(
                  opacity: isConnected ? 1 : 0.1,
                  duration: const Duration(milliseconds: 100),
                  child: IgnorePointer(
                    ignoring: isConnected == false,
                    child: child,
                  ),
                ),
                if (!isConnected)
                  const Align(
                    alignment: Alignment.center,
                    child: Center(
                        child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text("No connection"),
                        Text("Please check your network connection"),
                      ],
                    )),
                  )
              ],
            );
          }),
        );
      },
      home: const Scaffold(
        body: Center(
          child: Text("Home page"),
        ),
      ),
    );
  }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published