feat: Simplify document upload by removing initial data handling and existing file name references

This commit is contained in:
Vaibhav Surve 2025-09-05 16:22:05 +05:30
parent e12e5ab13b
commit 4d11a2ccf0

View File

@ -13,12 +13,10 @@ import 'package:marco/helpers/widgets/my_snackbar.dart';
class DocumentUploadBottomSheet extends StatefulWidget {
final Function(Map<String, dynamic>) onSubmit;
final Map<String, dynamic>? initialData;
const DocumentUploadBottomSheet({
Key? key,
required this.onSubmit,
this.initialData,
}) : super(key: key);
@override
@ -35,55 +33,6 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
final TextEditingController _descriptionController = TextEditingController();
File? selectedFile;
String? existingFileName;
@override
void initState() {
super.initState();
// Pre-fill if editing
if (widget.initialData != null) {
final data = widget.initialData!;
_docIdController.text = data["documentId"] ?? "";
_docNameController.text = data["name"] ?? "";
_descriptionController.text = data["description"] ?? "";
existingFileName = data["fileName"];
// Preselect category & type
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (data["categoryName"] != null) {
final category = controller.categories.firstWhereOrNull(
(c) => c.name == data["categoryName"],
);
if (category != null) {
setState(() => controller.selectedCategory = category);
await controller.fetchDocumentTypes(category.id);
if (data["documentTypeName"] != null) {
final type = controller.documentTypes.firstWhereOrNull(
(t) => t.name == data["documentTypeName"],
);
if (type != null) {
setState(() => controller.selectedType = type);
}
}
}
}
});
// Prefill tags
if (data["tags"] != null) {
controller.enteredTags.value =
List<String>.from(data["tags"].map((t) => t["name"]));
}
// Prefill file info
controller.selectedFileName = data["fileName"];
controller.selectedFileContentType = data["contentType"];
controller.selectedFileSize = data["fileSize"];
}
}
@override
void dispose() {
@ -96,7 +45,7 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
void _handleSubmit() {
if (!(_formKey.currentState?.validate() ?? false)) return;
if (selectedFile == null && existingFileName == null) {
if (selectedFile == null) {
showAppSnackbar(
title: "Error",
message: "Please attach a document",
@ -173,7 +122,6 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
setState(() {
selectedFile = file;
existingFileName = null;
controller.selectedFileName = fileName;
controller.selectedFileBase64 = base64Data;
controller.selectedFileContentType =
@ -188,7 +136,7 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
@override
Widget build(BuildContext context) {
return BaseBottomSheet(
title: widget.initialData == null ? "Upload Document" : "Edit Document",
title: "Upload Document",
onCancel: () => Navigator.pop(context),
onSubmit: _handleSubmit,
child: Form(
@ -267,11 +215,9 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
/// Single Attachment Section
AttachmentSectionSingle(
attachment: selectedFile,
existingFileName: existingFileName,
onPick: _pickFile,
onRemove: () => setState(() {
selectedFile = null;
existingFileName = null;
controller.selectedFileName = null;
controller.selectedFileBase64 = null;
controller.selectedFileContentType = null;
@ -392,38 +338,18 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
/// ---------------- Single Attachment Widget ----------------
class AttachmentSectionSingle extends StatelessWidget {
final File? attachment;
final String? existingFileName; // new
final VoidCallback onPick;
final VoidCallback? onRemove;
const AttachmentSectionSingle({
Key? key,
this.attachment,
this.existingFileName,
required this.onPick,
this.onRemove,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (attachment == null && existingFileName != null) {
return Row(
children: [
const Icon(Icons.insert_drive_file, color: Colors.blueAccent),
const SizedBox(width: 8),
Expanded(
child: Text(existingFileName!,
style: const TextStyle(fontSize: 14, color: Colors.black87)),
),
if (onRemove != null)
IconButton(
icon: const Icon(Icons.close, color: Colors.red),
onPressed: onRemove,
),
],
);
}
final allowedImageExtensions = ['jpg', 'jpeg', 'png'];
Widget buildTile(File file) {
@ -553,6 +479,7 @@ class AttachmentSectionSingle extends StatelessWidget {
}
}
// ---- Reusable Widgets ----
class LabeledInput extends StatelessWidget {