Advertisement
danielbrito1987

connectivity

Apr 5th, 2020
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.43 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5.  
  6. class HomePage extends StatefulWidget {
  7.   final String origem;
  8.  
  9.   HomePage(this.origem);
  10.  
  11.   @override
  12.   _HomePageState createState() => _HomePageState();
  13. }
  14.  
  15. class _HomePageState extends State<HomePage> {
  16.   PageController _pageCtrl = PageController();
  17.   String _connectionStatus = 'Unknown';
  18.   final Connectivity _connectivity = Connectivity();
  19.   StreamSubscription<ConnectivityResult> _connectivitySubscription;
  20.  
  21.   @override
  22.   void initState() {
  23.     super.initState();
  24.  
  25.     initConnectivity();
  26.     _connectivitySubscription =
  27.         _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
  28.   }
  29.  
  30.   Future<void> initConnectivity() async {
  31.     ConnectivityResult result;
  32.     // Platform messages may fail, so we use a try/catch PlatformException.
  33.     try {
  34.       result = await _connectivity.checkConnectivity();
  35.     } on PlatformException catch (e) {
  36.       print(e.toString());
  37.     }
  38.  
  39.     // If the widget was removed from the tree while the asynchronous platform
  40.     // message was in flight, we want to discard the reply rather than calling
  41.     // setState to update our non-existent appearance.
  42.     if (!mounted) {
  43.       return Future.value(null);
  44.     }
  45.  
  46.     return _updateConnectionStatus(result);
  47.   }
  48.  
  49.   Future<void> _updateConnectionStatus(ConnectivityResult result) async {
  50.     switch (result) {
  51.       case ConnectivityResult.wifi:
  52.         String wifiName, wifiBSSID, wifiIP;
  53.  
  54.         try {
  55.           if (Platform.isIOS) {
  56.             LocationAuthorizationStatus status =
  57.                 await _connectivity.getLocationServiceAuthorization();
  58.             if (status == LocationAuthorizationStatus.notDetermined) {
  59.               status =
  60.                   await _connectivity.requestLocationServiceAuthorization();
  61.             }
  62.             if (status == LocationAuthorizationStatus.authorizedAlways ||
  63.                 status == LocationAuthorizationStatus.authorizedWhenInUse) {
  64.               wifiName = await _connectivity.getWifiName();
  65.             } else {
  66.               wifiName = await _connectivity.getWifiName();
  67.             }
  68.           } else {
  69.             wifiName = await _connectivity.getWifiName();
  70.           }
  71.         } on PlatformException catch (e) {
  72.           print(e.toString());
  73.           wifiName = "Failed to get Wifi Name";
  74.         }
  75.  
  76.         try {
  77.           if (Platform.isIOS) {
  78.             LocationAuthorizationStatus status =
  79.                 await _connectivity.getLocationServiceAuthorization();
  80.             if (status == LocationAuthorizationStatus.notDetermined) {
  81.               status =
  82.                   await _connectivity.requestLocationServiceAuthorization();
  83.             }
  84.             if (status == LocationAuthorizationStatus.authorizedAlways ||
  85.                 status == LocationAuthorizationStatus.authorizedWhenInUse) {
  86.               wifiBSSID = await _connectivity.getWifiBSSID();
  87.             } else {
  88.               wifiBSSID = await _connectivity.getWifiBSSID();
  89.             }
  90.           } else {
  91.             wifiBSSID = await _connectivity.getWifiBSSID();
  92.           }
  93.         } on PlatformException catch (e) {
  94.           print(e.toString());
  95.           wifiBSSID = "Failed to get Wifi BSSID";
  96.         }
  97.  
  98.         try {
  99.           wifiIP = await _connectivity.getWifiIP();
  100.         } on PlatformException catch (e) {
  101.           print(e.toString());
  102.           wifiIP = "Failed to get Wifi IP";
  103.         }
  104.  
  105.         setState(() {
  106.           _connectionStatus = '$result\n'
  107.               'Wifi Name: $wifiName\n'
  108.               'Wifi BSSID: $wifiBSSID\n'
  109.               'Wifi IP: $wifiIP\n';
  110.         });
  111.         break;
  112.       case ConnectivityResult.mobile:
  113.       case ConnectivityResult.none:
  114.         setState(() => _connectionStatus = result.toString());
  115.         _scaffoldKey.currentState.showSnackBar(SnackBar(
  116.           duration: Duration(seconds: 3),
  117.           backgroundColor: Colors.green,
  118.           content: Text("Você está online novamente."),
  119.         ));
  120.         break;
  121.       default:
  122.         setState(() => _connectionStatus = 'Unknow');
  123.         _scaffoldKey.currentState.showSnackBar(SnackBar(
  124.           duration: Duration(seconds: 3),
  125.           backgroundColor: Colors.red,
  126.           content: Text("Você está offline no momento."),
  127.         ));
  128.         break;
  129.     }
  130.   }
  131.  
  132.   @override
  133.   Widget build(BuildContext){...}
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement