Created
September 23, 2021 10:16
-
-
Save red-star25/f67bd4a8fcd7d21fe0bc999764ab0ac0 to your computer and use it in GitHub Desktop.
This file contains 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
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