253 lines
8.5 KiB
Dart
253 lines
8.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
import 'package:wifi_iot/wifi_iot.dart';
|
|
import 'dart:async';
|
|
import 'package:wifi_scan/wifi_scan.dart';
|
|
|
|
import '../login_in/connect/wifi.dart';
|
|
|
|
SocketSetting socket = SocketSetting();
|
|
|
|
class WifiPage extends StatefulWidget {
|
|
const WifiPage({super.key});
|
|
|
|
@override
|
|
State<WifiPage> createState() => _WifiPageState();
|
|
}
|
|
|
|
class _WifiPageState extends State<WifiPage> {
|
|
String ip = "192.168.4.1";
|
|
int port = 6000;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
SchedulerBinding.instance.addPostFrameCallback((_) async {
|
|
await _getScannedResults(context);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
}
|
|
|
|
reconnectToWifi(currentTitle, password, withInternet) async {
|
|
String ssid = currentTitle;
|
|
bool isConnected = await WiFiForIoTPlugin.isConnected();
|
|
if (isConnected) {
|
|
// 断开当前WiFi连接
|
|
await WiFiForIoTPlugin.disconnect();
|
|
}
|
|
|
|
// 连接到指定WiFi网络
|
|
await WiFiForIoTPlugin.connect(ssid,
|
|
password: password,
|
|
security: NetworkSecurity.WPA,
|
|
withInternet: withInternet);
|
|
|
|
// 检查连接状态
|
|
isConnected = await WiFiForIoTPlugin.isConnected();
|
|
if (isConnected) {
|
|
connectedssid = ssid;
|
|
|
|
setState(() {});
|
|
print("Successfully reconnected to WiFi network: $ssid");
|
|
} else {
|
|
print("Failed to reconnect to WiFi network: $ssid");
|
|
}
|
|
}
|
|
|
|
List<WiFiAccessPoint> accessPoints = <WiFiAccessPoint>[];
|
|
StreamSubscription<List<WiFiAccessPoint>>? subscription;
|
|
bool shouldCheckCan = true;
|
|
Future<bool> _canGetScannedResults(BuildContext context) async {
|
|
if (shouldCheckCan) {
|
|
// check if can-getScannedResults
|
|
final can = await WiFiScan.instance.canGetScannedResults();
|
|
// if can-not, then show error
|
|
if (can != CanGetScannedResults.yes) {
|
|
if (mounted) kShowSnackBar(context, "Cannot get scanned results: $can");
|
|
accessPoints = <WiFiAccessPoint>[];
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Future<void> _getScannedResults(BuildContext context) async {
|
|
if (await _canGetScannedResults(context)) {
|
|
// get scanned results
|
|
final results = await WiFiScan.instance.getScannedResults();
|
|
setState(() => accessPoints = results);
|
|
}
|
|
}
|
|
|
|
bool withInternet = false;
|
|
String password = "01010101";
|
|
String currentTitle = "";
|
|
String connectedssid = "";
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final size = MediaQuery.of(context).size;
|
|
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
|
TextEditingController passWordController =
|
|
TextEditingController(text: password);
|
|
|
|
List<Widget> children = [
|
|
Row(
|
|
children: [
|
|
const Text("是否允许上网:"),
|
|
Switch(
|
|
value: withInternet,
|
|
onChanged: ((value) {
|
|
setState(() {
|
|
withInternet = value;
|
|
});
|
|
})),
|
|
],
|
|
),
|
|
TextFormField(
|
|
controller: passWordController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
prefixText: '密码:',
|
|
border: InputBorder.none,
|
|
),
|
|
onChanged: (String value) {
|
|
password = value;
|
|
},
|
|
)
|
|
];
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
toolbarHeight: 40,
|
|
title: const Text('WiFi列表'),
|
|
actions: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
child: IconButton(
|
|
onPressed: () async => _getScannedResults(context),
|
|
icon: const Icon(Icons.refresh)),
|
|
)
|
|
],
|
|
),
|
|
body: Builder(
|
|
builder: (context) => Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 20),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.max,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Divider(),
|
|
Flexible(
|
|
child: Center(
|
|
child: accessPoints.isEmpty
|
|
? const Text("空")
|
|
: ListView.builder(
|
|
itemCount: accessPoints.length,
|
|
itemBuilder: (context, i) {
|
|
var accessPoint = accessPoints[i];
|
|
var signalIcon = accessPoint.level >= -80
|
|
? Icons.signal_wifi_4_bar
|
|
: Icons.signal_wifi_0_bar;
|
|
int level = accessPoint.level;
|
|
if (level <= -80) {
|
|
signalIcon = Icons.signal_wifi_0_bar; // 弱信号
|
|
} else if (level > -80 && level <= -70) {
|
|
signalIcon = Icons.network_wifi_2_bar_sharp; // 较弱
|
|
} else if (level > -70 && level <= -60) {
|
|
signalIcon = Icons.network_wifi_3_bar_sharp; // 较好
|
|
} else if (level > -60 && level <= -50) {
|
|
signalIcon = Icons.network_wifi; // 较强
|
|
} else {
|
|
signalIcon = Icons.signal_wifi_4_bar; // 强信号
|
|
}
|
|
|
|
final title = accessPoint.ssid.isNotEmpty
|
|
? accessPoint.ssid
|
|
: "";
|
|
|
|
return ListTile(
|
|
visualDensity: VisualDensity.compact,
|
|
leading: Icon(signalIcon),
|
|
title: Text(title),
|
|
subtitle: Text(accessPoint.bssid),
|
|
trailing: ElevatedButton(
|
|
child: Text(
|
|
connectedssid == accessPoint.ssid &&
|
|
connectedssid != ""
|
|
? "已连接"
|
|
: '连接'),
|
|
onPressed: () {
|
|
currentTitle = title;
|
|
Scaffold.of(context).openEndDrawer();
|
|
}),
|
|
onTap: () {
|
|
currentTitle = title;
|
|
Scaffold.of(context).openEndDrawer();
|
|
},
|
|
);
|
|
}),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
endDrawer: Drawer(
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.white,
|
|
width: size.width * .8,
|
|
child: SingleChildScrollView(
|
|
child: Container(
|
|
height: size.height,
|
|
padding: const EdgeInsets.fromLTRB(5, 20, 5, 20),
|
|
child: Stack(
|
|
children: [
|
|
Column(
|
|
children: children,
|
|
),
|
|
Positioned(
|
|
bottom: 20,
|
|
left: 10,
|
|
child: Row(
|
|
children: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: const Text("取消")),
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop();
|
|
await reconnectToWifi(
|
|
currentTitle, password, withInternet);
|
|
},
|
|
child: const Text("确认"),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop();
|
|
await socket.connect();
|
|
},
|
|
child: const Text("连接"),
|
|
)
|
|
],
|
|
))
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void kShowSnackBar(BuildContext context, String message) {
|
|
if (kDebugMode) print(message);
|
|
ScaffoldMessenger.of(context)
|
|
..hideCurrentSnackBar()
|
|
..showSnackBar(SnackBar(content: Text(message)));
|
|
}
|