Responsive buttons in table

This commit is contained in:
hodasemi 2023-10-11 09:57:37 +02:00
parent ab477c08a9
commit 77c595528c
3 changed files with 137 additions and 18 deletions

View file

@ -0,0 +1,4 @@
class Constants {
static const String BASE_URL = "http://127.0.0.1:8062";
// static const String BASE_URL = "http://smart.gavania.de";
}

View file

@ -4,14 +4,16 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'constants.dart';
class Category { class Category {
Category(this.name) : devices = []; Category(this.name) : devices = [];
final String name; final String name;
List<Device> devices; List<Device> devices;
static Future<List<Category>> fetch(String base_url) async { static Future<List<Category>> fetch() async {
final response = await http.get(Uri.parse("http://$base_url/devices")); final response = await http.get(Uri.parse("${Constants.BASE_URL}/devices"));
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception("Failed to fetch devices"); throw Exception("Failed to fetch devices");
@ -32,8 +34,8 @@ class Category {
final String? device_descriptor = device_info['desc']; final String? device_descriptor = device_info['desc'];
final bool power_control = device_info['toggle']; final bool power_control = device_info['toggle'];
final response = final response = await http
await http.get(Uri.parse("http://$base_url/plug_state/$device_id")); .get(Uri.parse("${Constants.BASE_URL}/plug_state/$device_id"));
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception("Failed to fetch plug_state for $device_id"); throw Exception("Failed to fetch plug_state for $device_id");
@ -77,18 +79,12 @@ class CategoryWidget extends StatelessWidget {
Text( Text(
textAlign: TextAlign.center, textAlign: TextAlign.center,
device.device_descriptor ?? device.device_id), device.device_descriptor ?? device.device_id),
TextButton( DeviceLed(
child: Text( device_id: device.device_id, led_state: device.led_state),
textAlign: TextAlign.center, DevicePower(
device.led_state ? 'On' : 'Off'), device_id: device.device_id,
onPressed: () {}, power_state: device.power_state,
), power_control: device.power_control),
TextButton(
child: Text(
textAlign: TextAlign.center,
device.power_state ? 'On' : 'Off'),
onPressed: () {},
),
Text(textAlign: TextAlign.center, "${device.power_draw} W") Text(textAlign: TextAlign.center, "${device.power_draw} W")
])) ]))
.toList(); .toList();
@ -142,3 +138,123 @@ class Device {
Device(this.device_id, this.device_descriptor, this.led_state, Device(this.device_id, this.device_descriptor, this.led_state,
this.power_state, this.power_draw, this.power_control); this.power_state, this.power_draw, this.power_control);
} }
class DeviceLed extends StatefulWidget {
final String device_id;
final bool led_state;
DeviceLed({super.key, required this.device_id, required this.led_state});
@override
State<DeviceLed> createState() => DeviceLedState(device_id, led_state);
}
class DeviceLedState extends State<DeviceLed> {
final String device_id;
bool led_state;
String _led_state_info;
DeviceLedState(this.device_id, this.led_state)
: this._led_state_info = led_state ? "On" : "Off";
void _toggle_led_state() {
String target_state;
if (led_state) {
target_state = "off";
} else {
target_state = "on";
}
change_plug_state(device_id, "led", target_state).then((device_state) {
led_state = device_state["led"];
setState(() {
_led_state_info = led_state ? "On" : "Off";
});
});
}
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: _toggle_led_state,
child: Text(textAlign: TextAlign.center, _led_state_info));
}
}
class DevicePower extends StatefulWidget {
final String device_id;
final bool power_state;
final bool power_control;
DevicePower(
{super.key,
required this.device_id,
required this.power_state,
required this.power_control});
@override
State<DevicePower> createState() =>
DevicePowerState(device_id, power_state, power_control);
}
class DevicePowerState extends State<DevicePower> {
final String device_id;
bool power_state;
String _power_state_info;
bool power_control;
DevicePowerState(this.device_id, this.power_state, this.power_control)
: this._power_state_info = power_state ? "On" : "Off";
void _toggle_power_state() {
String target_state;
if (power_state) {
target_state = "off";
} else {
target_state = "on";
}
change_plug_state(device_id, "power", target_state).then((device_state) {
power_state = device_state["power"];
power_control = device_state["power_draw"] < 15;
setState(() {
_power_state_info = power_state ? "On" : "Off";
});
});
}
@override
Widget build(BuildContext context) {
if (power_control) {
return TextButton(
onPressed: _toggle_power_state,
child: Text(textAlign: TextAlign.center, _power_state_info));
} else {
return Text(_power_state_info, textAlign: TextAlign.center);
}
}
}
Future<Map<String, dynamic>> change_plug_state(
String device_id, String module, String state) async {
var response = await http.post(
Uri.parse("${Constants.BASE_URL}/plug/$device_id/${module}_$state"));
if (response.statusCode != 200) {
throw Exception("Failed to post new state");
}
response =
await http.get(Uri.parse("${Constants.BASE_URL}/plug_state/$device_id"));
if (response.statusCode != 200) {
throw Exception("Failed to fetch plug_state for $device_id");
}
return Map.castFrom(jsonDecode(jsonDecode(response.body)));
}

View file

@ -36,8 +36,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder<List<Category>>( return FutureBuilder<List<Category>>(
// future: Category.fetch("smart.gavania.de"), future: Category.fetch(),
future: Category.fetch("127.0.0.1:8062"),
builder: (context, AsyncSnapshot<List<Category>> categories) { builder: (context, AsyncSnapshot<List<Category>> categories) {
if (!categories.hasData) { if (!categories.hasData) {
return const Text("still loading"); return const Text("still loading");