-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathobserve-network-connection-in-flutter.dart
55 lines (50 loc) · 1.51 KB
/
observe-network-connection-in-flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 🐦 Twitter: https://twitter.com/vandadnp
// 🔵 LinkedIn: https://linkedin.com/in/vandadnp
// 🎥 YouTube: https://youtube.com/c/vandadnp
// 💙 Free Flutter Course: https://linktr.ee/vandadnp
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
class ConnectionText extends StatelessWidget {
final AsyncSnapshot<ConnectivityResult> snapshot;
const ConnectionText({
Key? key,
required this.snapshot,
}) : super(key: key);
@override
Widget build(BuildContext context) {
switch (snapshot.connectionState) {
case ConnectionState.active:
final state = snapshot.data!;
switch (state) {
case ConnectivityResult.none:
return const Text('Not connected');
default:
return const Text('Connected');
}
break;
default:
return const Text('');
}
}
}
class HomePage extends HookWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final connectivity = useMemoized(
() => Connectivity(),
[key],
);
return Scaffold(
appBar: AppBar(
title: StreamBuilder<ConnectivityResult>(
stream: connectivity.onConnectivityChanged,
builder: (_, snapshot) => ConnectionText(
snapshot: snapshot,
),
),
),
);
}
}