Flutter Getx Animation

Created At: 2023-07-04 05:42:21 Updated At: 2023-07-04 22:32:25

Getx could be powerful for simple 2D animation in Flutter. Getx is state management library which helps seamlessly manage your state.

Since, with Getx, you don't need to manage your context, it takes away passing context headache you might have BLoC or Riverpod.

With Getx, you don't need to use AnimationController and SingleTickerProviderStateMixin which saves a lot of headache and memory. Because with them, you need to do extra set.

With Getx we just a controller with GetxController and GetBuilder() and inject the controller using Get.put().

In general making 2D movable object you need to move x and y position. We will do both in this tut. We will be moving a bird on the UI. Let's take a look at our controller.

class MoveController extends GetxController{
  double _move = 10;
  double _startPos = 0;

  double get startPos => _startPos;
  double get move => _move;

  void onInit() {
    _startPos = 500;
    // TODO: implement onInit

  void setPos(double valueStart){
    _startPos = valueStart;
    _move = move +10;

  void setMove(){
    _move = 10;


here, _startPos would be responsible for start position of the bird Y position (like y coordinate). And for X position (like x coordinate) we have _move variable.

We have initialized the both variables in onInit() method and initial position is 500 for _startPos.

setPos() and setMove() they are responsible for update _startPos and _move variable.

Let's see our UI.

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  State<HomePage> createState() => _HomePageState();

class _HomePageState extends State<HomePage> {
  var images = [
  int index=0;
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: GetBuilder<MoveController>(builder: (controller) {
          return Stack(
            children: [
                bottom: controller.startPos,
                left: controller.move,
                child: Image.asset(
                width: 50,
                height: 50,
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 100, vertical: 10),
                    child: ElevatedButton(
                      onPressed: () {


                          index = 0;

                        var h = controller.startPos;
                        h = h + controller.move;

                      child: Text("Fly"),

Here we have Stack widget to show images and button on the top of each other. Since we are going to play with position and change position of the bird, Stack and Positioned widgets come into rescue.



Add Reviews