|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:auto_layout_frame/auto_layout_frame.dart'; |
| 5 | + |
| 6 | +final double kChildSize = 48; |
| 7 | +final double kPadding = 12; |
| 8 | + |
| 9 | +void main() { |
| 10 | + runApp(const SnakeAnimationApp()); |
| 11 | +} |
| 12 | + |
| 13 | +class SnakeAnimationApp extends StatelessWidget { |
| 14 | + const SnakeAnimationApp({super.key}); |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return const MaterialApp( |
| 19 | + title: 'Snake Animation', |
| 20 | + home: SnakeAnimationPage(), |
| 21 | + ); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class SnakeAnimationPage extends StatefulWidget { |
| 26 | + const SnakeAnimationPage({super.key}); |
| 27 | + |
| 28 | + @override |
| 29 | + State<SnakeAnimationPage> createState() => _SnakeAnimationPageState(); |
| 30 | +} |
| 31 | + |
| 32 | +class _SnakeAnimationPageState extends State<SnakeAnimationPage> |
| 33 | + with TickerProviderStateMixin { |
| 34 | + // Animation controller |
| 35 | + late AnimationController _controller; |
| 36 | + late Animation<double> _animation; |
| 37 | + |
| 38 | + // Number of children (colored squares) - constant throughout animation |
| 39 | + static const int _childCount = 11; |
| 40 | + |
| 41 | + // Pre-created children - never rebuilt |
| 42 | + final List<Container> _children = []; |
| 43 | + |
| 44 | + // Animation duration |
| 45 | + static const Duration _duration = Duration(milliseconds: 6000); |
| 46 | + |
| 47 | + @override |
| 48 | + void initState() { |
| 49 | + super.initState(); |
| 50 | + |
| 51 | + // Initialize children once - never rebuild |
| 52 | + for (int i = 0; i < _childCount; i++) { |
| 53 | + _children.add( |
| 54 | + Container( |
| 55 | + width: kChildSize, |
| 56 | + height: kChildSize, |
| 57 | + decoration: BoxDecoration( |
| 58 | + color: _getColor(i), |
| 59 | + borderRadius: BorderRadius.circular(4), |
| 60 | + ), |
| 61 | + child: Center(child: Text("$i")), |
| 62 | + ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + // Setup animation |
| 67 | + _controller = AnimationController(duration: _duration, vsync: this); |
| 68 | + |
| 69 | + // Animation goes from 0 to 4 (representing 4 corners with interpolation) |
| 70 | + _animation = Tween<double>(begin: 0, end: 4).animate( |
| 71 | + CurvedAnimation( |
| 72 | + parent: _controller, // |
| 73 | + curve: Curves.linear, // |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + // Start the animation |
| 78 | + _controller.repeat(); |
| 79 | + } |
| 80 | + |
| 81 | + Color _getColor(int index) { |
| 82 | + final hue = (index / _childCount).toDouble(); |
| 83 | + // Use HSL color with fixed saturation and lightness |
| 84 | + final color = Color.fromARGB( |
| 85 | + 255, |
| 86 | + (255 * hue).toInt(), |
| 87 | + (255 * (1 - hue)).toInt(), |
| 88 | + (128 + 127 * (1 - hue)).toInt(), |
| 89 | + ); |
| 90 | + return color; |
| 91 | + } |
| 92 | + |
| 93 | + // Get alignment based on current corner |
| 94 | + Alignment _getAlignment(int corner) { |
| 95 | + switch (corner) { |
| 96 | + case 0: |
| 97 | + return Alignment.topLeft; |
| 98 | + case 1: |
| 99 | + return Alignment.topRight; |
| 100 | + case 2: |
| 101 | + return Alignment.bottomRight; |
| 102 | + case 3: |
| 103 | + return Alignment.bottomLeft; |
| 104 | + default: |
| 105 | + return Alignment.topLeft; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Get direction based on current corner for layout |
| 110 | + AutoLayoutDirection _getDirection(int corner) { |
| 111 | + switch (corner) { |
| 112 | + case 0: |
| 113 | + case 2: |
| 114 | + return AutoLayoutDirection.horizontal; |
| 115 | + case 1: |
| 116 | + case 3: |
| 117 | + return AutoLayoutDirection.vertical; |
| 118 | + default: |
| 119 | + return AutoLayoutDirection.horizontal; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Calculate snake size based on animation value |
| 124 | + // Value goes from 0 to 4, where each integer is a corner |
| 125 | + // Sequence per corner: extend (25%) -> switch align (25%) -> shrink (25%) -> switch align (25%) |
| 126 | + double _getSnakeSize(double value, double maxSize, double minSize) { |
| 127 | + // Get the fractional part within the current corner's phase |
| 128 | + final cornerProgress = value - value.floor(); |
| 129 | + |
| 130 | + double size; |
| 131 | + |
| 132 | + // First 25%: extend from 1 to max |
| 133 | + if (cornerProgress <= 0.25) { |
| 134 | + size = cornerProgress / 0.25; |
| 135 | + } |
| 136 | + // Next 25%: stay at max (align switch happens here) |
| 137 | + else if (cornerProgress <= 0.50) { |
| 138 | + size = 1; |
| 139 | + } |
| 140 | + // Next 25%: shrink from max to 1 |
| 141 | + else if (cornerProgress <= 0.75) { |
| 142 | + size = 1 - ((cornerProgress - 0.50) / 0.25); |
| 143 | + } |
| 144 | + // Last 25%: stay at 1 (align switch happens here before next corner) |
| 145 | + else { |
| 146 | + size = 0; |
| 147 | + } |
| 148 | + |
| 149 | + return lerpDouble(minSize, maxSize, size)!; |
| 150 | + } |
| 151 | + |
| 152 | + @override |
| 153 | + void dispose() { |
| 154 | + _controller.dispose(); |
| 155 | + super.dispose(); |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + Widget build(BuildContext context) { |
| 160 | + return Scaffold( |
| 161 | + backgroundColor: Colors.grey.shade900, |
| 162 | + body: AnimatedBuilder( |
| 163 | + animation: _animation, |
| 164 | + builder: (context, child) { |
| 165 | + final value = _animation.value; |
| 166 | + |
| 167 | + // Get current corner and interpolation |
| 168 | + final int currentCorner = value.floor(); |
| 169 | + |
| 170 | + final Alignment currentAlignment = _getAlignment(currentCorner); |
| 171 | + final Alignment nextAlignment = _getAlignment( |
| 172 | + (currentCorner + 1) % 4, |
| 173 | + ); |
| 174 | + |
| 175 | + final Alignment alignment = value % 1 < 0.5 |
| 176 | + ? currentAlignment |
| 177 | + : nextAlignment; // Switch alignment at halfway point of corner phase |
| 178 | + |
| 179 | + // Get screen size |
| 180 | + final Size screenSize = MediaQuery.of(context).size; |
| 181 | + // Get snake size |
| 182 | + final double snakeSize = _getSnakeSize( |
| 183 | + value, |
| 184 | + // max size: width/height of screen (depends on direction) minus padding |
| 185 | + (currentCorner % 2 == 0 ? screenSize.width : screenSize.height), |
| 186 | + // min size: just the child size (when fully shrunk) |
| 187 | + kChildSize + kPadding * 2, // child size + padding |
| 188 | + ); |
| 189 | + |
| 190 | + return Align( |
| 191 | + alignment: alignment, |
| 192 | + child: AutoLayoutFrame( |
| 193 | + direction: _getDirection(currentCorner), |
| 194 | + gap: 8, |
| 195 | + width: currentCorner % 2 == 0 |
| 196 | + ? snakeSize |
| 197 | + : kChildSize + kPadding * 2, // child size + padding |
| 198 | + height: currentCorner % 2 == 1 |
| 199 | + ? snakeSize |
| 200 | + : kChildSize + kPadding * 2, // child size + padding |
| 201 | + padding: EdgeInsets.all(kPadding), |
| 202 | + alignChildren: Alignment |
| 203 | + .center, // Keep children centered during alignment switch |
| 204 | + horizontalResizing: AutoLayoutResizing.fixed, |
| 205 | + verticalResizing: AutoLayoutResizing.fixed, |
| 206 | + overflow: AutoLayoutOverflowBehavior.clip, |
| 207 | + backgroundColor: Colors.lime.withValues(alpha: 0.9), |
| 208 | + children: _children, |
| 209 | + ), |
| 210 | + ); |
| 211 | + }, |
| 212 | + ), |
| 213 | + ); |
| 214 | + } |
| 215 | +} |
0 commit comments