Flutter:显示天、小时、分钟和秒的倒计时小部件
Posted
技术标签:
【中文标题】Flutter:显示天、小时、分钟和秒的倒计时小部件【英文标题】:Flutter : Countdown widget that displays days, hours, minutes and seconds 【发布时间】:2020-09-15 03:58:47 【问题描述】:我正在尝试构建一个可显示剩余天数、小时数、分钟数和秒数的扩展小部件(参见屏幕截图)。
我怎样才能做到这一点?
我已经浏览了一大堆 Flutter 文档,但仍然无法正确创建它..
screenshot of the widget I am trying to build
感谢您的帮助!
【问题讨论】:
【参考方案1】:您可以在下面复制粘贴运行完整代码 你可以使用包https://pub.dev/packages/flutter_countdown_timer
代码sn-p
CountdownTimer(
endTime: 1594829147719,
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "days",
hoursSymbol: "hrs ",
minSymbol: "min ",
secSymbol: "sec",
daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
daysSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.green),
hoursSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.amberAccent),
minSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.black),
secSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.deepOrange))
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/countdown_timer.dart';
void main()
runApp(MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
int _counter = 0;
void _incrementCounter()
setState(()
_counter++;
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CountdownTimer(
endTime: 1594829147719,
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "days",
hoursSymbol: "hrs ",
minSymbol: "min ",
secSymbol: "sec",
daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
daysSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.green),
hoursSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.amberAccent),
minSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.black),
secSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.deepOrange)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
【讨论】:
谢谢@chunhunghan!感谢您的指导,我能够实现我的小部件。但是,我想知道您如何为 9 月 4 日下午 6 点的日期/时间生成 endTime 的时间戳? 很高兴为您提供帮助。如果对您有帮助,请将其标记为答案。谢谢。 你可以使用这个endTime:DateTime.parse("2020-09-04 18:00:00Z").millisecondsSinceEpoch, @pger7516 如果您能分享您实现的小部件,那就太好了。以上是关于Flutter:显示天、小时、分钟和秒的倒计时小部件的主要内容,如果未能解决你的问题,请参考以下文章