Created
March 23, 2025 15:41
-
-
Save chooyan-eng/f6bf1b563d4f4339cb14e98123914da4 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 _controllerX; | |
| late final AnimationController _controllerY; | |
| final _boxSize = 60.0; | |
| double _valueX = 20; | |
| double _valueY = 20; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| /// [AnimationController] for x axis | |
| _controllerX = AnimationController.unbounded(vsync: this)..addListener(() { | |
| setState(() { | |
| _valueX = _controllerX.value; | |
| }); | |
| }); | |
| /// [AnimationController] for y axis | |
| _controllerY = AnimationController.unbounded(vsync: this)..addListener(() { | |
| setState(() { | |
| _valueY = _controllerY.value; | |
| final ground = MediaQuery.of(context).size.height - 100 - _boxSize; | |
| if (_valueY > ground) { | |
| // box now touches the ground | |
| // retrieve the velocity of slightly weaker to opposite(upper) direction | |
| final velocity = _controllerY.velocity * -1 * 0.7; | |
| // stop previous animation | |
| _controllerY.stop(); | |
| // start new animation | |
| _controllerY.animateWith( | |
| GravitySimulation(2000, ground, 1000, velocity), | |
| ); | |
| } | |
| }); | |
| }); | |
| } | |
| void _startFriction(Offset initialPosition, Offset velocity) { | |
| // start animation for x axis | |
| _controllerX.animateWith( | |
| FrictionSimulation(0.5, initialPosition.dx, velocity.dx), | |
| ); | |
| /// simulate y-axis with [GravitySimulation] with previous bouncing logic | |
| _controllerY.animateWith( | |
| GravitySimulation(2000, initialPosition.dy, 1000, velocity.dy), | |
| ); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: GestureDetector( | |
| // grab and move the box | |
| onPanUpdate: (details) { | |
| setState(() { | |
| _valueX = details.globalPosition.dx - _boxSize / 2; | |
| _valueY = details.globalPosition.dy - _boxSize / 2; | |
| }); | |
| }, | |
| // slide the ball | |
| onPanEnd: (details) { | |
| _startFriction( | |
| Offset(_valueX, _valueY), | |
| details | |
| .velocity | |
| .pixelsPerSecond, // velocity exposed by GestureDetector | |
| ); | |
| }, | |
| child: Stack( | |
| children: [ | |
| CustomPaint(size: Size.infinite, painter: _GridPainter()), | |
| Positioned( | |
| top: _valueY, | |
| left: _valueX, | |
| child: Container( | |
| width: _boxSize, | |
| height: _boxSize, | |
| color: Colors.blue, | |
| ), | |
| ), | |
| Positioned( | |
| left: 0, | |
| right: 0, | |
| bottom: 0, | |
| child: Container(height: 100, color: Colors.brown), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| /// 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