|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: public_member_api_docs |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | +import 'dart:io'; |
| 9 | + |
| 10 | +import 'package:connectivity/connectivity.dart'; |
| 11 | +import 'package:flutter/foundation.dart'; |
| 12 | +import 'package:flutter/material.dart'; |
| 13 | +import 'package:flutter/services.dart'; |
| 14 | + |
| 15 | +// Sets a platform override for desktop to avoid exceptions. See |
| 16 | +// https://flutter.dev/desktop#target-platform-override for more info. |
| 17 | +void _enablePlatformOverrideForDesktop() { |
| 18 | + if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) { |
| 19 | + debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +void main() { |
| 24 | + _enablePlatformOverrideForDesktop(); |
| 25 | + runApp(MyApp()); |
| 26 | +} |
| 27 | + |
| 28 | +class MyApp extends StatelessWidget { |
| 29 | + // This widget is the root of your application. |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + return MaterialApp( |
| 33 | + title: 'Flutter Demo', |
| 34 | + theme: ThemeData( |
| 35 | + primarySwatch: Colors.blue, |
| 36 | + ), |
| 37 | + home: MyHomePage(title: 'Flutter Demo Home Page'), |
| 38 | + ); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class MyHomePage extends StatefulWidget { |
| 43 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 44 | + |
| 45 | + final String title; |
| 46 | + |
| 47 | + @override |
| 48 | + _MyHomePageState createState() => _MyHomePageState(); |
| 49 | +} |
| 50 | + |
| 51 | +class _MyHomePageState extends State<MyHomePage> { |
| 52 | + String _connectionStatus = 'Unknown'; |
| 53 | + final Connectivity _connectivity = Connectivity(); |
| 54 | + StreamSubscription<ConnectivityResult> _connectivitySubscription; |
| 55 | + |
| 56 | + @override |
| 57 | + void initState() { |
| 58 | + super.initState(); |
| 59 | + initConnectivity(); |
| 60 | + _connectivitySubscription = |
| 61 | + _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + void dispose() { |
| 66 | + _connectivitySubscription.cancel(); |
| 67 | + super.dispose(); |
| 68 | + } |
| 69 | + |
| 70 | + // Platform messages are asynchronous, so we initialize in an async method. |
| 71 | + Future<void> initConnectivity() async { |
| 72 | + ConnectivityResult result; |
| 73 | + // Platform messages may fail, so we use a try/catch PlatformException. |
| 74 | + try { |
| 75 | + result = await _connectivity.checkConnectivity(); |
| 76 | + } on PlatformException catch (e) { |
| 77 | + print(e.toString()); |
| 78 | + } |
| 79 | + |
| 80 | + // If the widget was removed from the tree while the asynchronous platform |
| 81 | + // message was in flight, we want to discard the reply rather than calling |
| 82 | + // setState to update our non-existent appearance. |
| 83 | + if (!mounted) { |
| 84 | + return Future.value(null); |
| 85 | + } |
| 86 | + |
| 87 | + return _updateConnectionStatus(result); |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + Widget build(BuildContext context) { |
| 92 | + return Scaffold( |
| 93 | + appBar: AppBar( |
| 94 | + title: const Text('Connectivity example app'), |
| 95 | + ), |
| 96 | + body: Center(child: Text('Connection Status: $_connectionStatus')), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + Future<void> _updateConnectionStatus(ConnectivityResult result) async { |
| 101 | + switch (result) { |
| 102 | + case ConnectivityResult.wifi: |
| 103 | + case ConnectivityResult.mobile: |
| 104 | + case ConnectivityResult.none: |
| 105 | + setState(() => _connectionStatus = result.toString()); |
| 106 | + break; |
| 107 | + default: |
| 108 | + setState(() => _connectionStatus = 'Failed to get connectivity.'); |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments