down_time.dart 4.1 KB

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