flutter 实现时间流失效果

2024-01-19 04:36

本文主要是介绍flutter 实现时间流失效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用 flutter 进度条组件和 RotatedBox 旋转组件,实现时间流失进度条

进度条组件LinearProgressIndicator

属性说明:

value  0-1 之间 进度

RotatedBox 组件

quarterTurns 属性表示象限

quarterTurns: 0 水平

quarterTurns: 1  顺时针旋转 90 度(quarterTurns = 1)

quarterTurns: -1 逆时针旋转 90 度(quarterTurns = -1)。

代码实现

library flutter_timer_countdown;import 'dart:async';
import 'package:LS/common/global.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';enum CountDownTimerFormat {daysHoursMinutesSeconds,daysHoursMinutes,daysHours,daysOnly,hoursMinutesSeconds,hoursMinutes,hoursOnly,minutesSeconds,minutesOnly,secondsOnly,
}typedef OnTickCallBack = void Function(Duration remainingTime);class TimerCountdown extends StatefulWidget {/// Format for the timer coundtown, choose between different `CountDownTimerFormat`sfinal CountDownTimerFormat format;/// Defines the time when the timer is over.final DateTime endTime;/// Gives you remaining time after every tick.final OnTickCallBack? onTick;/// Function to call when the timer is over.final VoidCallback? onEnd;/// Toggle time units descriptions.final bool enableDescriptions;/// `TextStyle` for the time numbers.final TextStyle? timeTextStyle;/// `TextStyle` for the colons betwenn the time numbers.final TextStyle? colonsTextStyle;/// `TextStyle` for the descriptionfinal TextStyle? descriptionTextStyle;/// Days unit description.final String daysDescription;/// Hours unit description.final String hoursDescription;/// Minutes unit description.final String minutesDescription;/// Seconds unit description.final String secondsDescription;/// Defines the width between the colons and the units.final double spacerWidth;final Widget? rightWidget;// 结束的 组件final Widget? endWidget;const TimerCountdown({required this.endTime,this.format = CountDownTimerFormat.daysHoursMinutesSeconds,this.enableDescriptions = true,this.onEnd,this.timeTextStyle,this.onTick,this.colonsTextStyle,this.descriptionTextStyle,this.daysDescription = "Days",this.hoursDescription = "Hours",this.minutesDescription = "Minutes",this.secondsDescription = "Seconds",this.spacerWidth = 10,this.rightWidget,this.endWidget,});@override_TimerCountdownState createState() => _TimerCountdownState();
}class _TimerCountdownState extends State<TimerCountdown> {bool isEnd = false;Timer? timer;late String countdownDays;late String countdownHours;late String countdownMinutes;late String countdownSeconds;late Duration difference;@overridevoid initState() {_startTimer();super.initState();}@overridevoid dispose() {if (timer != null) {timer!.cancel();}super.dispose();}/// Calculate the time difference between now end the given [endTime] and initialize all UI timer values.////// Then create a periodic `Timer` which updates all fields every second depending on the time difference which is getting smaller./// When this difference reached `Duration.zero` the `Timer` is stopped and the [onEnd] callback is invoked.void _startTimer() {if (widget.endTime.isBefore(Global.serverTime)) {difference = Duration.zero;} else {difference = widget.endTime.difference(Global.serverTime);}countdownDays = _durationToStringDays(difference);countdownHours = _durationToStringHours(difference);countdownMinutes = _durationToStringMinutes(difference);countdownSeconds = _durationToStringSeconds(difference);if (difference == Duration.zero) {if (widget.onEnd != null) {widget.onEnd!();}setState(() {isEnd = true;});} else {timer = Timer.periodic(const Duration(seconds: 1), (timer) {difference = widget.endTime.difference(Global.serverTime);widget.onTick?.call(difference);setState(() {countdownDays = _durationToStringDays(difference);countdownHours = _durationToStringHours(difference);countdownMinutes = _durationToStringMinutes(difference);countdownSeconds = _durationToStringSeconds(difference);});if (difference <= Duration.zero) {timer.cancel();if (widget.onEnd != null) {widget.onEnd!();}setState(() {isEnd = true;});}});}}@overrideWidget build(BuildContext context) {return Wrap(children: [if (widget.rightWidget != null && !isEnd) widget.rightWidget as Widget,_countDownTimerFormat(),],);}/// Builds the UI colons between the time units.Widget _colon({String unit = ":"}) {return Row(children: [SizedBox(width: widget.spacerWidth,),Column(mainAxisSize: MainAxisSize.min,mainAxisAlignment: MainAxisAlignment.center,children: [Text(unit.tr,style: widget.colonsTextStyle,),if (widget.enableDescriptions)const SizedBox(height: 5,),if (widget.enableDescriptions)Text("",style: widget.descriptionTextStyle,),],),SizedBox(width: widget.spacerWidth,),],);}/// Builds the timer days with its description.Widget _days(BuildContext context) {return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.center,children: [Text(countdownDays.tr,style: widget.timeTextStyle,),if (widget.enableDescriptions) const SizedBox(height: 5),if (widget.enableDescriptions)Text(widget.daysDescription.tr,style: widget.descriptionTextStyle,),],);}/// Builds the timer hours with its description.Widget _hours(BuildContext context) {return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.center,children: [Text(countdownHours.tr,style: widget.timeTextStyle,),if (widget.enableDescriptions) const SizedBox(height: 5),if (widget.enableDescriptions)Text(widget.hoursDescription.tr,style: widget.descriptionTextStyle,),],);}/// Builds the timer minutes with its description.Widget _minutes(BuildContext context) {return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.center,children: [Text(countdownMinutes.tr,style: widget.timeTextStyle,),if (widget.enableDescriptions) const SizedBox(height: 5),if (widget.enableDescriptions)Text(widget.minutesDescription.tr,style: widget.descriptionTextStyle,),],);}/// Builds the timer seconds with its description.Widget _seconds(BuildContext context) {return Column(crossAxisAlignment: CrossAxisAlignment.center,mainAxisSize: MainAxisSize.min,children: [Text(countdownSeconds.tr,style: widget.timeTextStyle,),if (widget.enableDescriptions) const SizedBox(height: 5),if (widget.enableDescriptions)Text(widget.secondsDescription.tr,style: widget.descriptionTextStyle,),],);}/// When the selected [CountDownTimerFormat] is leaving out the last unit, this function puts the UI value of the unit before up by one.////// This is done to show the currently running time unit.String _twoDigits(int n, String unitType) {switch (unitType) {case "minutes":if (widget.format == CountDownTimerFormat.daysHoursMinutes ||widget.format == CountDownTimerFormat.hoursMinutes ||widget.format == CountDownTimerFormat.minutesOnly) {if (difference > Duration.zero) {n++;}}if (n >= 10) return "$n";return "0$n";case "hours":if (widget.format == CountDownTimerFormat.daysHours ||widget.format == CountDownTimerFormat.hoursOnly) {if (difference > Duration.zero) {n++;}}if (n >= 10) return "$n";return "0$n";case "days":if (widget.format == CountDownTimerFormat.daysOnly) {if (difference > Duration.zero) {n++;}}if (n >= 10) return "$n";return "0$n";default:if (n >= 10) return "$n";return "0$n";}}/// Convert [Duration] in days to String for UI.String _durationToStringDays(Duration duration) {return _twoDigits(duration.inDays, "days").toString();}/// Convert [Duration] in hours to String for UI.String _durationToStringHours(Duration duration) {if (widget.format == CountDownTimerFormat.hoursMinutesSeconds ||widget.format == CountDownTimerFormat.hoursMinutes ||widget.format == CountDownTimerFormat.hoursOnly) {return _twoDigits(duration.inHours, "hours");} elsereturn _twoDigits(duration.inHours.remainder(24), "hours").toString();}/// Convert [Duration] in minutes to String for UI.String _durationToStringMinutes(Duration duration) {if (widget.format == CountDownTimerFormat.minutesSeconds ||widget.format == CountDownTimerFormat.minutesOnly) {return _twoDigits(duration.inMinutes, "minutes");} elsereturn _twoDigits(duration.inMinutes.remainder(60), "minutes");}/// Convert [Duration] in seconds to String for UI.String _durationToStringSeconds(Duration duration) {if (widget.format == CountDownTimerFormat.secondsOnly) {return _twoDigits(duration.inSeconds, "seconds");} elsereturn _twoDigits(duration.inSeconds.remainder(60), "seconds");}/// Switches the UI to be displayed based on [CountDownTimerFormat].Widget _countDownTimerFormat() {if (isEnd) {return widget.endWidget == null? Container(): widget.endWidget as Widget;}switch (widget.format) {case CountDownTimerFormat.daysHoursMinutesSeconds:return Row(mainAxisSize: MainAxisSize.min,children: [_days(context),_colon(unit: "天"),_hours(context),_colon(unit: ":"),_minutes(context),_colon(unit: ":"),_seconds(context),// Text(":".tr, style: widget.colonsTextStyle),],);case CountDownTimerFormat.daysHoursMinutes:return Row(mainAxisSize: MainAxisSize.min,children: [_days(context),_colon(unit: "天"),_hours(context),_colon(unit: ":"),_minutes(context),],);case CountDownTimerFormat.daysHours:return Row(mainAxisSize: MainAxisSize.min,children: [_days(context),_colon(unit: "天"),_hours(context),],);case CountDownTimerFormat.daysOnly:return Row(mainAxisSize: MainAxisSize.min,children: [_days(context),],);case CountDownTimerFormat.hoursMinutesSeconds:return Row(mainAxisSize: MainAxisSize.min,children: [_hours(context),_colon(unit: ":"),_minutes(context),_colon(unit: ":"),_seconds(context),// Text(":".tr, style: widget.colonsTextStyle),],);case CountDownTimerFormat.hoursMinutes:return Row(mainAxisSize: MainAxisSize.min,children: [_hours(context),_colon(unit: ":"),_minutes(context),],);case CountDownTimerFormat.hoursOnly:return Row(mainAxisSize: MainAxisSize.min,children: [_hours(context),],);case CountDownTimerFormat.minutesSeconds:return Row(mainAxisSize: MainAxisSize.min,children: [_minutes(context),_colon(unit: ":"),_seconds(context),],);case CountDownTimerFormat.minutesOnly:return Row(mainAxisSize: MainAxisSize.min,children: [_minutes(context),],);case CountDownTimerFormat.secondsOnly:return Row(mainAxisSize: MainAxisSize.min,children: [_seconds(context),],);default:return Row(mainAxisSize: MainAxisSize.min,children: [_days(context),_colon(unit: "天"),_hours(context),_colon(unit: ":"),_minutes(context),_colon(unit: ":"),_seconds(context),],);}}
}

这篇关于flutter 实现时间流失效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/621279

相关文章

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

SpringBoot实现基于URL和IP的访问频率限制

《SpringBoot实现基于URL和IP的访问频率限制》在现代Web应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段,为了保护系统资源,需要对接口的访问频率进行限制,下面我们就来看看如何使用... 目录1. 引言2. 项目依赖3. 配置 Redis4. 创建拦截器5. 注册拦截器6. 创建控制器8.