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

Switch how to change and get #120

Closed
yikwing opened this issue Mar 25, 2019 · 2 comments
Closed

Switch how to change and get #120

yikwing opened this issue Mar 25, 2019 · 2 comments

Comments

@yikwing
Copy link

yikwing commented Mar 25, 2019

redux

I need to get the value of value and can change it

@brianegan
Copy link
Owner

brianegan commented Mar 26, 2019

In that case, you'll need to produce a ViewModel that contains two things: 1) The function to dispatch the action 2) The Value from the state.

class ViewModel {
  bool active;
  void Function(bool active) onChange;
 
  ViewModel(this.active, this.onChange);

  static ViewModel fromStore(Store<CounterState> store) {
    return ViewModel(
      store.state.isSwitchActive,
      (bool active) => store.dispatch(ToggleSwitchAction(active))
    );
   }
)


/// Widget
StoreConnector<CountState, ViewModel>(
  converter: ViewModel.fromStore,
  builder: (context, vm) {
    return Switch(
      onChanged: vm.onChanged,
      value: vm.active,
    );
  }
)

@yikwing
Copy link
Author

yikwing commented Mar 27, 2019

This solved my problem, thanks

@yikwing yikwing closed this as completed Mar 27, 2019
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

No branches or pull requests

2 participants