import 'dart:async'; /// 自定义实现,替代已停维的 `countdown` 包的 [CountDown]。 /// /// 用法与原包兼容: /// ```dart /// var listener = CountDown(Duration(seconds: 90)).stream.listen(null); /// listener.onData((Duration d) { ... d.inSeconds ... }); /// listener.onDone(() { ... }); /// ``` /// /// `stream` 每秒发出剩余 [Duration](从满时长倒数到 0),到达 0 后关闭。 class CountDown { CountDown(this.duration) : assert(!duration.isNegative); final Duration duration; Stream get stream => _countdownStream(); Stream _countdownStream() async* { int remaining = duration.inSeconds; while (remaining > 0) { yield Duration(seconds: remaining); await Future.delayed(const Duration(seconds: 1)); remaining--; } yield Duration.zero; } }