connectivity_widget 0.1.7 connectivity_widget: ^0.1.7 copied to clipboard
A widget that shows the user if the phone is connected to the internet or not
import 'package:flutter/material.dart';
import 'package:connectivity_widget/connectivity_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//setup connectivity server to ping and callback
ConnectivityUtils.initialize(
serverToPing:
"https://gist.githubusercontent.com/Vanethos/dccc4b4605fc5c5aa4b9153dacc7391c/raw/355ccc0e06d0f84fdbdc83f5b8106065539d9781/gistfile1.txt",
callback: (response) => response.contains("This is a test!"));
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Connectivity Widget Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ConnectivityWidget(
onlineCallback: _incrementCounter,
builder: (context, isOnline) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"${isOnline ? 'Online' : 'Offline'}",
style: TextStyle(
fontSize: 30, color: isOnline ? Colors.green : Colors.red),
),
SizedBox(
height: 20,
),
Text(
'Number of times we connected to the internet:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}