简体   繁体   English

Flutter animation controller 使用 GetX

[英]Flutter animation controller using GetX

I recently switch to GetX and want to init animation controller in GetxController and can access it in GetView .我最近切换到 GetX 并想在 GetxController 中初始化 animation GetxController并可以在GetView中访问它。 When app started animation gets to go without any problem but can not forward it again.当应用程序启动时,animation 毫无问题地到达 go,但无法再次转发。

class SplashController extends GetxController with GetTickerProviderStateMixin {
  var h = 0.0.obs;
  late AnimationController ac;
  late Animation animation;

  Future<void> initAnimation() async {
    ac = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );
    animation = Tween(begin: 0.0, end: 1.0).animate(ac);
  }

  forwardAnimationFromZero() {
    ac.forward(from: 0);
  }

  @override
  void onInit() {
    super.onInit();
    initAnimation();
    forwardAnimationFromZero();
    ac.addListener(() {
      h.value = animation.value;
    });
  }

  @override
  void onReady() {
    super.onReady();
    forwardAnimationFromZero();
  }

  @override
  void onClose() {
    super.onClose();
    ac.dispose();
  }
}

As you see I extended GetxController with GetTickerProviderStateMixin but The ticker not work properly.如您所见,我使用 GetTickerProviderStateMixin 扩展GetxController with GetTickerProviderStateMixin但自动收报机无法正常工作。 I define var h = 0.0.obs;我定义var h = 0.0.obs; as observable so can access in screen and without it animation does not tick!因为可以观察到所以可以在屏幕上访问并且没有它 animation 不会打勾!

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

  @override
  Widget build(BuildContext context) {
    var c = Get.put(SplashController());
    return Scaffold(
      body: Column(
        children: [
          Container(
            color: Colors.amber,
            width: (controller.animation.value * 100) + 100,
            height: (controller.animation.value * 100) + 100,
            child: Center(
              child: Text(
                controller.animation.value.toString(),
              ),
            ),
          )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          c.ac.forward(from: 0);
        },
        child: Icon(Icons.refresh),
      ),
      appBar: AppBar(
        title: const Text('Splash Page'),
      ),
    );
  }
}

in this view when started animation does not react but when I hot relaod i see it in end state. when change the Container widget to:在此视图中,启动时 animation 没有反应,但当我hot relaod重播时,我看到它结束了 state。当将Container小部件更改为:

          Obx(
            () => Container(
              color: Colors.amber,
              width: (controller.animation.value * 100) + 100,
              height: (controller.h.value * 100) + 100,
              child: Center(
                child: Text(
                  controller.animation.value.toString(),
                ),
              ),
            ),
          ),

respet to ac.addListener(() {h.value = animation.value;});重述ac.addListener(() {h.value = animation.value;}); animation play at the beginning but can't forward again from zero when I press floatingActionButton . animation 开始播放但当我按下floatingActionButton时无法从零再次前进。 What I want:我想要的是:

  1. Why animation does not paly at the beginning without h observable?为什么 animation 在没有h observable 的情况下不会在开头出现?
  2. How can I access animation controller functions in the view?如何在视图中访问 animation controller 函数?
  3. When some animation controller complete I want to start another animation controller.当一些 animation controller 完成后我想开始另一个 animation controller。

Use AnimatedContainer in this part.这部分使用 AnimatedContainer。 Because writing too long will slow down your work as the code grows.因为写得太长会随着代码的增长而减慢你的工作。

Controller Controller

class SplashController extends GetxController {
  var h = 40.0.obs;
}

Page

class SplashPage extends GetView<SplashController> {
  var click = true;
  @override
  Widget build(BuildContext context) {
    return GetBuilder<SplashController>(
      init: Get.put(SplashController()),
      builder: (cnt) {
        return Center(
          child: PageView(
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  ElevatedButton(
                    child: Text('Animate!'),
                    onPressed: () {
                      click
                          ? cnt.h.value = 250.0
                          : cnt.h.value = 50.0;
                      click = !click;
                    },
                  ),
                  Obx(
                    () => AnimatedContainer(
                      duration: Duration(seconds: 1),
                      width: cnt.h.value,
                      height: 40,
                      color: Colors.red,
                    ),
                  ),
                ],
              )
            ],
          ),
        );
      }
    );
  }
}

The following code is used to update your code when it is a StatelessWidget.以下代码用于更新您的代码,当它是 StatelessWidget 时。

Obx(
    () => AnimatedContainer(
       duration: Duration(seconds: 1),
       width: cnt.h.value,
       height: 40,
       color: Colors.red,
       ),
   ),

Each time you click the button, you will be able to zoom in and out.每次单击该按钮时,您都可以放大和缩小。

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

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