雪花动画:
    image.png

    1. import 'dart:math';
    2. import 'package:flutter/material.dart';
    3. class AnimatedSnowPage extends StatefulWidget {
    4. const AnimatedSnowPage({Key? key}) : super(key: key);
    5. @override
    6. State<AnimatedSnowPage> createState() => _AnimatedSnowPageState();
    7. }
    8. class _AnimatedSnowPageState extends State<AnimatedSnowPage>
    9. with SingleTickerProviderStateMixin {
    10. AnimationController? _controller;
    11. final List<Snowflake> _snowflakes =
    12. List.generate(1000, (index) => Snowflake());
    13. @override
    14. void initState() {
    15. _controller = AnimationController(
    16. vsync: this,
    17. duration: const Duration(seconds: 1),
    18. )..repeat();
    19. super.initState();
    20. }
    21. @override
    22. void dispose() {
    23. _controller!.dispose();
    24. super.dispose();
    25. }
    26. @override
    27. Widget build(BuildContext context) {
    28. return Scaffold(
    29. body: Center(
    30. child: Container(
    31. width: double.infinity,
    32. height: double.infinity,
    33. decoration: const BoxDecoration(
    34. gradient: LinearGradient(
    35. begin: Alignment.topCenter,
    36. end: Alignment.bottomCenter,
    37. colors: [Colors.blue, Colors.lightBlue, Colors.white],
    38. stops: [0.0, 0.7, 0.95],
    39. ),
    40. ),
    41. child: AnimatedBuilder(
    42. animation: _controller!,
    43. builder: (BuildContext context, Widget? child) {
    44. for (var snow in _snowflakes) {
    45. snow.fall();
    46. }
    47. return CustomPaint(
    48. painter: MyPainter(_snowflakes),
    49. );
    50. },
    51. ),
    52. ),
    53. ),
    54. );
    55. }
    56. }
    57. class MyPainter extends CustomPainter {
    58. final whitePaint = Paint()..color = Colors.white;
    59. final List<Snowflake> _snowflakes;
    60. MyPainter(this._snowflakes);
    61. @override
    62. void paint(Canvas canvas, Size size) {
    63. canvas.drawCircle(size.center(const Offset(0, 150)), 60, whitePaint);
    64. canvas.drawOval(
    65. Rect.fromCenter(
    66. center: size.center(const Offset(0, 320)),
    67. width: 200,
    68. height: 250,
    69. ),
    70. whitePaint);
    71. for (var snowflake in _snowflakes) {
    72. canvas.drawCircle(
    73. Offset(snowflake.x, snowflake.y), snowflake.radius, whitePaint);
    74. }
    75. }
    76. @override
    77. bool shouldRepaint(covariant CustomPainter oldDelegate) {
    78. return true;
    79. }
    80. }
    81. class Snowflake {
    82. double x = Random().nextDouble() * 400;
    83. double y = Random().nextDouble() * 800;
    84. double radius = Random().nextDouble() * 2 + 2;
    85. double velocity = Random().nextDouble() * 4 + 2;
    86. fall() {
    87. y += velocity;
    88. if (y > 800) {
    89. y = 0;
    90. x = Random().nextDouble() * 400;
    91. y = Random().nextDouble() * 800;
    92. radius = Random().nextDouble() * 2 + 2;
    93. velocity = Random().nextDouble() * 4 + 2;
    94. }
    95. }
    96. }