使用wifi_iot 0.3.18写一个flutter弹出WIFI选择连接的页面,并返回给调用页面的demo.
时间: 2024-02-25 13:55:33 浏览: 235
好的,下面是一个完整的示例代码,可以弹出Wi-Fi选择页面并返回所选网络的SSID:
```dart
import 'package:flutter/material.dart';
import 'package:wifi_iot/wifi_iot.dart';
class WifiSelectionPage extends StatefulWidget {
@override
_WifiSelectionPageState createState() => _WifiSelectionPageState();
}
class _WifiSelectionPageState extends State<WifiSelectionPage> {
List<WifiNetwork> _wifiNetworks = [];
@override
void initState() {
super.initState();
_getWifiNetworks();
}
Future<void> _getWifiNetworks() async {
List<WifiNetwork> wifiNetworks = await WifiWizard.listWifiNetworks();
setState(() {
_wifiNetworks = wifiNetworks;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Select a Wi-Fi Network'),
),
body: ListView.builder(
itemCount: _wifiNetworks.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(_wifiNetworks[index].ssid),
onTap: () {
Navigator.pop(context, _wifiNetworks[index].ssid);
},
);
},
),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _selectedWifiNetwork = '';
Future<void> _selectWifiNetwork() async {
String wifiNetwork = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => WifiSelectionPage()),
);
setState(() {
_selectedWifiNetwork = wifiNetwork;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Wi-Fi Selection Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Selected Wi-Fi Network:'),
SizedBox(height: 20),
Text(
_selectedWifiNetwork,
style: TextStyle(fontSize: 20),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _selectWifiNetwork,
child: Icon(Icons.wifi),
),
);
}
}
```
在这个示例中,我们创建了两个页面:`HomePage`和`WifiSelectionPage`。`HomePage`显示一个浮动操作按钮,点击它可以弹出`WifiSelectionPage`来选择一个Wi-Fi网络。`WifiSelectionPage`列出所有可用的Wi-Fi网络,并允许用户选择一个网络,然后返回所选网络的SSID。
希望这可以帮助你实现你的需求。
阅读全文