Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- class HomePage extends StatefulWidget {
- final String origem;
- HomePage(this.origem);
- @override
- _HomePageState createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- PageController _pageCtrl = PageController();
- String _connectionStatus = 'Unknown';
- final Connectivity _connectivity = Connectivity();
- StreamSubscription<ConnectivityResult> _connectivitySubscription;
- @override
- void initState() {
- super.initState();
- initConnectivity();
- _connectivitySubscription =
- _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
- }
- Future<void> initConnectivity() async {
- ConnectivityResult result;
- // Platform messages may fail, so we use a try/catch PlatformException.
- try {
- result = await _connectivity.checkConnectivity();
- } on PlatformException catch (e) {
- print(e.toString());
- }
- // If the widget was removed from the tree while the asynchronous platform
- // message was in flight, we want to discard the reply rather than calling
- // setState to update our non-existent appearance.
- if (!mounted) {
- return Future.value(null);
- }
- return _updateConnectionStatus(result);
- }
- Future<void> _updateConnectionStatus(ConnectivityResult result) async {
- switch (result) {
- case ConnectivityResult.wifi:
- String wifiName, wifiBSSID, wifiIP;
- try {
- if (Platform.isIOS) {
- LocationAuthorizationStatus status =
- await _connectivity.getLocationServiceAuthorization();
- if (status == LocationAuthorizationStatus.notDetermined) {
- status =
- await _connectivity.requestLocationServiceAuthorization();
- }
- if (status == LocationAuthorizationStatus.authorizedAlways ||
- status == LocationAuthorizationStatus.authorizedWhenInUse) {
- wifiName = await _connectivity.getWifiName();
- } else {
- wifiName = await _connectivity.getWifiName();
- }
- } else {
- wifiName = await _connectivity.getWifiName();
- }
- } on PlatformException catch (e) {
- print(e.toString());
- wifiName = "Failed to get Wifi Name";
- }
- try {
- if (Platform.isIOS) {
- LocationAuthorizationStatus status =
- await _connectivity.getLocationServiceAuthorization();
- if (status == LocationAuthorizationStatus.notDetermined) {
- status =
- await _connectivity.requestLocationServiceAuthorization();
- }
- if (status == LocationAuthorizationStatus.authorizedAlways ||
- status == LocationAuthorizationStatus.authorizedWhenInUse) {
- wifiBSSID = await _connectivity.getWifiBSSID();
- } else {
- wifiBSSID = await _connectivity.getWifiBSSID();
- }
- } else {
- wifiBSSID = await _connectivity.getWifiBSSID();
- }
- } on PlatformException catch (e) {
- print(e.toString());
- wifiBSSID = "Failed to get Wifi BSSID";
- }
- try {
- wifiIP = await _connectivity.getWifiIP();
- } on PlatformException catch (e) {
- print(e.toString());
- wifiIP = "Failed to get Wifi IP";
- }
- setState(() {
- _connectionStatus = '$result\n'
- 'Wifi Name: $wifiName\n'
- 'Wifi BSSID: $wifiBSSID\n'
- 'Wifi IP: $wifiIP\n';
- });
- break;
- case ConnectivityResult.mobile:
- case ConnectivityResult.none:
- setState(() => _connectionStatus = result.toString());
- _scaffoldKey.currentState.showSnackBar(SnackBar(
- duration: Duration(seconds: 3),
- backgroundColor: Colors.green,
- content: Text("Você está online novamente."),
- ));
- break;
- default:
- setState(() => _connectionStatus = 'Unknow');
- _scaffoldKey.currentState.showSnackBar(SnackBar(
- duration: Duration(seconds: 3),
- backgroundColor: Colors.red,
- content: Text("Você está offline no momento."),
- ));
- break;
- }
- }
- @override
- Widget build(BuildContext){...}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement