Created
March 23, 2025 15:18
-
-
Save chooyan-eng/f092258d3a8e603c724a2eb979b20d43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:flutter/physics.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; | |
| double _value = 20; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = AnimationController.unbounded( | |
| vsync: this, | |
| duration: Duration(seconds: 1), | |
| )..addListener(() { | |
| setState(() => _value = _controller.value); | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Stack( | |
| children: [ | |
| CustomPaint(size: Size.infinite, painter: _GridPainter()), | |
| Positioned( | |
| left: 20, | |
| top: _value, | |
| child: Container(width: 60, height: 60, color: Colors.blue), | |
| ), | |
| Positioned( | |
| left: 200, | |
| top: _value + 50, | |
| child: Text('Hello Physics!', style: TextStyle(fontSize: 20)), | |
| ), | |
| Positioned( | |
| left: 100, | |
| top: _value + 100, | |
| child: Icon(Icons.arrow_downward, size: 40), | |
| ), | |
| ], | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () { | |
| _controller.animateWith(GravitySimulation(2000, 20, 1000, 0)); | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| /// 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