kita lanjutkan ke langkah berikutnya.
Kita akan membuat fungsi edit untuk data pasien terlebih dahulu.
Tambahkan tombol untuk mengedit data di setiap item pasien:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'edit_patient_screen.dart';
class PatientReportScreen extends StatelessWidget {
const PatientReportScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Laporan Data Pasien')),
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('patients').snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
return const Center(child: Text('Tidak ada data pasien.'));
}
final patients = snapshot.data!.docs;
return ListView.builder(
itemCount: patients.length,
itemBuilder: (context, index) {
final data = patients[index].data() as Map<String, dynamic>;
return Card(
margin: const EdgeInsets.all(8.0),
child: ListTile(
title: Text(data['nama']),
subtitle: Text(
'Alamat: ${data['alamat']}\nNo. HP: ${data['noHp']}'),
trailing: IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
EditPatientScreen(patientId: patients[index].id),
),
);
},
),
),
);
},
);
},
),
);
}
}
Buat file baru screens/edit_patient_screen.dart
:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class EditPatientScreen extends StatefulWidget {
final String patientId;
EditPatientScreen({required this.patientId});
@override
_EditPatientScreenState createState() => _EditPatientScreenState();
}
class _EditPatientScreenState extends State<EditPatientScreen> {
final _formKey = GlobalKey<FormState>();
Map<String, dynamic> patientData = {};
@override
void initState() {
super.initState();
_fetchPatientData();
}
void _fetchPatientData() async {
DocumentSnapshot doc = await FirebaseFirestore.instance
.collection('patients')
.doc(widget.patientId)
.get();
if (doc.exists) {
setState(() {
patientData = doc.data() as Map<String, dynamic>;
});
}
}
void _saveChanges() async {
if (_formKey.currentState!.validate()) {
try {
await FirebaseFirestore.instance
.collection('patients')
.doc(widget.patientId)
.update(patientData);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Data berhasil diperbarui!')),
);
Navigator.pop(context);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Gagal memperbarui data: $e')),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Edit Data Pasien')),
body: patientData.isEmpty
? Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: ListView(
children: [
TextFormField(
initialValue: patientData['nama'],
decoration: InputDecoration(labelText: 'Nama'),
onChanged: (value) => patientData['nama'] = value,
validator: (value) =>
value!.isEmpty ? 'Nama tidak boleh kosong' : null,
),
TextFormField(
initialValue: patientData['alamat'],
decoration: InputDecoration(labelText: 'Alamat'),
onChanged: (value) => patientData['alamat'] = value,
),
TextFormField(
initialValue: patientData['noHp'],
decoration: InputDecoration(labelText: 'No HP'),
onChanged: (value) => patientData['noHp'] = value,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _saveChanges,
child: Text('Simpan Perubahan'),
),
],
),
),
),
);
}
}
- Jalankan aplikasi dan buka Laporan Data Pasien.
- Klik tombol edit pada salah satu pasien.
- Ubah data pasien dan simpan.
- Periksa apakah data di Firebase Firestore sudah terupdate.
Jika fitur ini sudah berhasil, kita bisa lanjut ke Edit Data Pemeriksaan.🚀