Skip to content

Instantly share code, notes, and snippets.

@SlowGen
Created April 3, 2025 17:59
Show Gist options
  • Select an option

  • Save SlowGen/ba6db2e85107b64f4ae8378527192e3e to your computer and use it in GitHub Desktop.

Select an option

Save SlowGen/ba6db2e85107b64f4ae8378527192e3e to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.lightGreen[100],
body: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 5, // Adjust number of plants per row
),
itemCount: 30,
itemBuilder: (context, index) {
return AnimatedPlant(index: index);
},
),
),
);
}
}
class AnimatedPlant extends StatefulWidget {
final int index;
AnimatedPlant({required this.index});
@override
_AnimatedPlantState createState() => _AnimatedPlantState();
}
class _AnimatedPlantState extends State<AnimatedPlant>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
late Animation<double> _growthAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 2 + widget.index % 3), // Vary animation speed
vsync: this,
)..repeat(reverse: true);
_animation = Tween<double>(begin: -0.2, end: 0.2).animate( // larger rotation
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);
_growthAnimation = Tween<double>(begin: 0.2, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeOut,
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Transform.rotate(
angle: _animation.value,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CustomPaint(
painter: PlantPainter(growth: _growthAnimation.value),
size: const Size(80, 120), // Adjusted size
),
],
),
);
},
);
}
}
class PlantPainter extends CustomPainter {
final double growth;
PlantPainter({required this.growth});
@override
void paint(Canvas canvas, Size size) {
final stemPaint = Paint()
..color = Colors.brown[600]!
..strokeWidth = 10
..strokeCap = StrokeCap.round;
final leafPaint = Paint()..color = Colors.green[600]!;
final stemStart = Offset(size.width / 2, size.height);
final stemEnd = Offset(size.width / 2, size.height * (0.3 * growth));
canvas.drawLine(stemStart, stemEnd, stemPaint);
for (int i = 0; i < 10; i++) {
final angle = i * pi / 5;
final leafPath = Path();
final leafSize = 0.3 * growth; // Larger leaves
final leafX = size.width / 2 + cos(angle) * size.width * 0.3 * growth; // wider distribution
final leafY = size.height * 0.4 * growth + sin(angle) * size.height * 0.3 * growth;
leafPath.moveTo(leafX, leafY);
leafPath.quadraticBezierTo(
leafX + cos(angle + pi / 2) * size.width * leafSize,
leafY + sin(angle + pi / 2) * size.height * leafSize,
leafX + cos(angle) * size.width * leafSize * 2,
leafY + sin(angle) * size.height * leafSize * 2,
);
leafPath.quadraticBezierTo(
leafX,
leafY,
leafX,
leafY,
);
canvas.drawPath(leafPath, leafPaint);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment