409 lines
13 KiB
Dart
409 lines
13 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
|
|
import 'bottom_navigation.dart';
|
|
import 'dart:async';
|
|
|
|
class JadwalKotakObat2Screen extends StatefulWidget {
|
|
const JadwalKotakObat2Screen({super.key});
|
|
|
|
@override
|
|
State<JadwalKotakObat2Screen> createState() => _JadwalKotakObat2ScreenState();
|
|
}
|
|
|
|
class _JadwalKotakObat2ScreenState extends State<JadwalKotakObat2Screen> {
|
|
final DatabaseReference _database = FirebaseDatabase.instance.ref();
|
|
final String? uid = FirebaseAuth.instance.currentUser?.uid;
|
|
StreamSubscription<DatabaseEvent>? _jadwalSubscription;
|
|
|
|
List<Map<String, String>> _jadwalList = [];
|
|
bool _showInputIsiUlang = false;
|
|
final TextEditingController _isiUlangController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadJadwalObat2();
|
|
}
|
|
|
|
void _loadJadwalObat2() {
|
|
_jadwalSubscription = _database.child('jadwal/kotak2').onValue.listen((
|
|
event,
|
|
) {
|
|
final snapshot = event.snapshot;
|
|
final data = snapshot.value;
|
|
|
|
if (data is Map) {
|
|
final List<Map<String, String>> jadwal = [];
|
|
|
|
// Ambil nama obat dari path "jadwal/kotak1/obat1"
|
|
final namaObat = data['obat2']?.toString() ?? 'nama obat';
|
|
|
|
data.forEach((key, value) {
|
|
if (key != 'jumlahObat' &&
|
|
key != 'userId' &&
|
|
key != 'obat2' &&
|
|
value is Map) {
|
|
final jam = value['jam']?.toString().padLeft(2, '0');
|
|
final menit = value['menit']?.toString().padLeft(2, '0');
|
|
|
|
if (jam != null && menit != null) {
|
|
jadwal.add({'jam': '$jam:$menit', 'obat': namaObat});
|
|
}
|
|
}
|
|
});
|
|
|
|
jadwal.sort((a, b) => a['jam']!.compareTo(b['jam']!));
|
|
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_jadwalList = jadwal;
|
|
});
|
|
} else {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_jadwalList = [];
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_jadwalSubscription?.cancel(); // batalkan listener Firebase
|
|
_isiUlangController.dispose(); // dispose controller text juga
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _hapusJadwal(String jamString) async {
|
|
if (uid == null) return;
|
|
|
|
final ref = _database.child('jadwal/kotak2');
|
|
final snapshot = await ref.get();
|
|
|
|
if (snapshot.exists && snapshot.value is Map) {
|
|
final Map<dynamic, dynamic> data = Map<dynamic, dynamic>.from(
|
|
snapshot.value as Map,
|
|
);
|
|
|
|
String? keyToDelete;
|
|
|
|
data.forEach((key, value) {
|
|
if (value is Map &&
|
|
value.containsKey('jam') &&
|
|
value.containsKey('menit')) {
|
|
final jam = value['jam'].toString().padLeft(2, '0');
|
|
final menit = value['menit'].toString().padLeft(2, '0');
|
|
final waktu = '$jam:$menit';
|
|
|
|
if (waktu == jamString) {
|
|
keyToDelete = key.toString();
|
|
}
|
|
}
|
|
});
|
|
|
|
if (keyToDelete != null) {
|
|
// Hapus jadwal tertentu
|
|
await ref.child(keyToDelete!).remove();
|
|
|
|
// Ambil ulang data setelah penghapusan
|
|
final updatedSnapshot = await ref.get();
|
|
if (updatedSnapshot.exists && updatedSnapshot.value is Map) {
|
|
final updatedData = Map<dynamic, dynamic>.from(
|
|
updatedSnapshot.value as Map,
|
|
);
|
|
|
|
// Cek apakah masih ada jadwal tersisa (yang berbentuk Map dan punya jam/menit)
|
|
final masihAdaJadwal = updatedData.values.any(
|
|
(item) =>
|
|
item is Map &&
|
|
item.containsKey('jam') &&
|
|
item.containsKey('menit'),
|
|
);
|
|
|
|
if (!masihAdaJadwal) {
|
|
// Tidak ada lagi jadwal, hapus field tambahan
|
|
await ref.child('jumlahObat').remove();
|
|
await ref.child('obat2').remove();
|
|
await ref.child('userId').remove();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void _navigateToAturJadwal2() {
|
|
Navigator.pushNamed(context, '/aturJadwal2');
|
|
}
|
|
|
|
void _toggleIsiUlang() {
|
|
setState(() => _showInputIsiUlang = !_showInputIsiUlang);
|
|
}
|
|
|
|
Future<void> _simpanIsiUlang() async {
|
|
final jumlahText = _isiUlangController.text.trim();
|
|
final jumlahBaru = int.tryParse(jumlahText);
|
|
if (jumlahBaru == null || jumlahBaru <= 0) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('Masukkan jumlah yang valid')));
|
|
return;
|
|
}
|
|
|
|
final ref = _database.child('jadwal/kotak2/0/jumlahObat');
|
|
final snapshot = await ref.get();
|
|
|
|
if (!snapshot.exists || snapshot.value == null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Masukkan jadwal terlebih dahulu')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
int existing = 0;
|
|
if (snapshot.value is int) {
|
|
existing = snapshot.value as int;
|
|
} else if (snapshot.value is String) {
|
|
existing = int.tryParse(snapshot.value as String) ?? 0;
|
|
}
|
|
|
|
if (existing + jumlahBaru > 15) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Jumlah obat tidak boleh lebih dari 15')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
await ref.set(existing + jumlahBaru);
|
|
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('Obat berhasil diisi ulang')));
|
|
|
|
setState(() {
|
|
_showInputIsiUlang = false;
|
|
_isiUlangController.clear();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF3FA535),
|
|
body: Column(
|
|
children: [
|
|
SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
|
child: Row(
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Text(
|
|
'Jadwal Kotak Obat 2',
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
|
decoration: const BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(30),
|
|
topRight: Radius.circular(30),
|
|
),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: _jadwalList.length,
|
|
itemBuilder: (context, index) {
|
|
final item = _jadwalList[index];
|
|
final jam = item['jam'] ?? '';
|
|
final obat = item['obat'] ?? 'nama obat';
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
"${index + 1}. “$obat” $jam",
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
),
|
|
onPressed: () => _hapusJadwal(jam),
|
|
child: const Text(
|
|
'Hapus',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
if (_showInputIsiUlang) ...[
|
|
TextField(
|
|
controller: _isiUlangController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: InputDecoration(
|
|
labelText: 'Masukkan jumlah obat',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
SizedBox(height: 8),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: [
|
|
OutlinedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_showInputIsiUlang = false;
|
|
_isiUlangController.clear();
|
|
});
|
|
},
|
|
child: Text('Batal'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: _simpanIsiUlang,
|
|
child: Text('Simpan'),
|
|
),
|
|
],
|
|
),
|
|
] else
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
ElevatedButton(
|
|
onPressed: _navigateToAturJadwal2,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF3FA535),
|
|
),
|
|
child: const Text(
|
|
"Tambah Jadwal Obat",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
ElevatedButton(
|
|
onPressed: _toggleIsiUlang,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
),
|
|
child: const Text(
|
|
"Isi Ulang Obat 2",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
bottomNavigationBar: BottomNavigationBar(
|
|
currentIndex: 0, // sesuaikan dengan index aktif halaman sekarang
|
|
type: BottomNavigationBarType.fixed,
|
|
selectedItemColor: const Color(0xFF3FA535),
|
|
unselectedItemColor: Colors.grey.shade600,
|
|
showUnselectedLabels: true,
|
|
selectedLabelStyle: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 14,
|
|
),
|
|
unselectedLabelStyle: const TextStyle(
|
|
fontWeight: FontWeight.normal,
|
|
fontSize: 12,
|
|
),
|
|
elevation: 8,
|
|
backgroundColor: Colors.white,
|
|
iconSize: 28,
|
|
onTap: (index) {
|
|
switch (index) {
|
|
case 0:
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => MainNavigation(currentIndex: 0),
|
|
),
|
|
);
|
|
break;
|
|
case 1:
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => MainNavigation(currentIndex: 1),
|
|
),
|
|
);
|
|
break;
|
|
case 2:
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => MainNavigation(currentIndex: 2),
|
|
),
|
|
);
|
|
break;
|
|
case 3:
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => MainNavigation(currentIndex: 3),
|
|
),
|
|
);
|
|
break;
|
|
}
|
|
},
|
|
items: const [
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.home_outlined),
|
|
activeIcon: Icon(Icons.home),
|
|
label: 'Home',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.history_outlined),
|
|
activeIcon: Icon(Icons.history),
|
|
label: 'History',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.notifications_outlined),
|
|
activeIcon: Icon(Icons.notifications),
|
|
label: 'Notification',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.person_outline),
|
|
activeIcon: Icon(Icons.person),
|
|
label: 'Account',
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|