TKK_E32210095/lib/pages/admin_keluarga.dart

141 lines
5.3 KiB
Dart

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:ta_kebakaran_aplikasi/pages/EditDataKeluarga.dart';
import 'package:ta_kebakaran_aplikasi/pages/TambahDataKeluargaScreen.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final databaseReference = FirebaseDatabase.instance.reference();
class FamilyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[900],
floatingActionButton: FloatingActionButton(
onPressed: () {
TambahKeluarga(context); // Panggil fungsi TambahSatpam untuk menampilkan modal bottom sheet
},
child: const Icon(Icons.add),
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('Penghuni').orderBy('norumah').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError) {
return Center(
child: Text('Error: ${snapshot.error}'),
);
}
return ListView(
children: (snapshot.data?.docs ?? []).map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data() as Map<String, dynamic>;
return Container(
margin: EdgeInsets.symmetric(vertical: 5.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: const Color.fromARGB(255, 168, 145, 145).withOpacity(0.5),
spreadRadius: 1,
blurRadius: 2,
offset: Offset(0, 1), // changes position of shadow
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'No Rumah: ${data['norumah']}',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
SizedBox(height: 8.0),
Text(
'Nama Kepala Keluarga: ${data['namakepala']}',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 8.0),
Text(
'Nomor ID: ${data['idnomer']}',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 8.0),
Text(
'No Alat: ${data['noalat']}',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 8.0),
Text(
'No Telepon: ${data['nomortelepon']}',
style: TextStyle(fontSize: 16),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
ElevatedButton(
onPressed: () {
// Aksi untuk tombol edit
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditFamilyScreen(documentId: document.id),
),
);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
child: Text('Edit'),
),
SizedBox(width: 8.0),
ElevatedButton(
onPressed: () {
_showDeleteConfirmationDialog(context, document.id);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
child: Text('Hapus'),
),
],
),
],
),
);
}).toList(),
);
},
),
);
}
void _showDeleteConfirmationDialog(BuildContext context, String documentId) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Konfirmasi Hapus'),
content: Text('Apakah Anda yakin ingin menghapus data ini?'),
actions: <Widget>[
TextButton(
child: Text('Batal'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Hapus'),
onPressed: () {
FirebaseFirestore.instance.collection('Penghuni').doc(documentId).delete();
Navigator.of(context).pop();
},
),
],
);
},
);
}
}