日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Flutter自定義年月日倒計時_Android

作者:xudailong_blog ? 更新時間: 2022-05-26 編程語言

本文實(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

欄目分類
最近更新