Skip to content

Instantly share code, notes, and snippets.

@chooyan-eng
Created March 23, 2025 14:55
Show Gist options
  • Select an option

  • Save chooyan-eng/e6485e5418a114ea67f04720a39b3671 to your computer and use it in GitHub Desktop.

Select an option

Save chooyan-eng/e6485e5418a114ea67f04720a39b3671 to your computer and use it in GitHub Desktop.
import 'dart:async';
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> {
double _value = 0.0;
late Timer _timer;
void _startAnimation() {
_timer = Timer.periodic(const Duration(milliseconds: 10), (_) {
setState(() => _value += 0.1);
if (_value >= 1.0) {
_timer.cancel();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
CustomPaint(size: Size.infinite, painter: _GridPainter()),
Positioned(
left: 20,
top: 20,
child: Opacity(
opacity: _value.clamp(0.0, 1.0),
child: Container(width: 60, height: 60, color: Colors.blue),
),
),
],
),
floatingActionButton: FloatingActionButton(onPressed: _startAnimation),
);
}
}
/// 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