简体   繁体   中英

Flutter Countdown Timer milliseconde

I just got back to Flutter, and wanted to test the countdown for a simple practice app. I can scroll the countdown second by second but I haven't figured out how to do it with the milliseconds.

the current code:

import 'package:quiver/async.dart';



int _start = 10;
  int _current = 10;

void startTimer() {
    CountdownTimer countDownTimer = CountdownTimer(
      Duration(seconds: _start),
      const Duration(seconds: 1),
    );

    var sub = countDownTimer.listen(null);
    sub.onData((duration) {
      setState(() {
        int _current = _start - duration.elapsed.inSeconds;
        dureedefaut = _current;
      });
    });
  }

I updated on my page the variable: dureedefaut

Thank you for your help

Simply use this: Duration(milliseconds: )

you can use milisecond , second and etc in Duration Widget

Thank you, I tested but the countdown no longer stops at 0 but sometimes has negative numbers

void startTimer() {
    CountdownTimer countDownTimer = CountdownTimer(
      Duration(milliseconds: _start),
      const Duration(milliseconds: 1),
    );

    var sub = countDownTimer.listen(null);
    sub.onData((duration) {
      setState(() {
        _current = _start - duration.elapsed.inMilliseconds;
        dureedefaut = _current;
      });
    });
  }
import 'package:quiver/async.dart';



int _start = 10;
  int _current = 10;

void startTimer() {
    CountdownTimer countDownTimer = CountdownTimer(
      Duration(milliseconds: _start),
      const Duration(milliseconds: 1),
    );

    var sub = countDownTimer.listen(null);
    sub.onData((duration) {
      setState(() {
        int _current = _start - duration.elapsed.inSeconds;
        dureedefaut = _current;
      });
    });
  }

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