113 lines
4.2 KiB
Dart
113 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
class EditFamilyScreen extends StatefulWidget {
|
|
final String documentId;
|
|
|
|
const EditFamilyScreen({Key? key, required this.documentId}) : super(key: key);
|
|
|
|
@override
|
|
_EditFamilyScreenState createState() => _EditFamilyScreenState();
|
|
}
|
|
|
|
class _EditFamilyScreenState extends State<EditFamilyScreen> {
|
|
TextEditingController _norumahController = TextEditingController();
|
|
TextEditingController _namakepalaController = TextEditingController();
|
|
TextEditingController _idController = TextEditingController();
|
|
TextEditingController _nomorteleponController = TextEditingController();
|
|
TextEditingController _noalatController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// Load existing data from Firestore and set it to controllers
|
|
FirebaseFirestore.instance.collection('Penghuni').doc(widget.documentId).get().then((snapshot) {
|
|
if (snapshot.exists) {
|
|
setState(() {
|
|
_norumahController.text = snapshot['norumah'];
|
|
_namakepalaController.text = snapshot['namakepala'];
|
|
_idController.text = snapshot['idnomer'];
|
|
_nomorteleponController.text = snapshot['nomortelepon'];
|
|
_noalatController.text = snapshot['noalat'];
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('Edit Data Penghuni'),
|
|
),
|
|
body: SingleChildScrollView(
|
|
padding: EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
TextField(
|
|
controller: _norumahController,
|
|
decoration: InputDecoration(labelText: 'No Rumah'),
|
|
),
|
|
TextField(
|
|
controller: _namakepalaController,
|
|
decoration: InputDecoration(labelText: 'Nama Kepala Keluarga'),
|
|
),
|
|
TextField(
|
|
controller: _idController,
|
|
decoration: InputDecoration(labelText: 'No ID'),
|
|
),
|
|
TextField(
|
|
controller: _nomorteleponController,
|
|
decoration: InputDecoration(labelText: 'No Telepon'),
|
|
),
|
|
TextField(
|
|
controller: _noalatController,
|
|
decoration: InputDecoration(labelText: 'Nomor Seri Alat'),
|
|
),
|
|
SizedBox(height: 20.0),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
if (_norumahController.text.isEmpty ||
|
|
_namakepalaController.text.isEmpty ||
|
|
_idController.text.isEmpty ||
|
|
_nomorteleponController.text.isEmpty ||
|
|
_noalatController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Data Wajib Terisi Semua Dengan Benar'),
|
|
),
|
|
);
|
|
} else {
|
|
// Update data in Firestore
|
|
FirebaseFirestore.instance.collection('Penghuni').doc(widget.documentId).update({
|
|
'norumah': _norumahController.text,
|
|
'namakepala': _namakepalaController.text,
|
|
'idnomer': _idController.text,
|
|
'nomortelepon': _nomorteleponController.text,
|
|
'noalat': _noalatController.text,
|
|
}).then((_) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Data penghuni berhasil dirubah'),
|
|
),
|
|
);
|
|
Navigator.pop(context);
|
|
}).catchError((error) {
|
|
print('Error updating family data: $error');
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Failed to update family data'),
|
|
),
|
|
);
|
|
});
|
|
}
|
|
},
|
|
child: Text('Update'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |