How to hold the splash screen and show alert dialogue if there is no internet in flutter?

Viewed 787

I am a newbie , and I am facing a problem beyond my little knowledge. I am making a test project, where I want to use a splashscreen, as a newbie, I used flutter_native_splash to create splash screen; it is working fine, but now I want to hold the splash screen and show an AlertDialog about internet connectivity. I don't know how to use it :(

extra Q. is there any way to use an button to the AlertDialog, which will reopen / resume the process if the internet connection restore?

2 Answers

Try this package data_connection_checker

class InitialScreen extends StatefullWidget {

 @override
 _InitialScreenState createState() => _InitialScreenState();

}

class _InitialScreenState extends State<InitialScreen> {

 bool hasConnection;

 void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) async {
   setState(() {
    hasConnection = await DataConnectionChecker().hasConnection;
   });
  });
 }

 @override
 Widget build(BuildContext) {
  if(hasConnection) {
   return Text('hasConnection');
  } else {
   return Text('Connection error');
  }
 }

}

Hours of researching with noob knowledge, I maybe found a solution of my problem -->

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';

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

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  int internetValue = 0;
  
  Future<bool> asyncNetCheck() async {
  final result = await InternetAddress.lookup('google.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        return Future<bool>.value(true); 
    } 
  }

  @override
  void initState () {
    super.initState();    
    new Future.delayed(const Duration(seconds: 3),(){
      setState(() {
        internetValue = 2;
        asyncNetCheck().then((value) {
          internetValue = 1;
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => HomePage()),
          );
        });
      });
    });
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: GlobalColor.splashColorFill,
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/splash.png"),
          ),
        ),
        child: internetValue == 0 ? Container(): internetValue == 2? Container(child: Center(child: Text("checking Internet..."),),) : Container() 
      )
    );
  }
}

but still there is a problem though, if internet connection is slow or the process is slowing the thread; the "checking internet..." text splash for a few moment, hope I can fix that too!and to my extra question, I will place an button in space of text, and will call the initstate, hope that will cover at least minimum area!

Related