-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconnectivity_page.dart
More file actions
82 lines (71 loc) · 2.28 KB
/
connectivity_page.dart
File metadata and controls
82 lines (71 loc) · 2.28 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'package:flutter/material.dart';
import 'package:flutter_util_code/utils.dart';
import 'package:flutter_util_code_example/widget/display_screen.dart';
/// Name: 网络监听页
/// Created by Fitem on 2023/7/3
class ConnectivityPage extends StatefulWidget {
const ConnectivityPage({super.key});
@override
ConnectivityPageState createState() => ConnectivityPageState();
}
class ConnectivityPageState<ConnectivityPage> extends State {
final ConnectivityUtils _connectivityUtils = ConnectivityUtils.getInstance();
final GlobalKey<DisplayScreenState> globalKey = GlobalKey();
late ConnectivityResultCallBack _callBack;
@override
void initState() {
super.initState();
_callBack = (ConnectivityResult result) {
addContent('当前网络状态: $result\n');
};
}
@override
void dispose() {
// 取消网络监听
disposeConnectivity();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('网络监听工具类'),
),
body: SizedBox(
width: double.infinity,
height: double.infinity,
child: Column(
children: [
ElevatedButton(onPressed: checkConnectivity, child: const Text('获取当前网络状态')),
ElevatedButton(onPressed: addListener, child: const Text('添加网络状态监听')),
ElevatedButton(onPressed: removeListener, child: const Text('移除网络状态监听')),
ElevatedButton(onPressed: disposeConnectivity, child: const Text('取消网络状态监听')),
const Spacer(),
Expanded(flex: 2, child: DisplayScreen(key: globalKey)),
],
),
),
);
}
/// 检查网络状态
Future<void> checkConnectivity() async {
ConnectivityResult result = await _connectivityUtils.checkConnectivity();
addContent('当前网络状态: $result\n');
}
/// 添加监听
void addListener() {
_connectivityUtils.listen(_callBack);
}
/// 移除监听
void removeListener() {
_connectivityUtils.off(_callBack);
}
/// 取消网络监听
void disposeConnectivity(){
_connectivityUtils.dispose();
}
/// 添加内容
void addContent(String content) {
globalKey.currentState?.addContent(content);
}
}