bubble.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. class Bubbles extends StatefulWidget {
  4. @override
  5. State<StatefulWidget> createState() {
  6. return _BubblesState();
  7. }
  8. }
  9. class Bubble {
  10. Offset speed;
  11. Offset location;
  12. double radius;
  13. double life;
  14. Color color;
  15. double opacity;
  16. double remainingLife;
  17. Bubble() {
  18. Random rd = Random();
  19. this.speed = Offset(-0.5 + rd.nextDouble(), -0.5 + rd.nextDouble());
  20. this.location = Offset(32.0, 32.0);
  21. this.radius = 8.0 + rd.nextDouble() * 16;
  22. this.life = rd.nextDouble() * 20;
  23. this.remainingLife = this.life;
  24. this.color = const Color(0xFF6fa1ea);
  25. }
  26. move() {
  27. this.remainingLife -= 0.1;
  28. this.location = this.location + this.speed;
  29. }
  30. display(Canvas canvas) {
  31. this.opacity = (this.remainingLife / this.life * 100).round() / 100;
  32. var gradient = RadialGradient(
  33. colors: [
  34. Color.fromRGBO(
  35. this.color.red, this.color.green, this.color.blue, this.opacity),
  36. Color.fromRGBO(
  37. this.color.red, this.color.green, this.color.blue, this.opacity),
  38. Color.fromRGBO(this.color.red, this.color.green, this.color.blue, 0.0)
  39. ],
  40. stops: [0.0, 0.5, 1.0],
  41. );
  42. Paint painter = Paint()
  43. ..style = PaintingStyle.fill
  44. ..shader = gradient.createShader(
  45. Rect.fromCircle(center: this.location, radius: this.radius));
  46. canvas.drawCircle(this.location, this.radius, painter);
  47. }
  48. }
  49. class _BubblesState extends State<Bubbles> with TickerProviderStateMixin {
  50. AnimationController animationController;
  51. final bubbles = <Bubble>[];
  52. @override
  53. void initState() {
  54. super.initState();
  55. List.generate(5, (i) {
  56. bubbles.add(Bubble());
  57. });
  58. animationController =
  59. AnimationController(vsync: this, duration: Duration(milliseconds: 1000))
  60. ..addListener(() {
  61. for (int i = 0; i < bubbles.length; i++) {
  62. bubbles[i].move();
  63. if (bubbles[i].remainingLife < 0 || bubbles[i].radius < 0) {
  64. bubbles[i] = Bubble();
  65. }
  66. }
  67. })
  68. ..repeat();
  69. }
  70. @override
  71. void dispose() {
  72. animationController.dispose();
  73. super.dispose();
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return AnimatedBuilder(
  78. animation: animationController,
  79. builder: (context, child) => CustomPaint(
  80. size: Size(64.0, 64.0),
  81. painter: _BubblePainter(bubbles),
  82. ),
  83. );
  84. }
  85. }
  86. class _BubblePainter extends CustomPainter {
  87. final List<Bubble> bubbles;
  88. _BubblePainter(this.bubbles);
  89. @override
  90. void paint(Canvas canvas, Size size) {
  91. for (var bubble in bubbles) {
  92. bubble.display(canvas);
  93. }
  94. }
  95. @override
  96. bool shouldRepaint(_BubblePainter oldDelegate) => true;
  97. }