HomeServer/frontend/lib/states/plug_settings.dart

129 lines
4.1 KiB
Dart
Raw Normal View History

2023-10-14 06:41:27 +00:00
import 'package:flutter/material.dart';
2023-10-14 09:33:49 +00:00
import 'package:http/http.dart' as http;
2023-10-14 06:41:27 +00:00
2023-10-14 09:33:49 +00:00
import '../constants.dart';
2023-10-14 06:41:27 +00:00
class PlugSettingsArguments {
PlugSettingsArguments(this.device_id, this.device_descriptor);
2023-10-14 09:33:49 +00:00
final String device_id;
String? device_descriptor;
2023-10-14 06:41:27 +00:00
}
class PlugSettings extends StatefulWidget {
2023-10-19 14:26:09 +00:00
const PlugSettings({super.key});
2023-10-14 09:33:49 +00:00
2023-10-14 06:41:27 +00:00
@override
State<PlugSettings> createState() => _PlugSettingsState();
}
class _PlugSettingsState extends State<PlugSettings> {
2023-10-15 07:41:35 +00:00
final TextEditingController descriptor_controller = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is removed from the
// widget tree.
descriptor_controller.dispose();
super.dispose();
}
2023-10-14 06:41:27 +00:00
@override
Widget build(BuildContext context) {
final args =
ModalRoute.of(context)!.settings.arguments! as PlugSettingsArguments;
2023-10-15 07:41:35 +00:00
if (args.device_descriptor != null) {
descriptor_controller.text = args.device_descriptor!;
}
2023-10-14 09:33:49 +00:00
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Plug settings'),
),
body: Column(children: [
const SizedBox(height: 60),
Table(
border: TableBorder(
borderRadius: BorderRadius.circular(Constants.TABLE_RADIUS)),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
columnWidths: const {
0: FixedColumnWidth(200.0),
1: FixedColumnWidth(200.0),
},
children: [
TableRow(children: [
const Text(
'ID',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(args.device_id)
]),
TableRow(children: [
const Text(
'Name',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold),
),
TextField(
2023-10-15 07:41:35 +00:00
controller: descriptor_controller,
2023-10-14 09:33:49 +00:00
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius:
BorderRadius.circular(Constants.TABLE_RADIUS)),
hintText: 'device descriptor',
isDense: true,
),
style: const TextStyle(height: 1.0, color: Colors.black),
),
])
],
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pushReplacementNamed(
'/',
);
},
style: TextButton.styleFrom(
foregroundColor: Colors.red[900],
),
child: const Text(
'Back',
style: TextStyle(fontWeight: FontWeight.bold),
)),
const SizedBox(width: 100),
ElevatedButton(
onPressed: () {
http
.post(Uri.parse(
2023-10-15 07:41:35 +00:00
"${Constants.BASE_URL}/device_name/${args.device_id}/${descriptor_controller.text}"))
2023-10-14 09:33:49 +00:00
.then((response) {
if (response.statusCode != 200) {
throw Exception("Failed to fetch devices");
}
Navigator.of(context).pushReplacementNamed(
'/',
);
});
},
style: TextButton.styleFrom(
foregroundColor: Colors.green[900],
),
child: const Text(
'Accept',
style: TextStyle(fontWeight: FontWeight.bold),
))
],
)
]));
2023-10-14 06:41:27 +00:00
}
}