diff --git a/lib/pages/pets_page/pets_page_model.dart b/lib/pages/pets_page/pets_page_model.dart index 53d8cda3..fd099522 100644 --- a/lib/pages/pets_page/pets_page_model.dart +++ b/lib/pages/pets_page/pets_page_model.dart @@ -20,9 +20,14 @@ import 'package:hub/shared/utils/limited_text_size.dart'; import 'package:hub/shared/utils/validator_util.dart'; class PetsPageModel extends FlutterFlowModel { - late String devUUID = ''; - late String userUUID = ''; - late String cliUUID = ''; + static PetsPageModel? _instance = PetsPageModel._internal(); + PetsPageModel._internal(); + factory PetsPageModel() => _instance ?? PetsPageModel._internal(); + static void resetInstance() => _instance = null; + + late final String devUUID; + late final String userUUID; + late final String cliUUID; late String petAmountRegister = '0'; dynamic item; @@ -45,8 +50,6 @@ class PetsPageModel extends FlutterFlowModel { FFUploadedFile? uploadedTempFile; String? imgBase64; - late String defaultDropDownText = ''; - String? dropDownValue1; FormFieldController? dropDownValueController1; @@ -105,27 +108,18 @@ class PetsPageModel extends FlutterFlowModel { String? Function(BuildContext, String?)? textControllerObservationValidator; Future initAsync() async { - defaultDropDownText = FFLocalizations.of(buildContext!).getVariableText( - enText: 'Select an option', - ptText: 'Selecione uma opção', - ); - dropDownValueController1 = FormFieldController(dropDownValue1 ??= defaultDropDownText); - dropDownValueController2 = FormFieldController(dropDownValue2 ??= defaultDropDownText); - log('() => ): $devUUID'); - - WidgetsBinding.instance.addPostFrameCallback((_) async { - devUUID = await StorageHelper().get(SQLiteStorageKey.devUUID.value, Storage.SQLiteStorage) ?? ''; - userUUID = await StorageHelper().get(SQLiteStorageKey.userUUID.value, Storage.SQLiteStorage) ?? ''; - cliUUID = await StorageHelper().get(SQLiteStorageKey.clientUUID.value, Storage.SQLiteStorage) ?? ''; - petAmountRegister = await StorageHelper().get(SQLiteStorageKey.petAmount.value, Storage.SQLiteStorage) ?? ''; - }); + devUUID = (await StorageHelper().get(SQLiteStorageKey.devUUID.value, Storage.SQLiteStorage)) ?? ''; + userUUID = (await StorageHelper().get(SQLiteStorageKey.userUUID.value, Storage.SQLiteStorage)) ?? ''; + cliUUID = (await StorageHelper().get(SQLiteStorageKey.clientUUID.value, Storage.SQLiteStorage)) ?? ''; + petAmountRegister = await StorageHelper().get(SQLiteStorageKey.petAmount.value, Storage.SQLiteStorage) ?? '0'; safeSetState?.call(); - log('() => ): $devUUID'); } @override void initState(BuildContext context) { + initAsync(); + resetInstance(); tabBarController = TabController( vsync: Navigator.of(context), length: 2, @@ -149,7 +143,35 @@ class PetsPageModel extends FlutterFlowModel { textFieldFocusObservation = FocusNode(); textControllerObservation = TextEditingController(); - initAsync(); + 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(); } void setEditForm() { @@ -199,65 +221,26 @@ class PetsPageModel extends FlutterFlowModel { dropDownValueController2 = FormFieldController(dropDownValue2); } - @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(); - } - bool isFormValid(BuildContext context) { if (uploadedLocalFile == null || uploadedLocalFile!.bytes!.isEmpty) { return false; } - if (textControllerName.text.isEmpty || textControllerName.text.length > 80 || textControllerName.text == '') { + if (textControllerName!.text.isEmpty || textControllerName!.text.length > 80 || textControllerName!.text == '') { return false; } - if (textControllerSpecies.text.isEmpty || textControllerSpecies.text == '') { + if (textControllerSpecies!.text.isEmpty || textControllerSpecies!.text == '') { return false; } - if (textControllerRace.text.isEmpty || textControllerRace.text == '') { + if (textControllerRace!.text.isEmpty || textControllerRace!.text == '') { return false; } - if (dropDownValueController1!.value == defaultDropDownText || - dropDownValueController1!.value == '' || - dropDownValueController1!.value == null) { + if (dropDownValue1 == null || dropDownValue1!.isEmpty || dropDownValue1 == '') { return false; } - if (dropDownValueController2!.value == defaultDropDownText || - dropDownValueController2!.value == '' || - dropDownValueController2!.value == null) { + if (dropDownValue2 == null || dropDownValue2!.isEmpty || dropDownValue2 == '') { return false; } - - if (dropDownValue1 == 'Selecione uma opção' || dropDownValue1 == null || dropDownValue1 == '') { - return false; - } - - if (dropDownValue2 == 'Selecione uma opção' || dropDownValue2 == null || dropDownValue2 == '') { - return false; - } - return true; } @@ -280,7 +263,7 @@ class PetsPageModel extends FlutterFlowModel { ); if (response.jsonBody['error'] == false) { - await DialogUtil.success( + DialogUtil.success( buildContext!, FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet successfully updated', @@ -301,7 +284,7 @@ class PetsPageModel extends FlutterFlowModel { ptText: 'Falha ao atualizar o pet', ); } - await DialogUtil.error(buildContext!, errorMessage); + DialogUtil.error(buildContext!, errorMessage); } } @@ -321,7 +304,7 @@ class PetsPageModel extends FlutterFlowModel { ); if (response.jsonBody['error'] == false) { - await DialogUtil.success( + DialogUtil.success( buildContext!, FFLocalizations.of(buildContext!).getVariableText( enText: 'Pet successfully registered', @@ -333,16 +316,15 @@ class PetsPageModel extends FlutterFlowModel { String errorMessage; try { errorMessage = jsonDecode(response.jsonBody['error_msg'])[0]['message'].toString(); - await DialogUtil.error(buildContext!, errorMessage); } catch (e) { 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', ); - await DialogUtil.error(buildContext!, errorMessage); + DialogUtil.error(buildContext!, errorMessage); } else { - await DialogUtil.errorDefault(buildContext!); + DialogUtil.errorDefault(buildContext!); } } } @@ -382,8 +364,10 @@ class PetsPageModel extends FlutterFlowModel { // dropDownValue1 = ''; // dropDownValue2 = ''; - // dropDownValueController1 = FormFieldController('Selecione uma opção'); - // dropDownValueController2 = FormFieldController('Selecione uma opção'); + // dropDownValueController1 = + // FormFieldController('Selecione uma opção'); + // dropDownValueController2 = + // FormFieldController('Selecione uma opção'); } Widget buildPetDetails({ @@ -394,120 +378,123 @@ class PetsPageModel extends FlutterFlowModel { required String cliUUID, required String cliName, required PetsPageModel model, + bool isInteractive = true, }) { return DetailsComponentWidget( buttons: [ // EDIT ACTION - FFButtonWidget( - text: FFLocalizations.of(context).getVariableText( - ptText: 'Editar', - enText: 'Edit', - ), - icon: const Icon(Icons.edit), - onPressed: () async { - context.pop(); + if (isInteractive) + FFButtonWidget( + text: FFLocalizations.of(context).getVariableText( + ptText: 'Editar', + enText: 'Edit', + ), + icon: const Icon(Icons.edit), + onPressed: () async { + context.pop(); - model.isEditing = true; - model.item = item; - model.switchTab(0); - model.setEditForm(); - // model.safeSetState!(); - }, - options: FFButtonOptions( - width: 100, - height: 40, - color: FlutterFlowTheme.of(context).primaryBackground, - elevation: 0, - textStyle: TextStyle( - color: FlutterFlowTheme.of(context).primaryText, - fontSize: LimitedFontSizeUtil.getNoResizeFont(context, 15), - ), - splashColor: const Color.fromARGB(95, 0, 146, 5), - borderSide: BorderSide( + model.isEditing = true; + model.item = item; + model.switchTab(0); + model.setEditForm(); + // model.safeSetState!(); + }, + options: FFButtonOptions( + width: 100, + height: 40, color: FlutterFlowTheme.of(context).primaryBackground, - width: 1, + elevation: 0, + textStyle: TextStyle( + color: FlutterFlowTheme.of(context).primaryText, + fontSize: LimitedFontSizeUtil.getNoResizeFont(context, 15), + ), + splashColor: const Color.fromARGB(95, 0, 146, 5), + borderSide: BorderSide( + color: FlutterFlowTheme.of(context).primaryBackground, + width: 1, + ), + // borderRadius: 12, ), - // borderRadius: 12, ), - ), // DELETE ACTION - FFButtonWidget( - text: FFLocalizations.of(context).getVariableText( - ptText: 'Excluir', - enText: 'Delete', - ), - icon: const Icon(Icons.close), - onPressed: () async { - showAlertDialog( - context, - FFLocalizations.of(context).getVariableText( - ptText: 'Excluir Pet', - enText: 'Delete Pet', - ), - FFLocalizations.of(context).getVariableText( - ptText: 'Você tem certeza que deseja excluir esse pet?', - enText: 'Are you sure you want to delete this pet?', - ), () async { - int id = item['id']; - await PhpGroup.deletePet - .call( - petID: id, - ) - .then((value) { - // Navigator.pop(context, value); - context.pop(value); - context.pop(value); + if (isInteractive) + FFButtonWidget( + text: FFLocalizations.of(context).getVariableText( + ptText: 'Excluir', + enText: 'Delete', + ), + icon: const Icon(Icons.close), + onPressed: () async { + showAlertDialog( + context, + FFLocalizations.of(context).getVariableText( + ptText: 'Excluir Pet', + enText: 'Delete Pet', + ), + FFLocalizations.of(context).getVariableText( + ptText: 'Você tem certeza que deseja excluir esse pet?', + enText: 'Are you sure you want to delete this pet?', + ), () async { + int id = item['id']; + await PhpGroup.deletePet + .call( + petID: id, + ) + .then((value) { + // Navigator.pop(context, value); + context.pop(value); + context.pop(value); - if (value == false) { + if (value == false) { + showSnackbar( + context, + FFLocalizations.of(context).getVariableText( + ptText: 'Erro ao excluir pet', + enText: 'Error deleting pet', + ), + true, + ); + } else if (value == true) { + showSnackbar( + context, + FFLocalizations.of(context).getVariableText( + enText: 'Success deleting pet', + ptText: 'Succeso ao excluir pet', + ), + false, + ); + } + }).catchError((err, stack) { + context.pop(); showSnackbar( context, FFLocalizations.of(context).getVariableText( - ptText: 'Erro ao excluir pet', enText: 'Error deleting pet', + ptText: 'Erro ao excluir pet', ), true, ); - } else if (value == true) { - showSnackbar( - context, - FFLocalizations.of(context).getVariableText( - enText: 'Success deleting pet', - ptText: 'Succeso ao excluir pet', - ), - false, - ); - } - }).catchError((err, stack) { - context.pop(); - showSnackbar( - context, - FFLocalizations.of(context).getVariableText( - enText: 'Error deleting pet', - ptText: 'Erro ao excluir pet', - ), - true, - ); + }); }); - }); - }, - options: FFButtonOptions( - width: 100, - height: 40, - color: FlutterFlowTheme.of(context).primaryBackground, - elevation: 0, - textStyle: TextStyle( - color: FlutterFlowTheme.of(context).primaryText, - fontSize: LimitedFontSizeUtil.getNoResizeFont(context, 15), - ), - splashColor: const Color.fromARGB(131, 255, 17, 0), - borderSide: BorderSide( + }, + options: FFButtonOptions( + width: 100, + height: 40, color: FlutterFlowTheme.of(context).primaryBackground, - width: 1, + elevation: 0, + textStyle: TextStyle( + color: FlutterFlowTheme.of(context).primaryText, + fontSize: LimitedFontSizeUtil.getNoResizeFont(context, 15), + ), + splashColor: const Color.fromARGB(131, 255, 17, 0), + borderSide: BorderSide( + color: FlutterFlowTheme.of(context).primaryBackground, + width: 1, + ), + // borderRadius: 12, ), - // borderRadius: 12, ), - ), ], // 'MIN', 'PEQ', 'MED', 'GRA', 'GIG' labelsHashMap: Map.from({