rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

allow/deny state change #13

Closed smiLLe closed 4 years ago

smiLLe commented 4 years ago

Hi, i just added some code rather than creating an issue. The idea is to have an easy way to deny a state change, for example where state must be distinct. You can always override the state setter, but i thought it might be easier to have a seperate callback function. In addition we can add helper functions like stateIsDistinct()to be used in allowTransition.

Note; there is no documentation etc. I will add more if you like the idea :)

rrousselGit commented 4 years ago

Shouldn't this be the responsibility of the provider instead? Like with updateShouldNotify

smiLLe commented 4 years ago

Mh yes, but what if i am not using a provider .addListener((state) => persist(state)); My widget tree would not update because of updateShouldNotify , however, the listener would be called.

smiLLe commented 4 years ago

Thinking of it. Persisting the state could be a widget as well. updateShouldNotify will work in that case

rrousselGit commented 4 years ago

Even then, we don't need a new method for that.

We can do:

@protected
@overide
set state(MyState state) {
  if (state != this.state) {
    super.state = state;
  }
}
rrousselGit commented 4 years ago

I didn't check == on StateNotifier because that's already the default behavior of StateNotifierProvider, and that would lead to comparing the object twice for no reason.

But the tools necessary to do such things are available

smiLLe commented 4 years ago

Alright, i close this :)