down_time.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'package:flutter/material.dart';
  2. import 'dart:math';
  3. import 'dart:async';
  4. typedef DownTimeEndListener = void Function();
  5. /// Description: splash 倒计时图标
  6. /// Time : 08/17/2023 Thursday
  7. /// Author : liuyuqi.gov@msn.cn
  8. class DownTimeWidget extends StatefulWidget {
  9. DownTimeWidget(
  10. {Key? key,
  11. required this.clors,
  12. required this.width,
  13. required this.strokeWidth,
  14. required this.time,
  15. required this.textStyle,
  16. required this.endListener})
  17. : super();
  18. Color clors;
  19. double width;
  20. double strokeWidth;
  21. int time;
  22. TextStyle textStyle;
  23. DownTimeEndListener endListener;
  24. @override
  25. State<StatefulWidget> createState() => DownTimeState();
  26. }
  27. class DownTimeState extends State<DownTimeWidget>
  28. with TickerProviderStateMixin {
  29. late AnimationController controller;
  30. late CurvedAnimation curvedAnimation;
  31. late Tween<double> animationTween;
  32. double angle = 0;
  33. late Animation<double> animation;
  34. int _time = 0;
  35. @override
  36. void initState() {
  37. super.initState();
  38. _time = (widget.time / 1000).toInt();
  39. controller = AnimationController(
  40. vsync: this, duration: Duration(milliseconds: widget.time));
  41. curvedAnimation = CurvedAnimation(parent: controller, curve: Curves.linear);
  42. animationTween = Tween(begin: 0.0, end: 360.0);
  43. animation = animationTween.animate(curvedAnimation);
  44. animation.addStatusListener((status) {
  45. if (status == AnimationStatus.completed) {
  46. widget.endListener();
  47. }
  48. });
  49. animation.addListener(() {
  50. angle = animation.value;
  51. setState(() {});
  52. });
  53. controller.forward();
  54. }
  55. @override
  56. void dispose() {
  57. controller.dispose();
  58. super.dispose();
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return Container(
  63. width: widget.width,
  64. height: widget.width,
  65. decoration: BoxDecoration(
  66. borderRadius: BorderRadius.circular(widget.width / 2),
  67. color: Colors.white),
  68. child: Stack(
  69. children: <Widget>[
  70. Center(
  71. child: DownTimeText(
  72. time: _time,
  73. textStyle: widget.textStyle,
  74. ),
  75. ),
  76. CustomPaint(
  77. painter: DrawArcPainter(
  78. colors: widget.clors,
  79. angle: angle,
  80. width: widget.width,
  81. ),
  82. ),
  83. ],
  84. ),
  85. );
  86. }
  87. }
  88. class DrawArcPainter extends CustomPainter {
  89. DrawArcPainter(
  90. {required this.colors,
  91. required this.angle,
  92. required this.width,
  93. required this.mStrokeWidth});
  94. Color colors;
  95. double mStrokeWidth;
  96. double width;
  97. double angle;
  98. double angleToRadian(double angle) => angle * (pi / 180.0);
  99. double radianToAngle(double radian) => radian * (180.0 / pi);
  100. @override
  101. void paint(Canvas canvas, Size size) {
  102. Paint paint = Paint()
  103. ..color = colors == null ? Colors.red : colors
  104. ..strokeWidth = mStrokeWidth == null ? 2.0 : mStrokeWidth
  105. ..style = PaintingStyle.stroke
  106. ..strokeCap = StrokeCap.round;
  107. Rect rect = Rect.fromLTWH(0.0, 0.0, width, width);
  108. canvas.drawArc(rect, 0.0, angleToRadian(angle), false, paint);
  109. }
  110. @override
  111. bool shouldRepaint(CustomPainter oldDelegate) {
  112. return true;
  113. }
  114. }
  115. class DownTimeText extends StatefulWidget {
  116. DownTimeText({Key? key, required this.time, required this.textStyle})
  117. : super(key: key);
  118. int time;
  119. TextStyle textStyle;
  120. @override
  121. State<StatefulWidget> createState() => DownTimeTextState();
  122. }
  123. class DownTimeTextState extends State<DownTimeText> {
  124. DownTimeTextState();
  125. int _time;
  126. Timer timer;
  127. @override
  128. void initState() {
  129. super.initState();
  130. _time = widget.time;
  131. startDownTimer();
  132. }
  133. void startDownTimer() {
  134. timer = Timer.periodic(Duration(seconds: 1), (time) {
  135. if (_time == null || _time == 0) {
  136. setState(() {});
  137. timer.cancel();
  138. return;
  139. }
  140. _time--;
  141. setState(() {});
  142. });
  143. }
  144. @override
  145. void dispose() {
  146. timer.cancel();
  147. super.dispose();
  148. }
  149. @override
  150. Widget build(BuildContext context) {
  151. return Text(
  152. "倒计时:$_time",
  153. style: widget.textStyle,
  154. );
  155. }
  156. }