Reputation: 1169
I'm using a timer countdown library but it immediatly starts counting down once the widget it created.
I have a controller for it which has a .pause()
method which works.
How would I call the pause function right after the widgets creation?
Ex: How could I pause it immediately?
class MyWidget extends StatelessWidget {
final CountdownController controller = CountdownController();
@override
Widget build(BuildContext context) {
return Countdown(
controller: controller, // controller.pause() would pause it
seconds: 20,
build: (context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
onFinished: () {
print('Timer is done!');
},
);
}
}
Upvotes: 0
Views: 2426
Reputation: 68400
That library doesn't have a feature to disallow auto-start, but you could implement something outside the library to achieve a similar effect.
You could convert your stateless widget into a stateful one and use a flag to build a plain text if countdown is not started and build the countdown otherwise.
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool started = false;
final CountdownController controller = CountdownController();
final int seconds = 20;
@override
Widget build(BuildContext context) {
return !_started //< ==== When you set this to true, countdown will start
? Text(seconds)
: Countdown(
controller: controller,
seconds: seconds,
build: (context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
onFinished: () {
print('Timer is done!');
},
);
}
}
Somewhere in the code you will need to have (maybe as a callback for a button pressed)
setState(() => started = true);
Upvotes: 1