Files
liteauthconfig/lib/pages/reconfigure_network.dart

196 lines
5.4 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:liteauthconfig/models/appconfig.dart';
import 'package:liteauthconfig/dialogs/esptouchdialog.dart';
import 'package:liteauthconfig/l10n/app_localizations.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
// ignore_for_file: use_build_context_synchronously
class ReconfigureNetworkPage extends StatefulWidget {
final int deviceIndex;
const ReconfigureNetworkPage(this.deviceIndex, {super.key});
@override
State<StatefulWidget> createState() => _ReconfigureNetworkPageState();
}
class _ReconfigureNetworkPageState extends State<ReconfigureNetworkPage> {
TextEditingController ssidController = TextEditingController();
TextEditingController bssidController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool potentially5GHz = false;
bool obscurePassword = true;
@override
void initState() {
super.initState();
askForNetworkInfo();
}
void askForNetworkInfo() async {
await Future.delayed(Duration(milliseconds: 250));
if (!context.mounted) {
return;
}
var appLocal = AppLocalizations.of(context)!;
var appTheme = Theme.of(context);
if (await Permission.location.isDenied) {
showDialog(
context: context,
builder: (context) => AlertDialog.adaptive(
backgroundColor: appTheme.cardColor,
title: Text(appLocal.networkInfoHeadline),
content: Text(appLocal.networkInfoPrompt),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(appLocal.cancel),
),
TextButton(
onPressed: () async {
Navigator.pop(context);
if (await Permission.location.request().isGranted) {
fetchNetworkInfo();
}
},
child: Text(appLocal.continueText),
),
],
),
);
} else if (await Permission.location.isGranted) {
fetchNetworkInfo();
}
}
void fetchNetworkInfo() async {
final dev = AppConfig().devices[widget.deviceIndex];
var netInfo = NetworkInfo();
var name = await netInfo.getWifiName() ?? dev.routerSsid;
if (Platform.isAndroid) {
name = name.substring(1, name.length - 1);
}
ssidController.text = name;
var bssid = await netInfo.getWifiBSSID();
if (bssid == dev.routerBssid) {
passwordController.text = dev.networkPassword;
}
bssidController.text = bssid ?? dev.routerBssid;
if (name.contains("5G")) {
setState(() {
potentially5GHz = true;
});
}
}
void submit() async {
var dev = AppConfig().devices[widget.deviceIndex];
await showDialog(
context: context,
builder: (context) => ESPTouchDialog(
name: dev.name,
ssid: ssidController.text,
bssid: bssidController.text,
password: passwordController.text,
entryIndex: widget.deviceIndex,
),
);
if (context.mounted) {
Navigator.pop(context, true);
}
}
@override
Widget build(BuildContext context) {
final appLocal = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
title: Text(appLocal.reconfigureNetwork),
),
floatingActionButton: FloatingActionButton(
onPressed: submit,
child: const Icon(Icons.check),
),
body: Padding(
padding: EdgeInsetsGeometry.all(16),
child: Column(
spacing: 16,
children: [
if (potentially5GHz)
Card(
child: Column(
children: [
ListTile(
leading: const Icon(Icons.warning),
title: Text(appLocal.networkLimitation),
subtitle: Text(appLocal.networkLimitationDesc),
),
],
),
),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: appLocal.networkName,
),
controller: ssidController,
onChanged: (name) {
setState(() {
potentially5GHz = name.contains("5G");
});
},
),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: appLocal.routerMacAddress,
),
controller: bssidController,
),
TextField(
obscureText: obscurePassword,
controller: passwordController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: appLocal.networkPassword,
suffixIcon: IconButton(
onPressed: () {
setState(() {
obscurePassword = !obscurePassword;
});
},
icon: Icon(
obscurePassword
? Icons.remove_red_eye
: Icons.remove_red_eye_outlined,
),
),
),
),
],
),
),
);
}
}