mgs_connectivity_check 1.1.4 copy "mgs_connectivity_check: ^1.1.4" to clipboard
mgs_connectivity_check: ^1.1.4 copied to clipboard

A customizable Flutter connectivity checker with internet validation and automatic no-internet dialog support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:mgs_connectivity_check/mgs_connectivity_check.dart';
import 'package:mgs_connectivity_check/components/mgs_dialog_style.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  @override
  void initState() {
    super.initState();

    /// Start listening and auto show dialog when no internet
    WidgetsBinding.instance.addPostFrameCallback((_) {
      MgsConnectivityCheck
          .listenChangeInInternetConnectivityWithDialog(
        context: context,
        title: "No Internet",
        message: "Please check your connection.",
        dialogStyle: const MgsDialogStyle(
          buttonText: "Retry",
          openSettingsButtonText: "Open Settings",
        ),
      );
    });
  }

  @override
  void dispose() {
    MgsConnectivityCheck.dispose();
    super.dispose();
  }

  /// Manual Internet Check
  Future<void> _checkInternet() async {
    bool connected =
        await MgsConnectivityCheck.isConnectedToInternet();

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
          connected
              ? "Internet Connected"
              : "No Internet",
        ),
      ),
    );
  }

  /// Get Connection Types
  Future<void> _checkConnectionType() async {
    final types =
        await MgsConnectivityCheck.getConnectionTypes();

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text("Connection Type: $types"),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("MGS Connectivity Example"),
        centerTitle: true,
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [

              ElevatedButton(
                onPressed: _checkInternet,
                child: const Text("Check Internet"),
              ),

              const SizedBox(height: 20),

              ElevatedButton(
                onPressed: _checkConnectionType,
                child: const Text("Check Connection Type"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
4
likes
0
points
429
downloads

Publisher

unverified uploader

Weekly Downloads

A customizable Flutter connectivity checker with internet validation and automatic no-internet dialog support.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

app_settings, connectivity_plus, flutter

More

Packages that depend on mgs_connectivity_check