Real time check internet conection

 import 'package:ms_report/constants/Constant.dart';

import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:get/get.dart';
import 'package:ms_report/controllers/login/check_login.dart';
import 'package:connectivity/connectivity.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  // runApp(const MyApp());
  runApp(
    StreamBuilder(
      stream: Connectivity().onConnectivityChanged,
      builder:
          (BuildContext context, AsyncSnapshot<ConnectivityResult> snapshot) {
            FlutterNativeSplash.remove();
        // if (!snapshot.hasData) {
        //   // return Text('Checking internet connectivity...');
        //   return NoInternet();
        // }
        if (snapshot.data != null) {
          var connectivityResult = snapshot.data;
        if (connectivityResult == ConnectivityResult.none) {
          return NoInternet();
        } else {
          return const MyApp();
        }
        } else {
          return NoInternet();
        }
       
      },
    ),
  );
}

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 GetMaterialApp(
      title: 'Medical Survey',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: AppConstant.primarySwatchColor,
      ),
      home: const SplashScreen());
  }
}

class SplashScreen extends StatefulWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    CheckLogin().hasLogin(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: AppConstant.backgroundColor,
      child: Center(
        child: Image.asset(
          'assets/images/splash.png',
          height: 150,
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
   
    return MaterialApp(
      title: 'Medical Survey',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: AppConstant.primarySwatchColor,
      ),
      home: Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset('assets/images/no-wifi.png',height: 120,),
            Text('\nNo internet connection',style: TextStyle(fontWeight: FontWeight.bold),),
          ],
        ),
      ),
    ));
   
  }
}




Comments