Skip to content

Instantly share code, notes, and snippets.

@red-star25
Created September 23, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save red-star25/f67bd4a8fcd7d21fe0bc999764ab0ac0 to your computer and use it in GitHub Desktop.
Save red-star25/f67bd4a8fcd7d21fe0bc999764ab0ac0 to your computer and use it in GitHub Desktop.
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