網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Flutter自定義年月日倒計時的具體代碼,供大家參考,具體內(nèi)容如下
因項(xiàng)目中的訂單頁需要一個定時器,比如下單后需要進(jìn)行倒計時,等倒計時完后,訂單狀態(tài)自動關(guān)閉。
如圖:
這里顯示等17分25秒就是我們所需要做的功能。
項(xiàng)目里還有其他倒計時類型,比如年月日,天之類的,
先上一個工具類:
//時間格式化,根據(jù)總秒數(shù)轉(zhuǎn)換為對應(yīng)的 hh:mm:ss 格式 ? static String constructTime(int seconds) { ? ? int day = seconds ~/3600 ~/24; ? ? int hour = seconds ~/ 3600; ? ? int minute = seconds % 3600 ~/ 60; ? ? int second = seconds % 60; ? ? if(day != 0){ ? ? ? return '$day天$hour小時$minute分$second秒后自動取消'; ? ? }else if(hour != 0){ ? ? ? return '$hour小時$minute分$second秒后自動取消'; ? ? }else if(minute !=0){ ? ? ? return '$minute分$second秒后自動取消'; ? ? }else if(second!=0){ ? ? ? return '$second秒后自動取消'; ? ? }else { ? ? ? return ''; ? ? } // ? ?return formatTime(day)+'天'+formatTime(hour) + "小時" + formatTime(minute) + "分" + formatTime(second)+'秒后自動取消'; ? } ? static String constructVipTime(int seconds) { ? ? int day = seconds ~/3600 ~/ 24; ? ? int hour = seconds ~/ 3600; ? ? int minute = seconds % 3600 ~/ 60; ? ? int second = seconds % 60; ? ? if(day!= 0){ ? ? ? return '剩$day天$hour小時$minute分'; ? ? }else if(hour!= 0){ ? ? ? return '剩$hour小時$minute分'; ? ? }else if(minute!=0){ ? ? ? return '剩$minute分'; ? ? }else { ? ? ? return ''; ? ? } // ? ?return formatTime(day)+'天'+formatTime(hour) + "小時" + formatTime(minute) + "分" + formatTime(second)+'秒后自動取消'; ? } ? //數(shù)字格式化,將 0~9 的時間轉(zhuǎn)換為 00~09 ? static String formatTime(int timeNum) { ? ? return timeNum < 10 ? "0" + timeNum.toString() : timeNum.toString(); ? }
再看一下如何使用:
class OrderPageState extends State{ ? String countContent ; // 倒計時內(nèi)容 ? Timer _timer; ? int seconds = 0; ? ///....... ? ?@override ? Widget build(BuildContext context) { ? ? countContent = Util.constructTime(seconds); ? ? return new Scaffold( ? ? ? ? appBar: HeadTitleBar( ? ? ? ? ? text: '訂單詳情', ? ? ? ? ? rightShow: true, ? ? ? ? ? rightIcPath: ImageConstant.icon_talk_black, ? ? ? ? ? callback: (){ ? ? ? ? ? ? RouteUtil.jump2ChatPage(context); ? ? ? ? ? },), ? ? ? ? body: _buildRoot()); ? } ?? ? ? void cancelTimer() { ? ? if (_timer != null) { ? ? ? _timer.cancel(); ? ? ? _timer = null; ? ? } ? } ? void startTimer() { ? ? //設(shè)置 1 秒回調(diào)一次 ? ? const period = const Duration(seconds: 1); ? ? _timer = Timer.periodic(period, (timer) { ? ? ? //更新界面 ? ? ? setState(() { ? ? ? ? //秒數(shù)減一,因?yàn)橐幻牖卣{(diào)一次 ? ? ? ? seconds --; // ? ? ? ?print('我在更新界面>>>>>>>>>>>>>> $seconds'); ? ? ? }); ? ? ? if (seconds == 0) { ? ? ? ? //倒計時秒數(shù)為0,取消定時器 ? ? ? ? print('我被取消了 ?'); ? ? ? ? cancelTimer(); ? ? ? ? getData(); ? ? ? } ? ? }); ? } ? ? void getData() async{ ? ? await DioUtil.request("xxx", ? ? ? method: DioUtil.GET, ? ? ? ).then((res) { ? ? ? ? var time = res.orderExprieTime; ? ? ? ? if(time !=null){ ? ? ? ? ? try{ ? ? ? ? ? ? var _diffDate = DateTime.parse(time.toString()); ? ? ? ? ? ? //獲取當(dāng)期時間 ? ? ? ? ? ? var now = DateTime.now(); ? ? ? ? ? ? var twoHours = _diffDate.difference(now); ? ? ? ? ? ? //獲取總秒數(shù),2 分鐘為 120 秒 ? ? ? ? ? ? seconds = twoHours.inSeconds; ? ? ? ? ? ? startTimer(); ? ? ? ? ? }catch(e){ ? ? ? ? ? ? seconds = 0; ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? ? loading = true; ? ? ? if(!mounted)return; ? ? ? setState(() { ? ? ? }); ? ? }).catchError((e){ ? ? }); ? } ?? ?@override ? void dispose() { ? ? super.dispose(); ? ? cancelTimer(); ? } }
注意:一定要在dispose方法中銷毀該定時器,不然會一只走下去的,其中countContent
可以寫在具體的text中,大概就是這樣。
原文鏈接:https://blog.csdn.net/xudailong_blog/article/details/105183134
相關(guān)推薦
- 2022-07-13 求兩個降序單鏈表的并集(利用原有鏈點(diǎn))
- 2022-06-28 React18之狀態(tài)批處理的使用_React
- 2023-07-07 maven項(xiàng)目如何引入項(xiàng)目本地jar包
- 2022-04-16 C語言實(shí)現(xiàn)順序循環(huán)隊(duì)列實(shí)例_C 語言
- 2022-04-07 React中代碼分割的4種實(shí)現(xiàn)方式_React
- 2022-11-02 一文搞懂Golang中的內(nèi)存逃逸_Golang
- 2024-03-09 【Redis】什么是緩存雪崩,如何預(yù)防緩存雪崩?
- 2022-12-29 Kotlin標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法應(yīng)用詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支