简体   繁体   中英

Flutter countdown timer auto start issue

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,
          )
        ],
      ),
    );
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM