main.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:smartledz_wifi_test/plugin/wifi_plugin.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:smartledz_wifi_test/models/wifi_model.dart';
  8. import 'package:smartledz_wifi_test/utils/modal.dart';
  9. import 'package:smartledz_wifi_test/widgets/button_for_block.dart';
  10. import 'package:smartledz_wifi_test/widgets/wifi_item.dart';
  11. void main() {
  12. runApp(const MyApp());
  13. }
  14. class MyApp extends StatelessWidget {
  15. const MyApp({super.key});
  16. @override
  17. Widget build(BuildContext context) {
  18. return ScreenUtilInit(
  19. builder: (BuildContext context, Widget? child) {
  20. return MaterialApp(
  21. title: 'SmartLEDZ WiFi Test',
  22. theme: ThemeData(
  23. primarySwatch: Colors.blue,
  24. ),
  25. home: Scaffold(
  26. body: child!,
  27. ),
  28. );
  29. },
  30. child: const SafeArea(
  31. left: false,
  32. right: false,
  33. bottom: false,
  34. child: WifiList(),
  35. ),
  36. );
  37. }
  38. }
  39. class WifiList extends StatefulWidget {
  40. const WifiList({Key? key}) : super(key: key);
  41. @override
  42. State<WifiList> createState() => _WifiListState();
  43. }
  44. class _WifiListState extends State<WifiList> {
  45. final List<WifiModel> _wifiList = []; // 扫描的wifi列表
  46. final List<String> _selectWifiList = []; // 已选择的wifi的bssid标识列表
  47. late bool testing = false; // 是否测试中:false[否]、true[是]
  48. @override
  49. void initState() {
  50. super.initState();
  51. var eventChannel = const EventChannel('native_event_channel');
  52. eventChannel.receiveBroadcastStream().listen((dataStr) {
  53. Map jsonArr = jsonDecode(dataStr);
  54. debugPrint("收到通知事件 ---> $jsonArr");
  55. switch(jsonArr["cmd"]){
  56. case "scanResult": // 扫描
  57. setState(() {
  58. _wifiList.add(WifiModel(
  59. ssid: jsonArr["ssid"],
  60. bssid: jsonArr["bssid"],
  61. rssi: int.parse(jsonArr["level"])
  62. ));
  63. });
  64. break;
  65. case "scanComplete": // 扫描完成
  66. setState(() { // 检查已选列表的wifi是否真实存在
  67. _selectWifiList.removeWhere((bssid){
  68. for(WifiModel wifiModel in _wifiList){ // 已存在则不删除
  69. if(wifiModel.bssid == bssid) return false;
  70. }
  71. return true;
  72. });
  73. });
  74. break;
  75. }
  76. });
  77. startScanWifi();
  78. Timer.periodic(const Duration(seconds: 3), (timer) {
  79. if(testing) return; // 测试中,忽略定时扫描
  80. startScanWifi(); // 定时调用扫描方法
  81. });
  82. }
  83. // 扫描wifi
  84. void startScanWifi(){
  85. _wifiList.clear();
  86. WiFiPlugin.startScan();
  87. }
  88. // 开始测试
  89. void startTest(){
  90. if(_wifiList.isEmpty){
  91. Modal.toast(msg: "wifi列表为空,无法测试");
  92. return;
  93. }
  94. testing = true; // 置为测试状态
  95. if(_selectWifiList.isNotEmpty){ // 有选中的wifi,则只处理选中的wifi
  96. eachTestWifiList(true);
  97. return;
  98. }
  99. // 处理全部wifi
  100. eachTestWifiList(false);
  101. }
  102. // 循环测试wifi列表
  103. void eachTestWifiList(bool isSelect, [int index=0]){
  104. int wifiIndex = index;
  105. if(isSelect == true){ // 处理选中设备
  106. if(index >= _selectWifiList.length){ // 处理完成
  107. testing = false;
  108. return;
  109. }
  110. for(int i=0; i<_wifiList.length; i++){
  111. if(_wifiList[i].bssid == _selectWifiList[index]){
  112. wifiIndex = i; // 获取选中设备在wifi列表中的索引值
  113. return;
  114. }
  115. }
  116. }else{
  117. if(index >= _wifiList.length){ // 处理完成
  118. testing = false;
  119. return;
  120. }
  121. }
  122. WifiModel wifiModel = _wifiList[wifiIndex];
  123. setState(()=>_wifiList[wifiIndex].result="正在测试");
  124. debugPrint("开始连接wifi ssid --> ${wifiModel.ssid} bssid --> ${wifiModel.bssid}");
  125. WiFiPlugin.connect(wifiModel.ssid, wifiModel.bssid, "12345678");
  126. testing = false; // TODO developer,置为非测试状态
  127. }
  128. @override
  129. Widget build(BuildContext context) {
  130. return Column(
  131. children: [
  132. Expanded(
  133. child: ListView(
  134. children: List.generate(_wifiList.length, (index){
  135. WifiModel wifi = _wifiList[index];
  136. return WifiItem(
  137. wifiModel: wifi, // wifi模型
  138. active: _selectWifiList.contains(wifi.bssid), // 是否选中状态
  139. onTap: (WifiModel wifiModel){ // 列表点击事件
  140. setState(() {
  141. if(_selectWifiList.contains(wifi.bssid)){ // 已选中,则取消选中
  142. _selectWifiList.remove(wifi.bssid);
  143. }else{
  144. _selectWifiList.add(wifi.bssid);
  145. }
  146. });
  147. },
  148. );
  149. }),
  150. ),
  151. ),
  152. ButtonForBlock(
  153. title: "开始测试",
  154. fontSize: 20.sp,
  155. radius: 0,
  156. width: MediaQuery.of(context).size.width,
  157. height: 55.sp,
  158. onTap: startTest,
  159. )
  160. ],
  161. );
  162. }
  163. }