Skip to content

Instantly share code, notes, and snippets.

@becek2n
Created May 8, 2020 14:52
Show Gist options
  • Save becek2n/e43385cd0bcb85bd7a1aa69ac89c6dad to your computer and use it in GitHub Desktop.
Save becek2n/e43385cd0bcb85bd7a1aa69ac89c6dad to your computer and use it in GitHub Desktop.
import 'package:bloc/bloc.dart';
class StepperBloc extends Bloc<StepperEvent, StepperState> {
void onContinue() {
dispatch(ContinueEvent());
}
void onBack() {
dispatch(BackEvent());
}
void onSaveRate(double rate, double volume) {
dispatch(SaveRateEvent(rate: rate, volume: volume));
}
@override
StepperState get initialState => StepperState.initial();
@override
Stream<StepperState> mapEventToState(StepperState state, StepperEvent event) async* {
final _currentState = currentState;
if (event is ContinueEvent) {
yield StepperState(currentStep: _currentState.currentStep + 1, rate: _currentState.rate, volume: _currentState.volume);
} else if (event is BackEvent) {
yield StepperState(currentStep: _currentState.currentStep - 1, rate: _currentState.rate, volume: _currentState.volume);
} else if (event is SaveRateEvent) {
yield StepperState(currentStep: _currentState.currentStep, rate: event.rate, volume: event.volume);
}
}
}
abstract class StepperEvent {
}
class ContinueEvent extends StepperEvent {}
class BackEvent extends StepperEvent {}
class SaveRateEvent extends StepperEvent {
final double rate;
final double volume;
SaveRateEvent({this.rate, this.volume});
}
class StepperState {
final int currentStep;
final double rate;
final double volume;
const StepperState({this.currentStep, this.rate, this.volume});
factory StepperState.initial() => StepperState(currentStep: 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment