Skip to content

Commit

Permalink
[FIX] Fix BUG no button request ao mudar estado de tela (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancavalcante committed Jan 10, 2025
1 parent 15fbdaa commit f5973f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 78 deletions.
25 changes: 20 additions & 5 deletions lib/core/state/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@ abstract class Command<T> extends ChangeNotifier {
Result<T>? _result;

bool _running = false;
bool _hasErrorOccurred = false;

Command();

bool get isError => result?.asError != null;
bool get isError => result?.asError != null && !_hasErrorOccurred;

bool get isOk => result?.asValue != null;

Result<T>? get result => _result;

bool get running => _running;

Future<void> _execute(action) async {
if (running) return;
void resetError() {
if (isError) {
_result = null;
notifyListeners();
}
}

Future<void> _execute(Future<Result<T>> Function() action) async {
if (running || _hasErrorOccurred) return;

_result = null;
_hasErrorOccurred = false;
_running = true;
notifyListeners();

try {
_result = await action();
} catch (e) {
_result = Result.error(e);
_hasErrorOccurred = true;
} finally {
_running = false;
_running = false;
notifyListeners();
}
}
Expand All @@ -42,6 +52,11 @@ class Command0<T> extends Command<T> {
Future<Result<T>> execute() async {
await _execute(action);

return _result!;
return result!;
}

@override
void resetError() {
super.resetError();
}
}
127 changes: 54 additions & 73 deletions lib/ui/edit_profile/view/edit_profile_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import 'package:aranduapp/ui/shared/TextEmail.dart';
import 'package:aranduapp/ui/shared/ProfileHeader.dart';
import 'package:aranduapp/ui/shared/TextName.dart';
import 'package:aranduapp/ui/shared/requestbutton.dart';
import 'package:aranduapp/core/state/command.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:async/async.dart'; // Certifique-se de que a biblioteca async está importada

class EditProfile extends StatelessWidget {
const EditProfile({super.key});
Expand All @@ -28,34 +30,7 @@ class EditProfileScreen extends StatelessWidget {
EditProfileViewModel viewModel = Provider.of<EditProfileViewModel>(context);

return Scaffold(
// appBar: AppBar(
// backgroundColor: Theme.of(context).colorScheme.surface,
// elevation: 0,
// title: Center(
// child: Text(
// 'Editar perfil',
// style: TextStyle(
// color: Theme.of(context).colorScheme.onSurface,
// fontSize: 24,
// ),
// ),
// ),
// actions: [
// IconButton(
// color: Theme.of(context).colorScheme.primary,
// icon: const Icon(Icons.notifications),
// onPressed: () {},
// ),
// ],
// leading: IconButton(
// color: Theme.of(context).colorScheme.primary,
// icon: const Icon(Icons.arrow_back),
// onPressed: () {
// Navigator.of(context).pop();
// },
// ),
// ),
body: SingleChildScrollView(
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
children: [
Expand Down Expand Up @@ -91,7 +66,7 @@ class EditProfileScreen extends StatelessWidget {
const SizedBox(height: 100),
_saveButton(context, viewModel),
const SizedBox(height: 20),
// _deleteButton(context),
// _deleteButton(context), // Parte comentada mantida
],
),
);
Expand All @@ -113,10 +88,16 @@ class EditProfileScreen extends StatelessWidget {
command: viewModel.editCommand,
nameButton: "Salvar",
onErrorCallback: (e) {
showDialog<Object>(
context: context,
builder: (BuildContext context) => ErrorPopUp(content: Text(e)),
);
if (viewModel.editCommand.isError) {
showDialog<Object>(
context: context,
builder: (BuildContext dialogContext) {
return ErrorPopUp(content: Text(e.toString()));
},
);
// Resetando o erro após mostrar o pop-up para evitar a repetição
viewModel.editCommand.resetError();
}
},
onSuccessCallback: () {
ScaffoldMessenger.of(context).showSnackBar(
Expand All @@ -126,44 +107,44 @@ class EditProfileScreen extends StatelessWidget {
);
});
}

//Widget _deleteButton(BuildContext context) {
// return ElevatedButton(
// onPressed: () => _showDeleteConfirmationDialog(context),
// style: ElevatedButton.styleFrom(
// minimumSize: Size(0, 50),
// ),
// child: const Text('Deletar Conta'),
// );
//}

//void _showDeleteConfirmationDialog(BuildContext context) {
// showDialog(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// title: const Text('Confirmar Deleção'),
// content: const Text(
// 'Tem certeza de que deseja deletar sua conta? Essa ação não pode ser desfeita.'),
// actions: [
// TextButton(
// onPressed: () {
// Navigator.of(context).pop();
// },
// child: const Text('Cancelar'),
// ),
// ElevatedButton(
// onPressed: () {
// Navigator.of(context).pop();
// ScaffoldMessenger.of(context).showSnackBar(
// const SnackBar(content: Text('Conta deletada com sucesso!')),
// );
// },
// child: const Text('Deletar'),
// ),
// ],
// );
// },
// );
//}
}
// Mantendo o código comentado conforme solicitado
//Widget _deleteButton(BuildContext context) {
// return ElevatedButton(
// onPressed: () => _showDeleteConfirmationDialog(context),
// style: ElevatedButton.styleFrom(
// minimumSize: Size(0, 50),
// ),
// child: const Text('Deletar Conta'),
// );
//}

//void _showDeleteConfirmationDialog(BuildContext context) {
// showDialog(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// title: const Text('Confirmar Deleção'),
// content: const Text(
// 'Tem certeza de que deseja deletar sua conta? Essa ação não pode ser desfeita.'),
// actions: [
// TextButton(
// onPressed: () {
// Navigator.of(context).pop();
// },
// child: const Text('Cancelar'),
// ),
// ElevatedButton(
// onPressed: () {
// Navigator.of(context).pop();
// ScaffoldMessenger.of(context).showSnackBar(
// const SnackBar(content: Text('Conta deletada com sucesso!')),
// );
// },
// child: const Text('Deletar'),
// ),
// ],
// );
// },
// );
//}

0 comments on commit f5973f6

Please sign in to comment.