204 lines
5.5 KiB
Dart
204 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
|
|
class SetpointScreen extends StatefulWidget {
|
|
const SetpointScreen({super.key});
|
|
|
|
@override
|
|
State<SetpointScreen> createState() => _SetpointScreenState();
|
|
}
|
|
|
|
class _SetpointScreenState extends State<SetpointScreen> {
|
|
final DatabaseReference db =
|
|
FirebaseDatabase.instance.ref("hydroponic/setpoint");
|
|
|
|
final TextEditingController phMin = TextEditingController();
|
|
final TextEditingController phMax = TextEditingController();
|
|
final TextEditingController tdsMin = TextEditingController();
|
|
final TextEditingController tdsMax = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
loadData();
|
|
}
|
|
|
|
Future<void> loadData() async {
|
|
final snapshot = await db.get();
|
|
|
|
if (snapshot.exists) {
|
|
final data =
|
|
Map<String, dynamic>.from(snapshot.value as Map);
|
|
|
|
phMin.text = data["ph_min"].toString();
|
|
phMax.text = data["ph_max"].toString();
|
|
tdsMin.text = data["tds_min"].toString();
|
|
tdsMax.text = data["tds_max"].toString();
|
|
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
Future<void> simpanData() async {
|
|
await db.set({
|
|
"ph_min": double.parse(phMin.text),
|
|
"ph_max": double.parse(phMax.text),
|
|
"tds_min": int.parse(tdsMin.text),
|
|
"tds_max": int.parse(tdsMax.text),
|
|
});
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text("Setpoint berhasil disimpan"),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget setpointCard({
|
|
required String title,
|
|
required IconData icon,
|
|
required Color color,
|
|
required TextEditingController minController,
|
|
required TextEditingController maxController,
|
|
required String minLabel,
|
|
required String maxLabel,
|
|
}) {
|
|
return Card(
|
|
elevation: 5,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Icon(
|
|
icon,
|
|
color: color,
|
|
size: 28,
|
|
),
|
|
const SizedBox(width: 10),
|
|
Text(
|
|
title,
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
TextField(
|
|
controller: minController,
|
|
keyboardType:
|
|
const TextInputType.numberWithOptions(
|
|
decimal: true,
|
|
),
|
|
decoration: InputDecoration(
|
|
labelText: minLabel,
|
|
prefixIcon: Icon(icon),
|
|
border: OutlineInputBorder(
|
|
borderRadius:
|
|
BorderRadius.circular(12),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 15),
|
|
|
|
TextField(
|
|
controller: maxController,
|
|
keyboardType:
|
|
const TextInputType.numberWithOptions(
|
|
decimal: true,
|
|
),
|
|
decoration: InputDecoration(
|
|
labelText: maxLabel,
|
|
prefixIcon: Icon(icon),
|
|
border: OutlineInputBorder(
|
|
borderRadius:
|
|
BorderRadius.circular(12),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey.shade100,
|
|
|
|
appBar: AppBar(
|
|
title: const Text("Setpoint Otomatis"),
|
|
centerTitle: true,
|
|
backgroundColor: Colors.green,
|
|
),
|
|
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
children: [
|
|
|
|
// CARD PH
|
|
setpointCard(
|
|
title: "Pengaturan pH",
|
|
icon: Icons.science,
|
|
color: Colors.blue,
|
|
minController: phMin,
|
|
maxController: phMax,
|
|
minLabel: "pH Minimum",
|
|
maxLabel: "pH Maksimum",
|
|
),
|
|
|
|
const SizedBox(height: 15),
|
|
|
|
// CARD TDS
|
|
setpointCard(
|
|
title: "Pengaturan Nutrisi (TDS)",
|
|
icon: Icons.water_drop,
|
|
color: Colors.orange,
|
|
minController: tdsMin,
|
|
maxController: tdsMax,
|
|
minLabel: "TDS Minimum (ppm)",
|
|
maxLabel: "TDS Maksimum (ppm)",
|
|
),
|
|
|
|
const SizedBox(height: 25),
|
|
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 55,
|
|
child: ElevatedButton.icon(
|
|
icon: const Icon(Icons.save),
|
|
label: const Text(
|
|
"Simpan Setpoint",
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.green,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius:
|
|
BorderRadius.circular(12),
|
|
),
|
|
),
|
|
onPressed: simpanData,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |