diff --git a/frontend/lib/devices/devices.dart b/frontend/lib/devices/devices.dart index 5072a2c..222460a 100644 --- a/frontend/lib/devices/devices.dart +++ b/frontend/lib/devices/devices.dart @@ -122,12 +122,13 @@ class CategoryWidget extends StatelessWidget { return Wrap( spacing: 25, runSpacing: 25, - children: - category.devices.map((device) => device.create_widget()).toList(), + children: category.devices + .map((device) => device.create_widget(context)) + .toList(), ); } } abstract class Device { - Widget create_widget(); + Widget create_widget(BuildContext context); } diff --git a/frontend/lib/devices/dish_washer.dart b/frontend/lib/devices/dish_washer.dart index 205c7cd..b269772 100644 --- a/frontend/lib/devices/dish_washer.dart +++ b/frontend/lib/devices/dish_washer.dart @@ -8,7 +8,7 @@ class DishWasher extends Device { } @override - Widget create_widget() { + Widget create_widget(BuildContext context) { throw UnimplementedError(); } } diff --git a/frontend/lib/devices/plug.dart b/frontend/lib/devices/plug.dart index db3f86d..14fae8c 100644 --- a/frontend/lib/devices/plug.dart +++ b/frontend/lib/devices/plug.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import '../constants.dart'; +import '../states/plug_settings.dart'; import 'devices.dart'; class Plug extends Device { @@ -44,7 +45,7 @@ class Plug extends Device { this.power_draw, this.power_control); @override - Widget create_widget() { + Widget create_widget(BuildContext context) { const double header_height = 40; const double info_height = 30; const double info_width = 60; @@ -78,7 +79,12 @@ class Plug extends Device { Align( alignment: Alignment.centerRight, child: IconButton( - onPressed: () {}, + onPressed: () { + Navigator.of(context).pushReplacementNamed( + '/plug_settings', + arguments: PlugSettingsArguments( + device_id, device_descriptor ?? "")); + }, icon: const Icon(Icons.settings)), ), ], @@ -146,42 +152,40 @@ class Plug extends Device { class PlugLed extends StatefulWidget { final String device_id; - final bool led_state; + bool led_state; PlugLed({super.key, required this.device_id, required this.led_state}); @override - State createState() => PlugLedState(device_id, led_state); + State createState() => PlugLedState(); } class PlugLedState extends State { - final String device_id; - bool led_state; - String _led_state_info; - - PlugLedState(this.device_id, this.led_state) - : this._led_state_info = led_state ? "On" : "Off"; + String _led_state_info = ""; void _toggle_led_state() { String target_state; - if (led_state) { + if (widget.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"]; + change_plug_state(widget.device_id, "led", target_state) + .then((device_state) { + widget.led_state = device_state["led"]; setState(() { - _led_state_info = led_state ? "On" : "Off"; + _led_state_info = widget.led_state ? "On" : "Off"; }); }); } @override Widget build(BuildContext context) { + _led_state_info = widget.led_state ? "On" : "Off"; + return TextButton( onPressed: _toggle_led_state, child: Text(textAlign: TextAlign.center, _led_state_info)); @@ -190,8 +194,8 @@ class PlugLedState extends State { class PlugPower extends StatefulWidget { final String device_id; - final bool power_state; - final bool power_control; + bool power_state; + bool power_control; PlugPower( {super.key, @@ -200,42 +204,38 @@ class PlugPower extends StatefulWidget { required this.power_control}); @override - State createState() => - PlugPowerState(device_id, power_state, power_control); + State createState() => PlugPowerState(); } class PlugPowerState extends State { - final String device_id; - bool power_state; - String _power_state_info; - bool power_control; - - PlugPowerState(this.device_id, this.power_state, this.power_control) - : this._power_state_info = power_state ? "On" : "Off"; + String _power_state_info = ""; void _toggle_power_state() { String target_state; - if (power_state) { + if (widget.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"]; + change_plug_state(widget.device_id, "power", target_state) + .then((device_state) { + widget.power_state = device_state["power"]; - power_control = device_state["power_draw"] < 15; + widget.power_control = device_state["power_draw"] < 15; setState(() { - _power_state_info = power_state ? "On" : "Off"; + _power_state_info = widget.power_state ? "On" : "Off"; }); }); } @override Widget build(BuildContext context) { - if (power_control) { + _power_state_info = widget.power_state ? "On" : "Off"; + + if (widget.power_control) { return TextButton( onPressed: _toggle_power_state, child: Text(textAlign: TextAlign.center, _power_state_info)); diff --git a/frontend/lib/devices/temp_humid.dart b/frontend/lib/devices/temp_humid.dart index d11c9d5..e2cc195 100644 --- a/frontend/lib/devices/temp_humid.dart +++ b/frontend/lib/devices/temp_humid.dart @@ -9,7 +9,7 @@ class TemperatureHumidity extends Device { } @override - Widget create_widget() { + Widget create_widget(BuildContext context) { // TODO: implement create_widget throw UnimplementedError(); } diff --git a/frontend/lib/devices/thermostat.dart b/frontend/lib/devices/thermostat.dart index 8456dc4..2f8b3e6 100644 --- a/frontend/lib/devices/thermostat.dart +++ b/frontend/lib/devices/thermostat.dart @@ -8,7 +8,7 @@ class Thermostat extends Device { } @override - Widget create_widget() { + Widget create_widget(BuildContext context) { // TODO: implement create_widget throw UnimplementedError(); } diff --git a/frontend/lib/devices/washing_machine.dart b/frontend/lib/devices/washing_machine.dart index 7340d91..5c7281a 100644 --- a/frontend/lib/devices/washing_machine.dart +++ b/frontend/lib/devices/washing_machine.dart @@ -8,7 +8,7 @@ class WashingMachine extends Device { } @override - Widget create_widget() { + Widget create_widget(BuildContext context) { // TODO: implement create_widget throw UnimplementedError(); } diff --git a/frontend/lib/main.dart b/frontend/lib/main.dart index 45f8cfe..97c6dd5 100644 --- a/frontend/lib/main.dart +++ b/frontend/lib/main.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; -import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'constants.dart'; -import 'devices/devices.dart'; + +import 'states/home_page.dart'; +import 'states/plug_settings.dart'; void main() { runApp(const MyApp()); @@ -15,87 +16,16 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Home Server', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Constants.MAIN_COLOR), - useMaterial3: true, - ), - home: const MyHomePage(title: 'Home Server'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - List expanded = []; - - @override - Widget build(BuildContext context) { - return FutureBuilder>( - future: Category.fetch(), - builder: (context, AsyncSnapshot> categories) { - if (!categories.hasData) { - return SpinKitWanderingCubes( - color: Colors.deepPurple[100], - size: 80.0, - ); - } - - final data = categories.data!; - final category_count = data.length; - - if (category_count > expanded.length) { - final int diff = category_count - expanded.length; - - final List diff_list = List.filled(diff, true); - expanded.addAll(diff_list); - } else if (category_count < expanded.length) { - final int diff = expanded.length - category_count; - - expanded = List.filled(diff, false); - } - - return Scaffold( - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text(widget.title), - ), - body: Column(children: [ - Container( - margin: const EdgeInsets.all(15.0), - padding: const EdgeInsets.all(15.0), - child: ExpansionPanelList( - expansionCallback: (int index, bool expand) { - setState(() { - expanded[index] = expand; - }); - }, - children: data.asMap().entries.map( - (category) { - final CategoryWidget widget = - CategoryWidget(category: category.value); - - return ExpansionPanel( - headerBuilder: - (BuildContext context, bool isExpanded) { - return ListTile(title: Text(widget.Name())); - }, - body: widget, - isExpanded: expanded[category.key], - backgroundColor: Colors.deepPurple[100], - canTapOnHeader: true, - ); - }, - ).toList())) - ])); + title: 'Home Server', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Constants.MAIN_COLOR), + useMaterial3: true, + ), + home: const MyHomePage(title: 'Home Server'), + routes: { + '/home': (BuildContext context) => + const MyHomePage(title: 'Home Server'), + '/plug_settings': (BuildContext context) => const PlugSettings(), }); } } diff --git a/frontend/lib/states/home_page.dart b/frontend/lib/states/home_page.dart new file mode 100644 index 0000000..f933364 --- /dev/null +++ b/frontend/lib/states/home_page.dart @@ -0,0 +1,79 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_spinkit/flutter_spinkit.dart'; + +import '../devices/devices.dart'; + +class MyHomePage extends StatefulWidget { + const MyHomePage({super.key, required this.title}); + + final String title; + + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + List expanded = []; + + @override + Widget build(BuildContext context) { + return FutureBuilder>( + future: Category.fetch(), + builder: (context, AsyncSnapshot> categories) { + if (!categories.hasData) { + return SpinKitWanderingCubes( + color: Colors.deepPurple[100], + size: 80.0, + ); + } + + final data = categories.data!; + final category_count = data.length; + + if (category_count > expanded.length) { + final int diff = category_count - expanded.length; + + final List diff_list = List.filled(diff, true); + expanded.addAll(diff_list); + } else if (category_count < expanded.length) { + final int diff = expanded.length - category_count; + + expanded = List.filled(diff, false); + } + + return Scaffold( + appBar: AppBar( + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: Text(widget.title), + ), + body: Column(children: [ + Container( + margin: const EdgeInsets.all(15.0), + padding: const EdgeInsets.all(15.0), + child: ExpansionPanelList( + expansionCallback: (int index, bool expand) { + setState(() { + expanded[index] = expand; + }); + }, + children: data.asMap().entries.map( + (category) { + final CategoryWidget widget = + CategoryWidget(category: category.value); + + return ExpansionPanel( + headerBuilder: + (BuildContext context, bool isExpanded) { + return ListTile(title: Text(widget.Name())); + }, + body: widget, + isExpanded: expanded[category.key], + backgroundColor: Colors.deepPurple[100], + canTapOnHeader: true, + ); + }, + ).toList())) + ])); + }); + } +} diff --git a/frontend/lib/states/plug_settings.dart b/frontend/lib/states/plug_settings.dart new file mode 100644 index 0000000..c2ca4c4 --- /dev/null +++ b/frontend/lib/states/plug_settings.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_spinkit/flutter_spinkit.dart'; + +import '../devices/devices.dart'; + +class PlugSettingsArguments { + final String device_id; + final String device_descriptor; + + PlugSettingsArguments(this.device_id, this.device_descriptor); +} + +class PlugSettings extends StatefulWidget { + const PlugSettings({super.key}); + + @override + State createState() => _PlugSettingsState(); +} + +class _PlugSettingsState extends State { + @override + Widget build(BuildContext context) { + final args = + ModalRoute.of(context)!.settings.arguments! as PlugSettingsArguments; + + throw UnimplementedError(); + } +}