185 lines
5.0 KiB
Dart
185 lines
5.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:inkubator/theme.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class KontrolView extends StatefulWidget {
|
|
final String espIpAddress;
|
|
|
|
const KontrolView({super.key, required this.espIpAddress});
|
|
|
|
@override
|
|
State<KontrolView> createState() => _KontrolViewState();
|
|
}
|
|
|
|
class _KontrolViewState extends State<KontrolView> {
|
|
bool mistStatus = false;
|
|
bool trayStatus = false;
|
|
bool fanStatus = false;
|
|
double _brightness = 0.0;
|
|
bool _modekontrol = false;
|
|
|
|
void _updateSwitch(String switchName, bool status) async {
|
|
final response = await http.post(
|
|
Uri.parse('http://${widget.espIpAddress}/switch'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: json.encode({switchName: status}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
print('$switchName status updated to $status');
|
|
} else {
|
|
print('Failed to update $switchName status');
|
|
}
|
|
}
|
|
|
|
void _setBrightness(double brightness) async {
|
|
final response = await http.get(Uri.parse(
|
|
'http://${widget.espIpAddress}/setPower?value=${brightness.toInt()}'));
|
|
if (response.statusCode == 200) {
|
|
print('Brightness set to ${brightness.toInt()}');
|
|
} else {
|
|
print('Failed to set brightness');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Widget manualKontrol() {
|
|
// return Container(
|
|
// width: double.infinity,
|
|
// // height: 250,
|
|
// margin: EdgeInsets.only(top: 8),
|
|
// padding: EdgeInsets.all(8),
|
|
// decoration: BoxDecoration(
|
|
// borderRadius: BorderRadius.circular(15),
|
|
// color: Colors.white,
|
|
// boxShadow: [
|
|
// BoxShadow(
|
|
// color: Colors.black.withOpacity(0.5),
|
|
// blurRadius: 1,
|
|
// offset: Offset(0, 1), // changes position of shadow
|
|
// ),
|
|
// ],
|
|
// ),
|
|
// child: Column(
|
|
// children: [
|
|
// SwitchListTile(
|
|
// title: Text('Manual Kontrol'),
|
|
// value: _modekontrol,
|
|
// onChanged: (value) {
|
|
// setState(() {
|
|
// _modekontrol = value;
|
|
// database.child('kontrol/modekontrol').set(value ? 1 : 0);
|
|
// });
|
|
// },
|
|
// ),
|
|
// ],
|
|
// ),
|
|
// );
|
|
// }
|
|
|
|
Widget powerButtons() {
|
|
List<int> powerLevels = [10, 20, 30, 40, 50, 60, 70, 80];
|
|
|
|
return Wrap(
|
|
spacing: 10,
|
|
runSpacing: 10,
|
|
children: powerLevels.map((level) {
|
|
return ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_brightness = level.toDouble();
|
|
_setBrightness(level.toDouble());
|
|
});
|
|
},
|
|
child: Text('$level'),
|
|
);
|
|
}).toList(),
|
|
);
|
|
}
|
|
|
|
Widget tombol() {
|
|
return Container(
|
|
width: double.infinity,
|
|
// height: 250,
|
|
margin: EdgeInsets.only(top: 8),
|
|
padding: EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(15),
|
|
color: Colors.white,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.5),
|
|
blurRadius: 1,
|
|
offset: Offset(0, 1), // changes position of shadow
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
SwitchListTile(
|
|
title: Text("Mist"),
|
|
value: mistStatus,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
mistStatus = value;
|
|
_updateSwitch("mist", mistStatus);
|
|
});
|
|
},
|
|
),
|
|
SwitchListTile(
|
|
title: Text("Tray"),
|
|
value: trayStatus,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
trayStatus = value;
|
|
_updateSwitch("tray", trayStatus);
|
|
});
|
|
},
|
|
),
|
|
SwitchListTile(
|
|
title: Text("Fan"),
|
|
value: fanStatus,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
fanStatus = value;
|
|
_updateSwitch("fan", fanStatus);
|
|
});
|
|
},
|
|
),
|
|
SizedBox(
|
|
height: 5,
|
|
),
|
|
Text("Set Power Dimmer"),
|
|
powerButtons()
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
iconTheme: IconThemeData(color: Colors.black),
|
|
elevation: 0,
|
|
backgroundColor: Colors.black,
|
|
automaticallyImplyLeading: false,
|
|
centerTitle: true,
|
|
title: Text(
|
|
"Kontrol",
|
|
style: secondaryTextStyle.copyWith(fontSize: 16),
|
|
),
|
|
),
|
|
body: Container(
|
|
margin: EdgeInsets.symmetric(horizontal: 10),
|
|
child: Column(
|
|
children: [
|
|
// manualKontrol(),
|
|
tombol(),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|