TKK_E32221348/Mobile/lib/screens/penitipan.dart

156 lines
4.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:quickalert/quickalert.dart';
import '../Service/api_service.dart';
import '../Model/penitipan_model.dart';
class PenitipanFormSheet extends StatefulWidget {
final int lokerId;
final String? title;
final String? buttonText;
final void Function(PenitipanModel result)? onSuccess;
const PenitipanFormSheet({
Key? key,
required this.lokerId,
this.title,
this.buttonText,
this.onSuccess,
}) : super(key: key);
@override
_PenitipanFormSheetState createState() => _PenitipanFormSheetState();
}
class _PenitipanFormSheetState extends State<PenitipanFormSheet> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _userController = TextEditingController();
bool isSubmitting = false;
Future<void> _submit() async {
if (!_formKey.currentState!.validate()) return;
setState(() {
isSubmitting = true;
});
try {
// Kirim penitipan
PenitipanModel result = await ApiService.kirimPenitipan(
user: _userController.text,
lokerId: widget.lokerId,
);
widget.onSuccess?.call(result);
// Tampilkan popup sukses jika penitipan berhasil
await QuickAlert.show(
context: context,
type: QuickAlertType.success,
title: 'Berhasil!',
text: 'Penitipan berhasil dikirim.',
onConfirmBtnTap: () {
Navigator.of(context).pop();
Navigator.of(context).pop(); // Tutup popup setelah tombol konfirmasi
},
);
} catch (e) {
// Tampilkan popup error jika ada kegagalan
await QuickAlert.show(
context: context,
type: QuickAlertType.error,
title: 'Gagal',
text: 'Gagal mengirim penitipan: $e',
onConfirmBtnTap: () {
Navigator.of(context).pop();
Navigator.of(context).pop(); // Tutup popup setelah tombol konfirmasi
},
);
} finally {
setState(() {
isSubmitting = false;
});
}
}
@override
void dispose() {
_userController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final title = widget.title ?? 'Form Penitipan Loker ${widget.lokerId}';
final buttonText = widget.buttonText ?? 'Kirim Penitipan';
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
left: 16,
right: 16,
top: 24,
),
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
SizedBox(height: 16),
TextFormField(
controller: _userController,
decoration: InputDecoration(labelText: 'Nama Pengguna'),
validator: (value) =>
value == null || value.isEmpty ? 'Masukkan nama pengguna' : null,
),
SizedBox(height: 20),
// Tombol submit penitipan
isSubmitting
? CircularProgressIndicator()
: Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: _submit,
child: Text(buttonText),
),
),
// SizedBox(width: 12),
// Expanded(
// child: ElevatedButton.icon(
// onPressed: () async {
// try {
// bool success = await ApiService.updateLokerStatus(widget.lokerId, 'servis');
// if (success) {
// Navigator.of(context).pop();
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text("Status loker diubah menjadi 'servis'")),
// );
// }
// } catch (e) {
// Navigator.of(context).pop();
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text("Gagal ubah status: $e")),
// );
// }
// },
// icon: Icon(Icons.build),
// label: Text('Servis'),
// style: ElevatedButton.styleFrom(backgroundColor: Colors.orange),
// ),
// ),
],
),
SizedBox(height: 16),
],
),
),
),
);
}
}