Implement plug table
This commit is contained in:
parent
c33455ebbc
commit
3758b6eea3
3 changed files with 96 additions and 63 deletions
|
@ -16,7 +16,25 @@ class Category {
|
|||
if (response.statusCode == 200) {
|
||||
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;
|
||||
} 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 {
|
||||
final int device_id;
|
||||
final String device_id;
|
||||
String? device_descriptor;
|
||||
bool led_state;
|
||||
bool power_state;
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -57,27 +57,15 @@ class MyHomePage extends StatefulWidget {
|
|||
}
|
||||
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
// This method is rerun every time setState is called, for instance as done
|
||||
// by the _incrementCounter method above.
|
||||
//
|
||||
// The Flutter framework has been optimized to make rerunning build methods
|
||||
// fast, so that you can just rebuild anything that needs updating rather
|
||||
// than having to individually change instances of widgets.
|
||||
return FutureBuilder<List<Category>>(
|
||||
future: Category.fetch("smart.gavania.de"),
|
||||
builder: (context, AsyncSnapshot<List<Category>> categories) {
|
||||
if (!categories.hasData) {
|
||||
return const Scaffold();
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
// TRY THIS: Try changing the color here to a specific color (to
|
||||
|
@ -106,18 +94,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
// action in the IDE, or press "p" in the console), to see the
|
||||
// wireframe for each widget.
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Table(
|
||||
border: TableBorder.all(),
|
||||
)
|
||||
],
|
||||
children: categories.data!
|
||||
.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.
|
||||
);
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ dependencies:
|
|||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.2
|
||||
|
||||
http: ^1.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
|
Loading…
Reference in a new issue