網站首頁 編程語言 正文
本文實例為大家分享了Flutter自定義年月日倒計時的具體代碼,供大家參考,具體內容如下
因項目中的訂單頁需要一個定時器,比如下單后需要進行倒計時,等倒計時完后,訂單狀態自動關閉。
如圖:
這里顯示等17分25秒就是我們所需要做的功能。
項目里還有其他倒計時類型,比如年月日,天之類的,
先上一個工具類:
//時間格式化,根據總秒數轉換為對應的 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)+'秒后自動取消'; ? } ? //數字格式化,將 0~9 的時間轉換為 00~09 ? static String formatTime(int timeNum) { ? ? return timeNum < 10 ? "0" + timeNum.toString() : timeNum.toString(); ? }
再看一下如何使用:
class OrderPageState extends State{ ? String countContent ; // 倒計時內容 ? 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() { ? ? //設置 1 秒回調一次 ? ? const period = const Duration(seconds: 1); ? ? _timer = Timer.periodic(period, (timer) { ? ? ? //更新界面 ? ? ? setState(() { ? ? ? ? //秒數減一,因為一秒回調一次 ? ? ? ? seconds --; // ? ? ? ?print('我在更新界面>>>>>>>>>>>>>> $seconds'); ? ? ? }); ? ? ? if (seconds == 0) { ? ? ? ? //倒計時秒數為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()); ? ? ? ? ? ? //獲取當期時間 ? ? ? ? ? ? var now = DateTime.now(); ? ? ? ? ? ? var twoHours = _diffDate.difference(now); ? ? ? ? ? ? //獲取總秒數,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
相關推薦
- 2023-11-21 高階函數HoF:用filter()方法編寫一個素數生成函數primes()
- 2022-03-15 Docker Run Cadvisor failed: inotify_add_watch /sy
- 2022-02-21 django update_or_create 報錯GPUModel.MultipleObjects
- 2022-12-07 C++?二維(多維)vector添加一個空項問題_C 語言
- 2022-12-12 C++?Boost?Phoenix庫示例分析使用_C 語言
- 2022-03-17 解決.Net?Core項目發布在IIS上訪問404的問題_實用技巧
- 2022-07-14 python輸出小數精度控制的方法_python
- 2022-12-22 淺析Go語言中數組的這些細節_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支