Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text("Checking the connection"),
            ),
            body: const Center(
              child: ConnectionWidget(),
            )
        ),
      );
    }

  }
  

connection_widget.dart

  class ConnectionWidget extends StatelessWidget {
    static final _conn = Connectivity();
    const ConnectionWidget();

    @override
    Widget build(BuildContext context) {
      return StreamBuilder<ConnectivityResult>(
        stream: _conn.onConnectivityChanged,
        builder: (context, status) {
          if (status.hasData) {
            final data = status.data;

            if (data != null) {
              switch (data) {
                case ConnectivityResult.wifi:
                  return const WifiConnectionWidget();
                case ConnectivityResult.mobile:
                  return const MobileConnectionWidget();
                case ConnectivityResult.none:
                  return const NoConnectionWidget();
              }
            } else {
              debugPrint("Whoops");
            }
          }

          return const CircularProgressIndicator();
        },
      );
    }
  }
  

wifi_connection.dart

  class WifiConnectionWidget extends StatelessWidget {
    const WifiConnectionWidget();

    @override
    Widget build(BuildContext context) {
      return Wrap(
        spacing: 20,
        crossAxisAlignment: WrapCrossAlignment.center,
        direction: Axis.vertical,
        children: const <Widget>[
          Icon(Icons.wifi,
            color: Colors.lightGreen,
            size: 35,
          ),

          Text("Connected to WiFi")
        ],
      );
    }
  }
  

mobile_connection.dart

  class MobileConnectionWidget extends StatelessWidget {
    const MobileConnectionWidget();

    @override
    Widget build(BuildContext context) {
      return Wrap(
        spacing: 20,
        crossAxisAlignment: WrapCrossAlignment.center,
        direction: Axis.vertical,
        children: const <Widget>[
          Icon(Icons.network_cell,
            color: Colors.lightBlue,
            size: 35,
          ),

          Text("Connected via cellular")
        ],
      );
    }
  }
  

no_connection.dart

  class NoConnectionWidget extends StatelessWidget {
    const NoConnectionWidget();

    @override
    Widget build(BuildContext context) {
      return Wrap(
        spacing: 20,
        crossAxisAlignment: WrapCrossAlignment.center,
        direction: Axis.vertical,
        children: const <Widget>[
          Icon(Icons.error_outline,
            color: Colors.red,
            size: 35,
          ),

          Text("Not connected")
        ],
      );
    }
  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"