Created
March 23, 2025 15:33
-
-
Save chooyan-eng/61c4779abc68441c828cf5250fc4b431 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; | |
}); | |
}); | |
} | |
void _startFriction(Offset initialPosition, Offset velocity) { | |
// start animation for x axis | |
_controllerX.animateWith( | |
FrictionSimulation(0.5, initialPosition.dx, velocity.dx), | |
); | |
// start another animation for y axis | |
_controllerY.animateWith( | |
FrictionSimulation(0.5, initialPosition.dy, 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, | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
/// 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