280 lines
8.2 KiB
Dart
280 lines
8.2 KiB
Dart
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<PetsPageWidget> {
|
|
late final TabController tabBarController;
|
|
VoidCallback? onUpdatePet;
|
|
|
|
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<String>? dropDownValueController1;
|
|
|
|
String? dropDownValue2;
|
|
FormFieldController<String>? 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<String>(dropDownValue1 ??= 'Selecione uma opção');
|
|
|
|
dropDownValueController2 =
|
|
FormFieldController<String>(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<void> 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) {
|
|
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',
|
|
));
|
|
clearFields();
|
|
switchTab(1);
|
|
}).catchError((error) {
|
|
log(error.toString());
|
|
DialogUtil.errorDefault(buildContext!);
|
|
});
|
|
}
|
|
|
|
Future<void> 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) {
|
|
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',
|
|
));
|
|
clearFields();
|
|
onUpdatePet?.call();
|
|
}).catchError((error) {
|
|
DialogUtil.errorDefault(buildContext!);
|
|
});
|
|
}
|
|
|
|
void switchTab(int index) {
|
|
tabBarController.animateTo(index);
|
|
if (index == 1) handleEditingChanged(false);
|
|
onUpdatePet?.call();
|
|
}
|
|
|
|
void handleUploadComplete(FFUploadedFile uploadedFile) {
|
|
uploadedLocalFile = uploadedFile;
|
|
}
|
|
|
|
void handleEditingChanged(bool editing) {
|
|
isEditing = editing;
|
|
clearFields();
|
|
}
|
|
|
|
void clearFields() {
|
|
uploadedLocalFile = null;
|
|
textControllerName.text = '';
|
|
textControllerSpecies.text = '';
|
|
textControllerRace.text = '';
|
|
textControllerColor.text = '';
|
|
textControllerData.text = '';
|
|
textControllerObservation.text = '';
|
|
dropDownValueController1 = FormFieldController<String>('');
|
|
dropDownValueController2 = FormFieldController<String>('');
|
|
}
|
|
}
|