MIF_E31230549/lib/pages/edit_desa.dart

181 lines
5.5 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
class EditDesaPage extends StatefulWidget {
final Map<String, dynamic> data; // data dari tabel
const EditDesaPage({super.key, required this.data});
@override
State<EditDesaPage> createState() => _EditDesaPageState();
}
class _EditDesaPageState extends State<EditDesaPage> {
final _nama = TextEditingController();
bool _loading = false;
// Sesuaikan URL API Update kamu
final String url = "http://ta.myhost.id/E31230549/mposyandu_api/desa/update_desa.php";
@override
void initState() {
super.initState();
// PERBAIKAN: Menggunakan "nama_desa" sesuai dengan output JSON dari API kamu
_nama.text = widget.data["nama_desa"] ?? "";
}
Future<void> _update() async {
if (_nama.text.trim().isEmpty) {
_snack("Nama desa wajib diisi");
return;
}
setState(() => _loading = true);
try {
final res = await http.post(
Uri.parse(url),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: {
"id": widget.data["id"].toString(),
"nama": _nama.text.trim(),
},
).timeout(const Duration(seconds: 15));
final data = json.decode(res.body);
if (!mounted) return;
if (data["success"] == true) {
_snack("Berhasil update");
Navigator.pop(context, true);
} else {
_snack(data["message"] ?? "Gagal update");
}
} catch (e) {
if (mounted) _snack("Error koneksi");
} finally {
if (mounted) setState(() => _loading = false);
}
}
void _snack(String msg) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
msg,
style: GoogleFonts.poppins(fontSize: 12),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xfff5f6fa),
appBar: AppBar(
leading: const BackButton(color: Colors.white),
title: Text(
"",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 14, // Ukuran sedikit lebih besar untuk AppBar
fontWeight: FontWeight.w600,
),
),
backgroundColor: Colors.blue,
),
body: Center(
child: SingleChildScrollView(
child: Container(
width: 500,
margin: const EdgeInsets.all(16),
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: const [
BoxShadow(color: Colors.black12, blurRadius: 12)
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// PERBAIKAN: ID Desa dihapus sesuai permintaan
_label("Nama Desa"),
_input(_nama),
const SizedBox(height: 24),
// ================= BUTTON (WHITE WITH ORANGE BORDER) =================
SizedBox(
width: double.infinity,
child: OutlinedButton(
onPressed: _loading ? null : _update,
style: OutlinedButton.styleFrom(
backgroundColor: Colors.white, // Background Putih
side: const BorderSide(
color: Colors.orange, width: 1.5), // Garis Orange
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: _loading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
color: Colors.orange, strokeWidth: 2),
)
: Text(
"Simpan Perubahan",
style: GoogleFonts.poppins(
color: Colors.orange, // Teks Orange
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
),
),
],
),
),
),
),
);
}
Widget _label(String t) => Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Text(
t,
style: GoogleFonts.poppins(
fontWeight: FontWeight.w600,
fontSize: 12,
),
),
);
Widget _input(TextEditingController c) => TextField(
controller: c,
style: GoogleFonts.poppins(fontSize: 12),
decoration: InputDecoration(
hintText: "Masukkan nama desa...",
hintStyle: GoogleFonts.poppins(fontSize: 12, color: Colors.grey),
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.blue, width: 1.5),
),
),
);
}