Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isInProgressOrSuccess to FormzSubmissionStatusX extension #63

Closed
maRci002 opened this issue Sep 21, 2022 · 3 comments · Fixed by #83
Closed

Add isInProgressOrSuccess to FormzSubmissionStatusX extension #63

maRci002 opened this issue Sep 21, 2022 · 3 comments · Fixed by #83
Assignees

Comments

@maRci002
Copy link
Contributor

maRci002 commented Sep 21, 2022

Use case

In many cases this is the flow in cubit / bloc when operating with form inputs:

Future<void> submit() async {
  if (state.status.isInProgress || state.status.isSuccess || !state.isValid) {
    return;
  }

  emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
  try {
    await repository.doSomething();
    emit(state.copyWith(status: FormzSubmissionStatus.success));
  } catch (e) {
    emit(state.copyWith(failure: '$e', status: FormzSubmissionStatus.failure));
  }
}

And this happens on UI:

final isInProgress = context.select<MyCubit, bool>(
  (cubit) => cubit.state.status.isInProgress || cubit.state.status.isSuccess,
);

return ElevatedButton(
  onPressed: isInProgress ? null : () => context.read<MyCubit>().submit(),
  child: const Text('Submit'),
);

Proposal

Instead of cubit.state.status.isInProgress || cubit.state.status.isSuccess it would be much easier to write cubit.state.status.isInProgressOrSuccess.

Including myself I only check FormzSubmissionStatusX.isInProgress which can lead to unexpetcted behaviour since in case of succes emit(state.copyWith(status: FormzSubmissionStatus.success)); the ElevatedButton will be clickable which can happen since flutter animates the screens in and out or worse when CircularProgressIndicator is used in case of isInProgress which will be visible for some frames. (Usually when staus becomes succes then form page is closed and still visible while page fades away)

When developer tries to write isI or progr then IDE's autocomplete will show isInProgressOrSuccess along side isInProgress and they will read the docs or just read the changelog.

Note: this proposal should be aplied to 0.5.0-dev.1 I hope it will make to stable version since #48 refactor is awesome and logical.

@maRci002
Copy link
Contributor Author

the ElevatedButton will be clickable which can happen since flutter animates the screens in and out

This might not be true: flutter/flutter#4770
However ElevatedButton will be showed as enabled instead of disabled while changing pages or CircularProgressIndicator will be on screen.

@renancaraujo
Copy link
Contributor

That sounds reasonable, would you like to tackle that @maRci002 ?

@maRci002
Copy link
Contributor Author

maRci002 commented Mar 9, 2023

@renancaraujo yes, I can work on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants