123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:smartledz_wifi_test/plugin/wifi_plugin.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:smartledz_wifi_test/models/wifi_model.dart';
- import 'package:smartledz_wifi_test/utils/modal.dart';
- import 'package:smartledz_wifi_test/widgets/button_for_block.dart';
- import 'package:smartledz_wifi_test/widgets/wifi_item.dart';
- void main() {
- runApp(const MyApp());
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return ScreenUtilInit(
- builder: (BuildContext context, Widget? child) {
- return MaterialApp(
- title: 'SmartLEDZ WiFi Test',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- body: child!,
- ),
- );
- },
- child: const SafeArea(
- left: false,
- right: false,
- bottom: false,
- child: WifiList(),
- ),
- );
- }
- }
- class WifiList extends StatefulWidget {
- const WifiList({Key? key}) : super(key: key);
- @override
- State<WifiList> createState() => _WifiListState();
- }
- class _WifiListState extends State<WifiList> {
- final List<WifiModel> _wifiList = [];
- final List<String> _selectWifiList = [];
- late bool testing = false;
- @override
- void initState() {
- super.initState();
- var eventChannel = const EventChannel('native_event_channel');
- eventChannel.receiveBroadcastStream().listen((dataStr) {
- Map jsonArr = jsonDecode(dataStr);
- debugPrint("收到通知事件 ---> $jsonArr");
- switch(jsonArr["cmd"]){
- case "scanResult":
- setState(() {
- _wifiList.add(WifiModel(
- ssid: jsonArr["ssid"],
- bssid: jsonArr["bssid"],
- rssi: int.parse(jsonArr["level"])
- ));
- });
- break;
- case "scanComplete":
- setState(() {
- _selectWifiList.removeWhere((bssid){
- for(WifiModel wifiModel in _wifiList){
- if(wifiModel.bssid == bssid) return false;
- }
- return true;
- });
- });
- break;
- }
- });
- startScanWifi();
- Timer.periodic(const Duration(seconds: 3), (timer) {
- if(testing) return;
- startScanWifi();
- });
- }
-
- void startScanWifi(){
- _wifiList.clear();
- WiFiPlugin.startScan();
- }
-
- void startTest(){
- if(_wifiList.isEmpty){
- Modal.toast(msg: "wifi列表为空,无法测试");
- return;
- }
- testing = true;
- if(_selectWifiList.isNotEmpty){
- eachTestWifiList(true);
- return;
- }
-
- eachTestWifiList(false);
- }
-
- void eachTestWifiList(bool isSelect, [int index=0]){
- int wifiIndex = index;
- if(isSelect == true){
- if(index >= _selectWifiList.length){
- testing = false;
- return;
- }
- for(int i=0; i<_wifiList.length; i++){
- if(_wifiList[i].bssid == _selectWifiList[index]){
- wifiIndex = i;
- return;
- }
- }
- }else{
- if(index >= _wifiList.length){
- testing = false;
- return;
- }
- }
- WifiModel wifiModel = _wifiList[wifiIndex];
- setState(()=>_wifiList[wifiIndex].result="正在测试");
- debugPrint("开始连接wifi ssid --> ${wifiModel.ssid} bssid --> ${wifiModel.bssid}");
- WiFiPlugin.connect(wifiModel.ssid, wifiModel.bssid, "12345678");
- testing = false;
- }
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- Expanded(
- child: ListView(
- children: List.generate(_wifiList.length, (index){
- WifiModel wifi = _wifiList[index];
- return WifiItem(
- wifiModel: wifi,
- active: _selectWifiList.contains(wifi.bssid),
- onTap: (WifiModel wifiModel){
- setState(() {
- if(_selectWifiList.contains(wifi.bssid)){
- _selectWifiList.remove(wifi.bssid);
- }else{
- _selectWifiList.add(wifi.bssid);
- }
- });
- },
- );
- }),
- ),
- ),
- ButtonForBlock(
- title: "开始测试",
- fontSize: 20.sp,
- radius: 0,
- width: MediaQuery.of(context).size.width,
- height: 55.sp,
- onTap: startTest,
- )
- ],
- );
- }
- }
|