297 lines
11 KiB
Dart
297 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class PumpScheduleScreen extends StatefulWidget {
|
|
const PumpScheduleScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<PumpScheduleScreen> createState() => _PumpScheduleScreenState();
|
|
}
|
|
|
|
class _PumpScheduleScreenState extends State<PumpScheduleScreen> {
|
|
final DatabaseReference _database = FirebaseDatabase.instance.ref();
|
|
Map<String, dynamic> pumpSchedules = {};
|
|
bool isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadPumpSchedules();
|
|
}
|
|
|
|
void _loadPumpSchedules() {
|
|
_database.child('schedule').onValue.listen((event) {
|
|
if (event.snapshot.value != null) {
|
|
setState(() {
|
|
pumpSchedules =
|
|
Map<String, dynamic>.from(event.snapshot.value as Map);
|
|
isLoading = false;
|
|
});
|
|
print('Loaded schedules: $pumpSchedules');
|
|
}
|
|
});
|
|
}
|
|
|
|
String _formatTime(int hour, int minute) {
|
|
final time = DateTime(2024, 1, 1, hour, minute);
|
|
return DateFormat('HH:mm').format(time);
|
|
}
|
|
|
|
Widget _buildPumpScheduleCard(String pumpId, Map<String, dynamic> schedule) {
|
|
final pumpSchedule = schedule[pumpId] as Map<dynamic, dynamic>? ?? {};
|
|
final startHour = pumpSchedule['startHour'] ?? 0;
|
|
final startMinute = pumpSchedule['startMinute'] ?? 0;
|
|
final endHour = pumpSchedule['endHour'] ?? 0;
|
|
final endMinute = pumpSchedule['endMinute'] ?? 0;
|
|
|
|
return Card(
|
|
elevation: 4,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
pumpId == 'pump2'
|
|
? 'Jadwal Penyiraman'
|
|
: pumpId == 'pump3'
|
|
? 'Jadwal Pupuk Pertumbuhan'
|
|
: pumpId == 'pump4'
|
|
? 'Jadwal Pupuk Pembuahan'
|
|
: 'Pompa $pumpId',
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Waktu Mulai',
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
_formatTime(startHour, startMinute),
|
|
style: const TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Waktu Selesai',
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
_formatTime(endHour, endMinute),
|
|
style: const TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: ElevatedButton.icon(
|
|
onPressed: () => _showTimePicker(pumpId, true),
|
|
icon: const Icon(Icons.edit),
|
|
label: const Text('Edit Waktu Mulai'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.blue,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: ElevatedButton.icon(
|
|
onPressed: () => _showTimePicker(pumpId, false),
|
|
icon: const Icon(Icons.edit),
|
|
label: const Text('Edit Waktu Selesai'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.blue,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _showTimePicker(String pumpId, bool isStartTime) async {
|
|
final currentSchedule =
|
|
pumpSchedules[pumpId] as Map<dynamic, dynamic>? ?? {};
|
|
final currentHour = isStartTime
|
|
? currentSchedule['startHour'] ?? 0
|
|
: currentSchedule['endHour'] ?? 0;
|
|
final currentMinute = isStartTime
|
|
? currentSchedule['startMinute'] ?? 0
|
|
: currentSchedule['endMinute'] ?? 0;
|
|
|
|
final TimeOfDay? picked = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay(hour: currentHour, minute: currentMinute),
|
|
);
|
|
|
|
if (picked != null) {
|
|
final hour = picked.hour;
|
|
final minute = picked.minute;
|
|
|
|
try {
|
|
if (isStartTime) {
|
|
await _database.child('schedule/$pumpId').update({
|
|
'startHour': hour,
|
|
'startMinute': minute,
|
|
});
|
|
} else {
|
|
await _database.child('schedule/$pumpId').update({
|
|
'endHour': hour,
|
|
'endMinute': minute,
|
|
});
|
|
}
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
'Waktu ${isStartTime ? "mulai" : "selesai"} berhasil diperbarui'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Error: ${e.toString()}'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Jadwal Pompa'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
body: isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: RefreshIndicator(
|
|
onRefresh: () async {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
_loadPumpSchedules();
|
|
},
|
|
child: SingleChildScrollView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
_buildPumpScheduleCard('pump2', pumpSchedules),
|
|
const SizedBox(height: 16),
|
|
if (pumpSchedules.containsKey('pump3Enabled'))
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'Jenis Pupuk',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
ToggleButtons(
|
|
isSelected: [
|
|
!(pumpSchedules['pump3Enabled'] ?? false), // false = pembuahan
|
|
(pumpSchedules['pump3Enabled'] ?? false), // true = pertumbuhan
|
|
],
|
|
onPressed: (index) async {
|
|
final newValue = index == 1; // index 1 = pertumbuhan
|
|
try {
|
|
await _database.child('schedule').update({
|
|
'pump3Enabled': newValue,
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
newValue
|
|
? 'Mode diubah ke Pupuk Pertumbuhan'
|
|
: 'Mode diubah ke Pupuk Pembuahan',
|
|
),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Error: ${e.toString()}'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
borderRadius: BorderRadius.circular(12),
|
|
selectedColor: Colors.white,
|
|
fillColor: Colors.green,
|
|
color: Colors.green,
|
|
children: const [
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Text('Pembuahan'),
|
|
),
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Text('Pertumbuhan'),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
_buildPumpScheduleCard('pump3', pumpSchedules),
|
|
const SizedBox(height: 16),
|
|
_buildPumpScheduleCard('pump4', pumpSchedules),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|