Implement plug table

This commit is contained in:
hodasemi 2023-10-06 08:50:31 +02:00
parent c33455ebbc
commit 3758b6eea3
3 changed files with 96 additions and 63 deletions

View file

@ -16,7 +16,25 @@ class Category {
if (response.statusCode == 200) { if (response.statusCode == 200) {
List<Category> categories = []; List<Category> categories = [];
Map<String, Map<String, dynamic>> json = jsonDecode(response.body); Map<String, List<Map<String, dynamic>>> json = jsonDecode(response.body);
for (MapEntry<String, List<Map<String, dynamic>>> category_entry
in json.entries) {
Category category = Category(category_entry.key);
for (Map<String, dynamic> device_map in category_entry.value) {
Device device = Device(
device_map["device_id"],
device_map["device_descriptor"],
device_map["led"],
device_map["power"],
device_map["power_draw"]);
category.devices.add(device);
}
categories.add(category);
}
return categories; return categories;
} else { } else {
@ -25,11 +43,43 @@ class Category {
} }
} }
class CategoryWidget extends StatelessWidget {
Category category;
CategoryWidget({super.key, required this.category});
@override
Widget build(BuildContext context) {
var list = category.devices
.map((device) => TableRow(children: [
Text(device.device_descriptor ?? device.device_id),
Text(device.led_state.toString()),
Text(device.power_state.toString()),
Text("${device.power_draw.toString()} W")
]))
.toList();
list.insert(
0,
const TableRow(children: [
Text("Name"),
Text("LED"),
Text("Power"),
Text("Power Draw"),
]));
return Column(
children: <Widget>[Text(category.name), Table(children: list)]);
}
}
class Device { class Device {
final int device_id; final String device_id;
String? device_descriptor;
bool led_state; bool led_state;
bool power_state; bool power_state;
double power_draw; double power_draw;
Device(this.device_id, this.led_state, this.power_state, this.power_draw); Device(this.device_id, this.device_descriptor, this.led_state,
this.power_state, this.power_draw);
} }

View file

@ -57,67 +57,48 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done return FutureBuilder<List<Category>>(
// by the _incrementCounter method above. future: Category.fetch("smart.gavania.de"),
// builder: (context, AsyncSnapshot<List<Category>> categories) {
// The Flutter framework has been optimized to make rerunning build methods if (!categories.hasData) {
// fast, so that you can just rebuild anything that needs updating rather return const Scaffold();
// than having to individually change instances of widgets. }
return Scaffold(
appBar: AppBar( return Scaffold(
// TRY THIS: Try changing the color here to a specific color (to appBar: AppBar(
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar // TRY THIS: Try changing the color here to a specific color (to
// change color while the other colors stay the same. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
backgroundColor: Theme.of(context).colorScheme.inversePrimary, // change color while the other colors stay the same.
// Here we take the value from the MyHomePage object that was created by backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// the App.build method, and use it to set our appbar title. // Here we take the value from the MyHomePage object that was created by
title: Text(widget.title), // the App.build method, and use it to set our appbar title.
), title: Text(widget.title),
body: Center( ),
// Center is a layout widget. It takes a single child and positions it body: Center(
// in the middle of the parent. // Center is a layout widget. It takes a single child and positions it
child: Column( // in the middle of the parent.
// Column is also a layout widget. It takes a list of children and child: Column(
// arranges them vertically. By default, it sizes itself to fit its // Column is also a layout widget. It takes a list of children and
// children horizontally, and tries to be as tall as its parent. // arranges them vertically. By default, it sizes itself to fit its
// // children horizontally, and tries to be as tall as its parent.
// Column has various properties to control how it sizes itself and //
// how it positions its children. Here we use mainAxisAlignment to // Column has various properties to control how it sizes itself and
// center the children vertically; the main axis here is the vertical // how it positions its children. Here we use mainAxisAlignment to
// axis because Columns are vertical (the cross axis would be // center the children vertically; the main axis here is the vertical
// horizontal). // axis because Columns are vertical (the cross axis would be
// // horizontal).
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" //
// action in the IDE, or press "p" in the console), to see the // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// wireframe for each widget. // action in the IDE, or press "p" in the console), to see the
mainAxisAlignment: MainAxisAlignment.center, // wireframe for each widget.
children: <Widget>[ mainAxisAlignment: MainAxisAlignment.center,
Table( children: categories.data!
border: TableBorder.all(), .map((category) => CategoryWidget(category: category))
) .toList(),
], ),
), ));
), });
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
} }
} }

View file

@ -36,6 +36,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
http: ^1.1.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter