Skip to content

Instantly share code, notes, and snippets.

@chooyan-eng
Created March 23, 2025 15:04
Show Gist options
  • Save chooyan-eng/cff161cd5a6499e02e15cb8e33f88757 to your computer and use it in GitHub Desktop.
Save chooyan-eng/cff161cd5a6499e02e15cb8e33f88757 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: AnimationPage()));
}
class AnimationPage extends StatefulWidget {
const AnimationPage({super.key});
@override
State<AnimationPage> createState() => _AnimationPageState();
}
class _AnimationPageState extends State<AnimationPage>
with TickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _sizeAnimation;
late final Animation<Color?> _colorAnimation;
double _size = 0.0;
Color? _color;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
)..addListener(() {
setState(() {
// obtain current size value of [Tween]
_size = _sizeAnimation.value;
// obtain current color value of [ColorTween]
_color = _colorAnimation.value;
});
});
// Tween for [double] value
_sizeAnimation = _controller.drive(Tween(begin: 50.0, end: 150.0));
// Tween for [Color] value
_colorAnimation = _controller.drive(
ColorTween(begin: Colors.blue, end: Colors.red),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
CustomPaint(size: Size.infinite, painter: _GridPainter()),
Positioned(
left: 20,
top: 20,
child: Container(width: _size, height: _size, color: _color),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _controller.forward,
),
);
}
}
/// background grid
class _GridPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint =
Paint()
..color = Colors.grey.withOpacity(0.3)
..strokeWidth = 0.5;
for (var i = 0.0; i < size.width; i += 10) {
canvas.drawLine(Offset(i, 0), Offset(i, size.height), paint);
}
for (var i = 0.0; i < size.height; i += 10) {
canvas.drawLine(Offset(0, i), Offset(size.width, i), paint);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment