🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / internet bağlantısı kontrolü giderse notfoundpage e yönlendirme tekrar dene buttonu koyma Check whether there is an Internet connection available on Flutter app

1-) FLUTTER - internet bağlantısı kontrolü giderse notfoundpage e yönlendirme tekrar dene buttonu koyma Check whether there is an Internet connection available on Flutter app

 

flutter pub add connectivity

 

1- NotFoundPage.dart

import 'package:connectivity/connectivity.dart';

import 'package:flutter/material.dart';

import 'package:indomindo/main.dart';

 

class NotFoundPage extends StatefulWidget {

  const NotFoundPage({super.key});

 

  @override

  State<NotFoundPage> createState() => _NotFoundPageState();

}

 

class _NotFoundPageState extends State<NotFoundPage> {

  Future<bool> _checkInternetConnection() async {

    var connectivityResult = await (Connectivity().checkConnectivity());

    if (connectivityResult == ConnectivityResult.none) {

      Navigator.pushReplacement(

        context,

        MaterialPageRoute(builder: (context) => NotFoundPage()),

      );

 

      return false;

    }

 

    return true;

  }

 

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Internet Connection Error'),

      ),

      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,

          children: [

            Text(

              'Anda tidak memiliki koneksi internet!',

              style: TextStyle(fontSize: 20),

            ),

            SizedBox(height: 20),

            ElevatedButton(

              onPressed: () async {

                bool varmi = await _checkInternetConnection();

                if (varmi == false) {

                } else {

                  Navigator.pushReplacement(

                    context,

                    MaterialPageRoute(builder: (context) => MyApp()),

                  );

 

                }

              },

              child: Text('Coba lagi'),

            ),

          ],

        ),

      ),

    );

  }

}

 

 

2- _checkInternetConnection fonksiyonu

 

Future<bool> _checkInternetConnection() async {

    var connectivityResult = await (Connectivity().checkConnectivity());

    if (connectivityResult == ConnectivityResult.none) {

      Navigator.pushReplacement(

        context,

        MaterialPageRoute(builder: (context) => NotFoundPage()),

      );

 

      return false;

    }

 

    return true;

  }

 

 

3- Main.dart

 

 @override

  void initState() {

    super.initState();

    _checkInternetConnection();

 

 

 

 2024 Mayıs 29 Çarşamba
 490