Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[flutter_timer] Timer Bloc pause event
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_timer/bloc/bloc.dart';
import 'package:flutter_timer/ticker.dart';
class TimerBloc extends Bloc<TimerEvent, TimerState> {
final Ticker _ticker;
final int _duration = 60;
StreamSubscription<int> _tickerSubscription;
TimerBloc({@required Ticker ticker})
: assert(ticker != null),
_ticker = ticker;
TimerState get initialState => Ready(_duration);
Stream<TimerState> mapEventToState(
TimerEvent event,
) async* {
if (event is Start) {
yield* _mapStartToState(event);
} else if (event is Pause) {
yield* _mapPauseToState(event);
} else if (event is Tick) {
yield* _mapTickToState(event);
void dispose() {
Stream<TimerState> _mapStartToState(Start start) async* {
yield Running(start.duration);
_tickerSubscription = _ticker.tick(ticks: start.duration).listen(
(duration) {
dispatch(Tick(duration: duration));
Stream<TimerState> _mapPauseToState(Pause pause) async* {
final state = currentState;
if (state is Running) {
yield Paused(state.duration);
Stream<TimerState> _mapTickToState(Tick tick) async* {
yield tick.duration > 0 ? Running(tick.duration) : Finished();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.