简体   繁体   English

如何在 GetX 中使用 AnimationController 和 GetTickerProviderStateMixin

[英]How to use AnimationController in GetX with GetTickerProviderStateMixin

I want to impeliment animation in flutter with GetxController and GetView to controll my animation in every widget I want and this is my Getx Controller:我想用 GetxController 和 GetView 来控制动画,以便在我想要的每个小部件中控制我的动画,这是我的 Getx 控制器:

class TestAnimationController extends GetxController
    with GetTickerProviderStateMixin {
  late AnimationController anim;
  late Animation<double> animation;
  @override
  void onInit() {
    super.onInit();
    initAnimation();
    anim.forward();
  }

  void initAnimation() {
    anim = AnimationController(
      duration: Duration(seconds: 1),
      vsync: this,
    );
    animation = Tween<double>(begin: 1, end: 0).animate(
      CurvedAnimation(
        parent: anim,
        curve: Interval(0.0, 1.0, curve: Curves.linear),
      ),
    );
  }
}

and my GetView class is:我的 GetView 类是:

class TestAnimationHome extends GetView<TestAnimationController> {
  const TestAnimationHome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: controller.animation.value + 100,
          height: controller.animation.value + 100,
          color: Colors.amber,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.anim.forward();
        },
        child: Icon(
          Icons.play_circle,
        ),
      ),
    );
  }
}

I google a lot and do every step that mentioned but my animation doesn't start and I don't know where I did wrong!我经常在谷歌上搜索并执行提到的每一步,但我的动画没有开始,我不知道我哪里做错了!

You are facing this problem because you are not telling the UI to explicitly draw (eg setstate).您遇到这个问题是因为您没有告诉 UI 显式绘制(例如 setstate)。 You can see the below implementation in GetX.您可以在 GetX 中看到以下实现。

I have used addListener function along with GetBuilder in the UI part我在 UI 部分使用了 addListener 函数和 GetBuilder

class TestAnimationController extends GetxController
    with GetTickerProviderStateMixin {
  late AnimationController anim;
  late Animation<double> animation;
  @override
  void onInit() {
    initAnimation();
    anim.forward();
    super.onInit();
  }

  void initAnimation() {
    anim = AnimationController(
      duration: Duration(seconds: 1),
      vsync: this,
    );
    animation = Tween<double>(begin: 10, end: 100).animate(
      CurvedAnimation(
        parent: anim,
        curve: Interval(0.0, 1.0, curve: Curves.linear),
      ),
    )..addListener(() {
        update();
      });
  }
}

class TestAnimationHome extends StatelessWidget {
  const TestAnimationHome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<TestAnimationController>(
        init: TestAnimationController(),
        initState: (_) {},
        builder: (controller) {
          return Center(
            child: Container(
              width: controller.animation.value + 100,
              height: controller.animation.value + 100,
              color: Colors.amber,
            ),
          );
        },
      ),
      floatingActionButton: GetBuilder<TestAnimationController>(
        builder: (controller) {
          return FloatingActionButton(
            onPressed: () {
              controller.anim.forward();
              controller.update();
            },
            child: const Icon(
              Icons.play_circle,
            ),
          );
        },
      ),
    );
  }
}

Hope it answers your question希望它能回答你的问题

In addition what I learned from @cavin-macwan 's answer, I want to mention some of my mistakes to anyone has same problem to undrestand better.除了我从@cavin-macwan 的回答中学到的东西,我想向任何有同样问题的人提及我的一些错误以便更好地理解。

first of all, we need GetBiulder to track any update in controller and when you 'addListener' to your animation and update the controller with it, The GetBuilder will update the UI properly.首先,我们需要GetBiulder来跟踪控制器中的任何更新,当您将“addListener”添加到动画并使用它更新控制器时, GetBuilder将正确更新 UI。

another mistake was Tween<double>(begin: 1, end: 0) that cause the width: of Container change just 1px so you can't see the changes that acually accure.另一个错误是Tween<double>(begin: 1, end: 0)导致width: Container仅更改 1px,因此您看不到准确的更改。 change Tween<double>(begin: 1, end: 0) to Tween<double>(begin: 0, end: 100) or change the dimentions of Container from width: c.animation.value + 100, to width: c.animation.value * 100, .Tween<double>(begin: 1, end: 0)更改为Tween<double>(begin: 0, end: 100)或将Container的尺寸从width: c.animation.value + 100,更改为width: c.animation.value * 100, .

No need controller.update();不需要controller.update(); in floatingActionButton because addListener do it after any AnimationController tickes in the controller.floatingActionButton中,因为addListener在控制器中的任何 AnimationController ticks 之后执行它。

Instead of using GetBuilder in floatingActionButton I prefer using extends GetView<TestAnimationController> becasue less boiler plate code.我更喜欢使用extends GetView<TestAnimationController>而不是在floatingActionButton中使用GetBuilder ,因为样板代码更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM