Guest User

Untitled

a guest
Jan 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import 'dart:math' show pi;
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. class Spinnies extends StatefulWidget {
  6. final Duration duration;
  7. final Size size;
  8. final double strokeWidth;
  9. final List<SpinRect> rects;
  10.  
  11. const Spinnies({
  12. Key key,
  13. @required this.duration,
  14. @required this.size,
  15. @required this.strokeWidth,
  16. @required this.rects,
  17. }) : super(key: key);
  18.  
  19. @override
  20. _SpinniesState createState() => _SpinniesState();
  21. }
  22.  
  23. class _SpinniesState extends State<Spinnies>
  24. with SingleTickerProviderStateMixin {
  25. AnimationController _controller;
  26.  
  27. @override
  28. void initState() {
  29. _controller = AnimationController(vsync: this, duration: widget.duration)
  30. ..forward()
  31. ..repeat();
  32.  
  33. super.initState();
  34. }
  35.  
  36. @override
  37. void dispose() {
  38. _controller.dispose();
  39. super.dispose();
  40. }
  41.  
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. backgroundColor: Colors.black,
  46. body: Center(
  47. child: SizedBox(
  48. width: widget.size.width,
  49. height: widget.size.height,
  50. child: CustomPaint(
  51. painter: SpinniesPainter(
  52. rects: widget.rects,
  53. animation: _controller,
  54. ),
  55. ),
  56. ),
  57. ),
  58. );
  59. }
  60. }
  61.  
  62. class SpinniesPainter extends CustomPainter {
  63. final List<SpinRect> rects;
  64. final Animation<double> animation;
  65.  
  66. SpinniesPainter({
  67. @required this.rects,
  68. @required this.animation,
  69. }) : super(repaint: animation);
  70.  
  71. @override
  72. void paint(Canvas canvas, Size size) {
  73. final rrect = RRect.fromLTRBAndCorners(
  74. 0.0,
  75. 0.0,
  76. size.width,
  77. size.height,
  78. topLeft: Radius.elliptical(
  79. size.width * 1.15,
  80. size.height * 1.25,
  81. ),
  82. topRight: Radius.elliptical(
  83. size.width * 1.40,
  84. size.height * 1.40,
  85. ),
  86. bottomRight: Radius.elliptical(
  87. size.width * 1.45,
  88. size.height * 1.10,
  89. ),
  90. bottomLeft: Radius.elliptical(
  91. size.width * 1.10,
  92. size.height * 1.25,
  93. ),
  94. );
  95.  
  96. for (final rect in rects) {
  97. canvas.save();
  98. canvas.translate(size.width / 2, size.height / 2);
  99. canvas.rotate(rect.tween.lerp(animation.value) * pi * 2);
  100. canvas.translate(-size.width / 2, -size.height / 2);
  101. canvas.drawRRect(
  102. rrect,
  103. Paint()
  104. ..blendMode = BlendMode.screen
  105. ..strokeWidth = rect.strokeWidth
  106. ..style = PaintingStyle.stroke
  107. ..color = rect.color);
  108. canvas.restore();
  109. }
  110. }
  111.  
  112. @override
  113. bool shouldRepaint(SpinniesPainter oldDelegate) {
  114. return true;
  115. }
  116. }
  117.  
  118. class SpinRect {
  119. final Color color;
  120. final double strokeWidth;
  121. final Tween<double> tween;
  122.  
  123. SpinRect({
  124. @required this.color,
  125. @required double begin,
  126. @required double end,
  127. this.strokeWidth = 7.0,
  128. }) : tween = Tween<double>(begin: begin, end: end);
  129. }
Add Comment
Please, Sign In to add comment