Guest User

Untitled

a guest
Jan 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import 'dart:math' show pi;
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. /// A Widget that can be configured to show funky spinning rectangles!
  6. ///
  7. /// ### Usage
  8. ///
  9. /// ```
  10. /// Spinnies(
  11. /// duration: Duration(milliseconds: 2500),
  12. /// size: Size(150, 150),
  13. /// rects: [
  14. /// SpinRect(color: Color(0xFFDB4437), begin: 0.0, end: 1.0),
  15. /// SpinRect(color: Color(0xFFF4B400), begin: 0.25, end: 1.25),
  16. /// SpinRect(color: Color(0xFF4285F4), begin: 0.5, end: 1.5),
  17. /// SpinRect(color: Color(0xFF0F9D58), begin: 0.75, end: 1.75),
  18. /// ],
  19. /// );
  20. /// ```
  21. class Spinnies extends StatefulWidget {
  22. final Duration duration;
  23. final Size size;
  24. final List<SpinRect> rects;
  25.  
  26. const Spinnies({
  27. Key key,
  28. @required this.duration,
  29. @required this.size,
  30. @required this.rects,
  31. }) : super(key: key);
  32.  
  33. @override
  34. _SpinniesState createState() => _SpinniesState();
  35. }
  36.  
  37. class _SpinniesState extends State<Spinnies>
  38. with SingleTickerProviderStateMixin {
  39. AnimationController _controller;
  40.  
  41. @override
  42. void initState() {
  43. _controller = AnimationController(vsync: this, duration: widget.duration)
  44. ..forward()
  45. ..repeat();
  46.  
  47. super.initState();
  48. }
  49.  
  50. @override
  51. void dispose() {
  52. _controller.dispose();
  53. super.dispose();
  54. }
  55.  
  56. @override
  57. Widget build(BuildContext context) {
  58. return SizedBox(
  59. width: widget.size.width,
  60. height: widget.size.height,
  61. child: CustomPaint(
  62. painter: SpinniesPainter(
  63. rects: widget.rects,
  64. animation: _controller,
  65. ),
  66. ),
  67. );
  68. }
  69. }
  70.  
  71. class SpinniesPainter extends CustomPainter {
  72. final List<SpinRect> rects;
  73. // We accept the animation for two reasons: first, it drives when the custom
  74. // painter should repaint. Second, it allows us to get the current value of
  75. // the animation so we can use that to calculate the current rotation of each
  76. // SpinRect.
  77. final Animation<double> animation;
  78.  
  79. SpinniesPainter({
  80. @required this.rects,
  81. @required this.animation,
  82. }) : super(repaint: animation);
  83.  
  84. @override
  85. void paint(Canvas canvas, Size size) {
  86. // Defines the Rect we'll be drawing and it's funky border radii
  87. final funkyRect = RRect.fromLTRBAndCorners(
  88. 0.0,
  89. 0.0,
  90. size.width,
  91. size.height,
  92. topLeft: Radius.elliptical(
  93. size.width * 1.15,
  94. size.height * 1.25,
  95. ),
  96. topRight: Radius.elliptical(
  97. size.width * 1.40,
  98. size.height * 1.40,
  99. ),
  100. bottomRight: Radius.elliptical(
  101. size.width * 1.45,
  102. size.height * 1.10,
  103. ),
  104. bottomLeft: Radius.elliptical(
  105. size.width * 1.10,
  106. size.height * 1.25,
  107. ),
  108. );
  109.  
  110. // The drawing portion of the class. It is responsible for drawing all of
  111. // the different rectangles.
  112. for (final rect in rects) {
  113. canvas.save();
  114.  
  115. // Rotate the correct amount around an origin point
  116. canvas.translate(size.width / 2, size.height / 2);
  117. canvas.rotate(rect.tween.lerp(animation.value) * pi * 2);
  118. canvas.translate(-size.width / 2, -size.height / 2);
  119.  
  120. // Then draw the rectangle
  121. canvas.drawRRect(
  122. funkyRect,
  123. Paint()
  124. ..blendMode = BlendMode.screen
  125. ..strokeWidth = rect.strokeWidth
  126. ..style = PaintingStyle.stroke
  127. ..color = rect.color);
  128.  
  129. canvas.restore();
  130. }
  131. }
  132.  
  133. @override
  134. bool shouldRepaint(SpinniesPainter oldDelegate) {
  135. return true;
  136. }
  137. }
  138.  
  139. class SpinRect {
  140. final Color color;
  141. final double strokeWidth;
  142. final Tween<double> tween;
  143.  
  144. SpinRect({
  145. @required this.color,
  146. @required double begin,
  147. @required double end,
  148. this.strokeWidth = 7.0,
  149. }) : tween = Tween<double>(begin: begin, end: end);
  150. }
Add Comment
Please, Sign In to add comment