Bloc: flutter_timer: how to make duration dynamic?

Created on 26 Nov 2020  路  4Comments  路  Source: felangel/bloc

Hi,

i want to make the duration variable dynamic.
The user should determine the seconds. But I just can't do it.
Can someone help me and give an example?

question

Most helpful comment

I see.

TimerBloc({@required Ticker ticker, this.duration})
      : assert(ticker != null),
        _ticker = ticker,
        super(TimerInitial(duration ?? _duration));

final Duration duration;

Then you can provide your bloc with a different value for the duration.

BlocProvider(
        create: (context) => TimerBloc(ticker: Ticker(), duration: const Duration(milliseconds: 500)),
        child: Timer(),
      ),

The reset should also be handled a bit different:

Stream<TimerState> _mapTimerResetToState(TimerReset reset) async* {
    _tickerSubscription?.cancel();
    yield TimerInitial(duration);
  }

This is just a quick solution, normally you'd want to move even the initial duration on the state class to avoid storing data in the bloc itself. I'll leave that as an exercise 馃槉

All 4 comments

Hi @marcoheine 馃憢

class Ticker {
  const Ticker({this.duration});

  final Duration duration;

  Stream<int> tick({int ticks}) {
    return Stream.periodic(duration ?? const Duration(seconds: 1), (x) => ticks - x - 1)
        .take(ticks);
  }
}
BlocProvider(
        create: (context) => TimerBloc(ticker: Ticker(duration: const Duration(milliseconds: 500))),
        child: Timer(),
      ),

Is that what you're after ?

Hi @narcodico,

thank you for the answer!
But this is not what i'm after.

I want to have the 60 dynamical:

`class TimerBloc extends Bloc {

final Ticker _ticker;
static const int _duration = 60;

StreamSubscription _tickerSubscription;

TimerBloc({@required Ticker ticker})
: assert(ticker != null),
_ticker = ticker,
super(TimerInitial(_duration));

@override
void onTransition(Transition transition) {
print(transition);
super.onTransition(transition);
}`

I see.

TimerBloc({@required Ticker ticker, this.duration})
      : assert(ticker != null),
        _ticker = ticker,
        super(TimerInitial(duration ?? _duration));

final Duration duration;

Then you can provide your bloc with a different value for the duration.

BlocProvider(
        create: (context) => TimerBloc(ticker: Ticker(), duration: const Duration(milliseconds: 500)),
        child: Timer(),
      ),

The reset should also be handled a bit different:

Stream<TimerState> _mapTimerResetToState(TimerReset reset) async* {
    _tickerSubscription?.cancel();
    yield TimerInitial(duration);
  }

This is just a quick solution, normally you'd want to move even the initial duration on the state class to avoid storing data in the bloc itself. I'll leave that as an exercise 馃槉

@narcodico thank you so much! :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nerder picture nerder  路  3Comments

abinvp picture abinvp  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

krusek picture krusek  路  3Comments

ricktotec picture ricktotec  路  3Comments