universal_internet_checker 0.1.2 universal_internet_checker: ^0.1.2 copied to clipboard
Listenable internet check for Flutter Web and Mobile
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:universal_internet_checker/universal_internet_checker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'UIC Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Universal Internet Checker'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _message = '';
StreamSubscription? subscription;
UniversalInternetChecker _internetChecker = UniversalInternetChecker();
@override
void initState() {
super.initState();
subscription = _internetChecker.onConnectionChange.listen((connected) {
setState(() {
_message = connected == ConnectionStatus.online
? 'Connected'
: 'Not Connected';
});
});
}
@override
void dispose() {
subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
ConnectionStatus status =
await UniversalInternetChecker.checkInternet();
setState(() {
_message = status == ConnectionStatus.offline
? 'Connected'
: 'Not connected';
});
},
child: Text('Check connection'),
),
Text(
_message,
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}