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?
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
TimerBloc({@required Ticker ticker})
: assert(ticker != null),
_ticker = ticker,
super(TimerInitial(_duration));
@override
void onTransition(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! :-)
Most helpful comment
I see.
Then you can provide your bloc with a different value for the duration.
The reset should also be handled a bit different:
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 馃槉