Home > Enterprise >  Flutter countdown timer auto start issue
Flutter countdown timer auto start issue

Time:07-12

im trying to make a countdown timer when the button is pressed, but my countdown timer runs automatically when I open the page.

the problem here is that the timer works as soon as you open the page

packages I use

https://pub.dev/packages/flutter_countdown_timer

package:flutter_countdown_timer/countdown_timer_controller.dart

here is my code

  late final CountdownTimerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = CountdownTimerController(
      endTime: endTime,
      onEnd: onEnd,
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void miningCounter() {
    _controller.start();
    final user = context.read<FirebaseAuthMethods>().user;

    dbRef.child(user.uid).child('miningEndTime').set(endTime);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Mining Screen'),
        backgroundColor: primaryMaterialColor,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CustomMiningButton(icon: Icons.add, onTap: miningCounter),
          CountdownTimer(
            controller: _controller,
            endTime: endTime,
            onEnd: onEnd,
          )
        ],
      ),
    );
  }
}

CodePudding user response:

It's not possible with flutter_countdown_timer as it doesn't support initializing in a paused state.

I'd recommend you to use one of the following countdown timers as they support start/stop/resume controls:

  1. timer_count_down
  2. stop_watch_timer
  3. circular_countdown_timer
  4. circular_countdown
  5. countdown_progress_indicator
  • Related