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; ApiCallResponse? petsResponse; int? petId; BuildContext? buildContext; // 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) { 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) { return false; } return true; } Future updatePet() async { await PhpGroup.updatePet .call( cliID: AppState().cliUUID, devUUID: AppState().devUUID, userUUID: AppState().userUUID, petID: petId, image: await actions.convertImageFileToBase64( uploadedLocalFile!, ), birthdayDate: textControllerData!.text, color: textControllerColor!.text, breed: textControllerRace!.text, species: textControllerSpecies!.text, name: textControllerName!.text, gender: dropDownValue1!, notes: textControllerObservation!.text, size: dropDownValue2!, ) .then((response) { log(response.jsonBody.toString()); if (response.jsonBody['error'] == true) { DialogUtil.error(buildContext!, jsonDecode(response.jsonBody['error_msg'])[0]['message']); } }).catchError((error) { log(error.toString()); DialogUtil.errorDefault(buildContext!); }); } Future registerPet() async { await PhpGroup.registerPet .call( cliID: AppState().cliUUID, devUUID: AppState().devUUID, userUUID: AppState().userUUID, image: await actions.convertImageFileToBase64( uploadedLocalFile!, ), 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) { DialogUtil.error(buildContext!, jsonDecode(response.jsonBody['error_msg'])[0]['message']); } DialogUtil.success( buildContext!, FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet successfully registered', ptText: 'Pet cadastrado com sucesso', )); }).catchError((error) { DialogUtil.errorDefault(buildContext!); }); } }