Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class _MyHomePageState extends State<MyHomePage> {
bool animatePosition = false;
Alignment currentAlignment = Alignment.topLeft;
Alignment resultAlignment = Alignment.topLeft;
int sensitivity = 8;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.blueGrey,
child: AnimatedAlign(
alignment: animatePosition ? resultAlignment : currentAlignment,
duration: const Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
child: GestureDetector(
onHorizontalDragUpdate: (details) {
if (details.delta.dx > sensitivity) {
setState(() {
animatePosition = true;
if (resultAlignment == Alignment.topLeft) {
resultAlignment = Alignment.topRight;
} else if (resultAlignment == Alignment.bottomLeft) {
resultAlignment = Alignment.bottomRight;
}
});
} else if (details.delta.dx < -sensitivity) {
setState(() {
animatePosition = true;
if (resultAlignment == Alignment.topRight) {
resultAlignment = Alignment.topLeft;
} else if (resultAlignment == Alignment.bottomRight) {
resultAlignment = Alignment.bottomLeft;
}
});
}
},
onVerticalDragUpdate: (details) {
if (details.delta.dy > sensitivity) {
setState(() {
animatePosition = true;
if (resultAlignment == Alignment.topLeft) {
resultAlignment = Alignment.bottomLeft;
} else if (resultAlignment == Alignment.topRight) {
resultAlignment = Alignment.bottomRight;
}
});
} else if (details.delta.dy < -sensitivity) {
setState(() {
animatePosition = true;
if (resultAlignment == Alignment.bottomLeft) {
resultAlignment = Alignment.topLeft;
} else if (resultAlignment == Alignment.bottomRight) {
resultAlignment = Alignment.topRight;
}
});
}
},
child: Container(
height: 100,
width: 100,
color: Colors.green,
),
)),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment