flutter - Flutter Timer with Hrs, Mins, Sec - Source
问题描述
我只是想为此启动拍卖应用程序我需要一个计时器我在 YouTube 中搜索得到了链接 https://www.youtube.com/watch?v=Rkkb8P9LsPw&t=121s但它很奇怪我只需要这个内容有人可以帮助这个代码在这里https://github.com/tadaspetra/bookclub/tree/INTEGRATION_BUILD_0_1/book_club
我一个人需要这部分
解决方案
如果没有看到您的代码,很难准确理解您要查找的内容,但是下面的示例可以用作基本倒计时计时器的起点:
import 'dart:async';
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
DateTime _now;
DateTime _auction;
Timer _timer;
@override
Widget build(BuildContext context) {
// Calculates the difference between the auction date time and the current date time.
final difference = _auction.difference(_now);
return Scaffold(
body: Center(
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('${difference.inHours} hours'),
Text('${difference.inMinutes.remainder(60)} minutes'),
Text('${difference.inSeconds.remainder(60)} seconds'),
],
),
),
),
);
}
@override
void dispose() {
// Cancels the timer when the page is disposed.
_timer.cancel();
super.dispose();
}
@override
void initState() {
super.initState();
// Sets the current date time.
_now = DateTime.now();
// Sets the date time of the auction.
_auction = _now.add(Duration(days: 1));
// Creates a timer that fires every second.
_timer = Timer.periodic(
Duration(
seconds: 1,
),
(timer) {
setState(() {
// Updates the current date time.
_now = DateTime.now();
// If the auction has now taken place, then cancels the timer.
if (_auction.isBefore(_now)) {
timer.cancel();
}
});
},
);
}
}
此答案从以下问题中获得灵感,我建议您查看它们以获取有关您的问题的更多信息:
推荐阅读
- reactjs - React Router V6 路由
- javascript - 从 CSV 中的十六进制代码创建位图图像
- android - Android缩放和旋转动画不流畅,跳跃
- python - 按行 ID 比较多个日期列并在 python 或 pandas 中突出显示它们
- excel - 从现有数据构建 UPC 代码 - 保留前导零并消除数字之间的空格
- windows - 使用控制台选项 False 运行 PyInstaller 时,可执行文件出现纸浆错误
- jenkins - Jenkins 管道以成功结束,但没有执行任何阶段
- powershell - 在简单的 Format-list 命令后删除空行
- flask - Flask url_for() 不需要第二个参数,即使它是必需的
- python - Pygame屏幕显示黑色,使用Mac