import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:hub/backend/api_requests/api_calls.dart'; import 'package:hub/backend/api_requests/api_manager.dart'; import 'package:hub/flutter_flow/flutter_flow_util.dart'; import 'package:hub/flutter_flow/form_field_controller.dart'; import 'package:hub/pages/pets_page/pets_page_widget.dart'; import 'package:hub/shared/utils/dialog_util.dart'; import 'package:hub/shared/utils/log_util.dart'; import '/custom_code/actions/index.dart' as actions; class PetsPageModel extends FlutterFlowModel { late final TabController tabBarController; VoidCallback? onUpdatePet; VoidCallback? onRegisterPet; VoidCallback? safeSetState; final GlobalKey registerFormKey = GlobalKey(); final GlobalKey updateFormKey = GlobalKey(); ApiCallResponse? petsResponse; int? petId; BuildContext? buildContext; bool isEditing = false; // Controller para o Upload de Arquivos bool isDataUploading = false; FFUploadedFile? uploadedLocalFile; String? imgBase64; // Controller para o DropDown String? dropDownValue1; FormFieldController? dropDownValueController1; String? dropDownValue2; FormFieldController? dropDownValueController2; // Controller para o TextField FocusNode? textFieldFocusName; TextEditingController? textControllerName; String? textControllerNameValidator(BuildContext context, String? val) { if (val == null || val.isEmpty) { return FFLocalizations.of(context).getVariableText( enText: 'This field is required', ptText: 'Este campo é obrigatório', ); } return null; } FocusNode? textFieldFocusSpecies; TextEditingController? textControllerSpecies; String? textControllerSpeciesValidator(BuildContext context, String? val) { if (val == null || val.isEmpty) { return FFLocalizations.of(context).getVariableText( enText: 'This field is required', ptText: 'Este campo é obrigatório', ); } return null; } FocusNode? textFieldFocusRace; TextEditingController? textControllerRace; String? textControllerRaceValidator(BuildContext context, String? val) { if (val == null || val.isEmpty) { return FFLocalizations.of(context).getVariableText( enText: 'This field is required', ptText: 'Este campo é obrigatório', ); } return null; } FocusNode? textFieldFocusColor; TextEditingController? textControllerColor; String? Function(BuildContext, String?)? textControllerColorValidator; DateTime? selectedDate; FocusNode? textFieldFocusData; TextEditingController? textControllerData; String? textControllerDataValidator(BuildContext context, String? val) { return null; } FocusNode? textFieldFocusObservation; TextEditingController? textControllerObservation; String? Function(BuildContext, String?)? textControllerObservationValidator; @override void initState(BuildContext context) { // Se liga! Esse é o controller do TabBar tabBarController = TabController( vsync: Navigator.of(context), length: 2, ); textFieldFocusName = FocusNode(); textControllerName = TextEditingController(); // textControllerNameValidator = // (context, value) => _textControllerNameValidator(context, value); textFieldFocusSpecies = FocusNode(); textControllerSpecies = TextEditingController(); textFieldFocusRace = FocusNode(); textControllerRace = TextEditingController(); textFieldFocusColor = FocusNode(); textControllerColor = TextEditingController(); textFieldFocusData = FocusNode(); textControllerData = TextEditingController( text: dateTimeFormat( 'dd/MM/yyyy', DateTime.now(), )); textFieldFocusObservation = FocusNode(); textControllerObservation = TextEditingController(); dropDownValueController1 = FormFieldController(dropDownValue1 ??= 'Selecione uma opção'); dropDownValueController2 = FormFieldController(dropDownValue2 ??= 'Selecione uma opção'); } @override void dispose() { tabBarController.dispose(); textFieldFocusName?.dispose(); textControllerName?.dispose(); textFieldFocusSpecies?.dispose(); textControllerSpecies?.dispose(); textFieldFocusRace?.dispose(); textControllerRace?.dispose(); textFieldFocusColor?.dispose(); textControllerColor?.dispose(); textFieldFocusData?.dispose(); textControllerData?.dispose(); textFieldFocusObservation?.dispose(); textControllerObservation?.dispose(); dropDownValueController1?.dispose(); dropDownValueController2?.dispose(); } // Validador do formulário bool isFormValid(BuildContext context) { if (uploadedLocalFile == null || uploadedLocalFile!.bytes!.isEmpty) { return false; } if (textControllerName.text.isEmpty || textControllerName.text.length > 80 || textControllerName.text == '') { return false; } if (textControllerSpecies.text.isEmpty || textControllerSpecies.text == '') { return false; } if (textControllerRace.text.isEmpty || textControllerRace.text == '') { return false; } if (dropDownValue1 == null || dropDownValue1!.isEmpty || dropDownValue1 == '') { return false; } if (dropDownValue2 == null || dropDownValue2!.isEmpty || dropDownValue2 == '') { return false; } return true; } Future updatePet() async { var img = await actions.convertImageFileToBase64(uploadedLocalFile!); img = "base64;jpeg,$img"; await PhpGroup.updatePet .call( cliID: AppState().cliUUID, devUUID: AppState().devUUID, userUUID: AppState().userUUID, petID: petId, image: img, birthdayDate: textControllerData!.text, color: textControllerColor!.text, breed: textControllerRace!.text, species: textControllerSpecies!.text, name: textControllerName!.text, gender: dropDownValue1!, notes: textControllerObservation!.text, size: dropDownValue2!, ) .then((response) { if (response.jsonBody['error'] == true || isFormValid(buildContext!) == false) { DialogUtil.error(buildContext!, jsonDecode(response.jsonBody['error_msg'])[0]['message']); } DialogUtil.success( buildContext!, FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet successfully updated', ptText: 'Pet atualizado com sucesso', )); onUpdatePet?.call(); switchTab(1); }); } Future registerPet() async { var img = await actions.convertImageFileToBase64(uploadedLocalFile!); img = "base64;jpeg,$img"; await PhpGroup.registerPet .call( cliID: AppState().cliUUID, devUUID: AppState().devUUID, userUUID: AppState().userUUID, image: img, birthdayDate: textControllerData!.text, color: textControllerColor!.text, breed: textControllerRace!.text, species: textControllerSpecies!.text, name: textControllerName!.text, gender: dropDownValue1!, size: dropDownValue2!, notes: textControllerObservation!.text, ) .then((response) { if (response.jsonBody['error'] == true) { String errorMessage = ''; try { errorMessage = jsonDecode(response.jsonBody['error_msg'])[0] ['message'] .toString(); } catch (e) { errorMessage = 'An error occurred.'; if (response.jsonBody['error_msg'] == "Limite de Cadastro de Pet Atingido.") { errorMessage = FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet registration limit reached', ptText: 'Limite de cadastro de pets atingido', ); } } DialogUtil.error(buildContext!, errorMessage); } else if (response.jsonBody['error'] == false) { DialogUtil.success( buildContext!, FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet successfully registered', ptText: 'Pet cadastrado com sucesso', )); onRegisterPet?.call(); } }); } void switchTab(int index) { tabBarController.animateTo(index); if (index == 1) handleEditingChanged(false); } void handleUploadComplete(FFUploadedFile uploadedFile) { uploadedLocalFile = uploadedFile; safeSetState?.call(); } void handleEditingChanged(bool editing) { isEditing = editing; clearFields(); } void clearFields() { uploadedLocalFile = null; textControllerName?.clear(); textControllerName?.clear(); textControllerSpecies?.clear(); textControllerRace?.clear(); textControllerColor?.clear(); textControllerData?.clear(); textControllerObservation?.clear(); dropDownValue1 = null; dropDownValue2 = null; dropDownValueController1 = FormFieldController(null); dropDownValueController2 = FormFieldController(null); } }