You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除timer_count_down的.0显示?求修改方案及替代组件

解决timer_count_down组件显示.0的问题及替代方案

直接修改现有代码解决

你的问题是因为time是double类型,调用toString()会自动带上.0后缀。只需把double转成int再转字符串即可:

Countdown(
  seconds: 60,
  interval: Duration(seconds: 1),
  build: (BuildContext context, double time) => Text(
    "00:${time.toInt().toString()}", // 转成int后再toString,去掉.0
    style: const TextStyle(
      color: AppColors.gray9D9F9E
    ),
  ),
  onFinished: () {
    log("It is finished!");
  },
)

如果后续需要处理非整秒的计时场景,也可以用字符串截断的方式:time.toString().split('.').first,同样能去掉.0后缀。

无法实现时的替代组件

如果上述修改无法满足需求,可选用以下计时器组件:

  • flutter_countdown_timer:支持直接配置时间格式,无需手动拼接字符串,能快速实现mm:ss这类格式的显示
  • countdown_flutter:提供丰富的格式化参数,可自定义显示样式和时间格式
  • 自定义Timer组件:使用Dart原生的Timer.periodic实现,完全掌控计时逻辑和显示格式,灵活性最高

内容的提问来源于stack exchange,提问作者newbie2210

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:16:24